Including dependencies in a jar with Maven

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

    With Maven 2, the right way to do this is to use the Maven2 Assembly Plugin which has a pre-defined descriptor file for this purpose and that you could just use on the command line:

    mvn assembly:assembly -DdescriptorId=jar-with-dependencies
    

    If you want to make this jar executable, just add the main class to be run to the plugin configuration:

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>my.package.to.my.MainClass</mainClass>
          </manifest>
        </archive>
      </configuration>
    </plugin>
    

    If you want to create that assembly as part of the normal build process, you should bind the single or directory-single goal (the assembly goal should ONLY be run from the command line) to a lifecycle phase (package makes sense), something like this:

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-assembly-plugin</artifactId>
      <executions>
        <execution>
          <id>create-my-bundle</id>
          <phase>package</phase>
          <goals>
            <goal>single</goal>
          </goals>
          <configuration>
            <descriptorRefs>
              <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
            ...
          </configuration>
        </execution>
      </executions>
    </plugin>
    

    Adapt the configuration element to suit your needs (for example with the manifest stuff as spoken).

    0 讨论(0)
提交回复
热议问题