Including dependencies in a jar with Maven

后端 未结 13 1968
盖世英雄少女心
盖世英雄少女心 2020-11-22 00:39

Is there a way to force maven(2.0.9) to include all the dependencies in a single jar file?

I have a project the builds into a single jar file. I want the classes fro

13条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 01:20

    If you (like me) dont particularly like the jar-with-dependencies approach described above, the maven-solution I prefer is to simply build a WAR-project, even if it is only a stand-alone java application you are building:

    1. Make a normal maven jar-project, that will build your jar-file (without the dependencies).

    2. Also, setup a maven war-project (with only an empty src/main/webapp/WEB-INF/web.xml file, which will avoid a warning/error in the maven-build), that only has your jar-project as a dependency, and make your jar-project a under your war-project. (This war-project is only a simple trick to wrap all your jar-file dependencies into a zip-file.)

    3. Build the war-project to produce the war-file.

    4. In the deployment-step, simply rename your .war-file to *.zip and unzip it.

    You should now have a lib-directory (which you can move where you want it) with your jar and all the dependencies you need to run your application:

    java -cp 'path/lib/*' MainClass
    

    (The wildcard in classpath works in Java-6 or higher)

    I think this is both simpler to setup in maven (no need to mess around with the assembly plugin) and also gives you a clearer view of the application-structure (you will see the version-numbers of all dependent jars in plain view, and avoid clogging everything into a single jar-file).

提交回复
热议问题