普通的maven项目,如何打成一个fat jar(包括了全部依赖jar包)?
1、前言
用过spring boot的同学肯定知道,现在web项目可以直接打成jar包运行,相当方便。
那么普通项目如何配置(非spring boot),才能打成一个类似的jar包呢?
2、解决方案:
在pom中的build中进行如下配置即可:
<plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <archive> <manifest> <!--这里指定要运行的main类--> <mainClass>netty.client.TcpClient809</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> <executions> <execution> <id>make-assembly</id> <!-- 此处指定继承合并 --> <phase>package</phase> <!-- 绑定到打包阶段 --> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins>
3、普通java工程,如何获取所有依赖的jar包
If you need to download the jars instead of using a build system, create a Maven pom file like this with the desired version:
<?xml version="1.0"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.netflix.hystrix.download</groupId> <artifactId>hystrix-download</artifactId> <version>1.0-SNAPSHOT</version> <name>Simple POM to download hystrix-core and dependencies</name> <url>http://github.com/Netflix/Hystrix</url> <dependencies> <dependency> <groupId>com.netflix.hystrix</groupId> <artifactId>hystrix-core</artifactId> <version>x.y.z</version> <scope/> </dependency> </dependencies> </project>
Then execute:
mvn -f download-hystrix-pom.xml dependency:copy-dependencies
It will download hystrix-core-*.jar and its dependencies into ./target/dependency/.
You need Java 6 or later.