如何使用Maven创建具有依赖关系的可执行JAR?

问题:

我想将我的项目打包在单个可执行文件JAR中进行分发。
如何使Maven将所有依赖JAR包装到我的JAR中?

回答:

class=”lang-xml prettyprint-override”>

<build>
  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>fully.qualified.MainClass</mainClass>
          </manifest>
        </archive>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
    </plugin>
  </plugins>
</build>

你运行它
class=”lang-xml prettyprint-override”>

mvn clean compile assembly:single

 Compile goal should be added before assembly:single or otherwise the code on your own project is not included.
在评论中查看更多细节。
通常这个目标与自动执行的构建阶段有关。这确保了在执行mvn install或执行部署/发布时构建JAR。
class=”lang-xml prettyprint-override”>

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <archive>
      <manifest>
        <mainClass>fully.qualified.MainClass</mainClass>
      </manifest>
    </archive>
    <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
  </configuration>
  <executions>
    <execution>
      <id>make-assembly</id> <!-- this is used for inheritance merges -->
      <phase>package</phase> <!-- bind to the packaging phase -->
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
</plugin>

 
 
Code问答: http://codewenda.com/topics/python/
Stackoverflow: How can I create an executable JAR with dependencies using Maven?

*转载请注明本文链接以及stackoverflow的英文链接

发表评论

电子邮件地址不会被公开。 必填项已用*标注

+ 81 = 88