6

maven项目打jar包包含所有依赖运行相应主类程序

 2 years ago
source link: https://cfonheart.github.io/2019/02/28/maven%E9%A1%B9%E7%9B%AE%E6%89%93jar%E5%8C%85%E5%8C%85%E5%90%AB%E6%89%80%E6%9C%89%E4%BE%9D%E8%B5%96%E8%BF%90%E8%A1%8C%E7%9B%B8%E5%BA%94%E4%B8%BB%E7%B1%BB%E7%A8%8B%E5%BA%8F/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

maven项目打jar包包含所有依赖运行相应主类程序

2019-02-28

在项目的pom.xml中添加如下配置,可以使得在mvn clean install 打包的jar包包含了所有的依赖包。

否则你得自己手动运行java 的时候添加这些jar包所在的classpath,也就是java -cp

<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>jdbc.BeelineJDBC</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>
</plugins>
</build>

指定主类:直接运行 java -jar xx.jar 的时候,会在这个jar包运行指定的主类,上述的pom.xml中也就是在

<mainClass>jdbc.BeelineJDBC</mainClass>

指定了主类为jdbc包下的BeelineJDBC类。

最后mvn clean install 就能在target中得到自己的jar包了。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK