4-Maven-plugins之打包方式
方式一【推荐】:使用maven-jar-plugin和maven-dependency-plugin
打成的最终jar包中没有所依赖的jar包。依赖跟自己的代码不在一个jar包中。
传说中的"瘦Jar"
<build>
<directory>${project.basedir}/target</directory>
</build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<!-- 配置mainClass和指定classpath -->
<mainClass>com.bocloud.cop.booter.Application</mainClass>
<addClasspath>true</addClasspath>
<useUniqueVersions>false</useUniqueVersions>
<classpathPrefix>libs/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.6.1</version>
<executions>
<execution>
<goals>
<!-- copy-dependencies 是用来拷当前工程的依赖包。 -->
<goal>copy-dependencies</goal>
</goals>
<configuration>
<type>jar</type>
<includeTypes>jar</includeTypes>
<includeScope>runtime</includeScope>
<!-- 把所依赖的jar包copy到指定目录 -->
<outputDirectory>${project.build.directory}/libs</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
方法二【推荐】:使用maven-assembly-plugin
maven-assembly-plugin
可以将所有的东西都打包到一个jar包中。
传说中的"胖jar"执行
mvn package
后,会在target文件夹下生成两个jar包,一个是不带依赖的jar包,一个是后缀有-dependencies
带有依赖的jar包直接执行:
java -jar XXXXX-jar-with-dependencies.jar
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<!-- 配置mainClass和指定classpath -->
<mainClass>com.bocloud.cop.booter.Application</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
方法三:使用maven-shade-plugin
跟
maven-assembly-plugin
类似,都可以将所有的东西都打包到一个jar包中。传说中的"胖jar"
执行
mvn package
后,会在target文件夹下生成两个jar包,一个是不带依赖的jar包,一个是后缀有-shaded
带有依赖的jar包直接执行:
java -jar XXXXXX-jar-with-shaded.jar
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<transformers>
<transformer implementation=
"org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<!-- 配置mainClass和指定classpath -->
<mainClass>com.bocloud.cop.booter.Application</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
方法四【推荐】:使用spring-boot-maven-plugin
可以将所有的东西都打包到一个jar包中。传说中的"胖jar"。
能同时打可执行jar包和war包
两个重点:
goal
要写成repackage
classifier
要写成spring-boot
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>spring-boot</classifier>
<mainClass>com.bocloud.cop.booter.Application</mainClass>
</configuration>
</execution>
</executions>
</plugin>