Including dependencies in a jar with Maven

后端 未结 13 1965
盖世英雄少女心
盖世英雄少女心 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

    There's the shade maven plugin. It can be used to package and rename dependencies (to omit dependency problems on the classpath).

    0 讨论(0)
  • 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 <module> 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).

    0 讨论(0)
  • 2020-11-22 01:21

    This post may be a bit old, but I also had the same problem recently. The first solution proposed by John Stauffer is a good one, but I had some problems as I am working this spring. The spring's dependency-jars I use have some property files and xml-schemas declaration which share the same paths and names. Although these jars come from the same versions, the jar-with-dependencies maven-goal was overwriting theses file with the last file found.

    In the end, the application was not able to start as the spring jars could not find the correct properties files. In this case the solution propose by Rop have solved my problem.

    Also since then, the spring-boot project now exist. It has a very cool way to manage this problem by providing a maven goal which overload the package goal and provide its own class loader. See spring-boots Reference Guide

    0 讨论(0)
  • 2020-11-22 01:24

    Have a look at this answer:

    I am creating an installer that runs as a Java JAR file and it needs to unpack WAR and JAR files into appropriate places in the installation directory. The dependency plugin can be used in the package phase with the copy goal and it will download any file in the Maven repository (including WAR files) and write them where ever you need them. I changed the output directory to ${project.build.directory}/classes and then end result is that the normal JAR task includes my files just fine. I can then extract them and write them into the installation directory.

    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>getWar</id>
            <phase>package</phase>
            <goals>
                <goal>copy</goal>
            </goals>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>the.group.I.use</groupId>
                        <artifactId>MyServerServer</artifactId>
                        <version>${env.JAVA_SERVER_REL_VER}</version>
                        <type>war</type>
                        <destFileName>myWar.war</destFileName>
                    </artifactItem>
                </artifactItems>
                <outputDirectory>${project.build.directory}/classes</outputDirectory>
            </configuration>
        </execution>
    </executions>
    

    0 讨论(0)
  • 2020-11-22 01:26

    You can do this using the maven-assembly plugin with the "jar-with-dependencies" descriptor. Here's the relevant chunk from one of our pom.xml's that does this:

      <build>
        <plugins>
          <!-- any other plugins -->
          <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
              <execution>
                <phase>package</phase>
                <goals>
                  <goal>single</goal>
                </goals>
              </execution>
            </executions>
            <configuration>
              <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
              </descriptorRefs>
            </configuration>
          </plugin>
        </plugins>
      </build>
    
    0 讨论(0)
  • 2020-11-22 01:26

    If you want to do an executable jar file, them need set the main class too. So the full configuration should be.

        <plugins>
                <plugin>
                     <artifactId>maven-assembly-plugin</artifactId>
                     <executions>
                         <execution>
                              <phase>package</phase>
                              <goals>
                                  <goal>single</goal>
                              </goals>
                          </execution>
                      </executions>
                      <configuration>
                           <!-- ... -->
                           <archive>
                               <manifest>
                                     <mainClass>fully.qualified.MainClass</mainClass>
                               </manifest>
                           </archive>
                           <descriptorRefs>
                               <descriptorRef>jar-with-dependencies</descriptorRef>
                          </descriptorRefs>
                     </configuration>
             </plugin>
       </plugins>
    
    0 讨论(0)
提交回复
热议问题