- <plugin>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-maven-pluginartifactId>
- <executions>
- <execution>
- <goals>
- <goal>repackagegoal>
- goals>
- <configuration>
- <classifier>spring-bootclassifier>
- <mainClass>
- com.xy.Application
- mainClass>
- configuration>
- execution>
- executions>
- plugin>
执行mvn package后,会在target文件夹下生成两个jar包,一个是不带依赖的jar包,一个是后缀有-shaded带有依赖的jar包。
- <plugin>
- <groupId>org.apache.maven.pluginsgroupId>
- <artifactId>maven-shade-pluginartifactId>
- <version>2.4.1version>
- <executions>
- <execution>
- <phase>packagephase>
- <goals>
- <goal>shadegoal>
- goals>
- <configuration>
- <transformers>
- <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
- <mainClass>com.my.jvm.AssemblyTestmainClass>
- transformer>
- transformers>
- configuration>
- execution>
- executions>
- plugin>
执行mvn package后,会在target文件夹下生成两个jar包,一个是不带依赖的jar包,一个是后缀有-dependencies带有依赖的jar包。
- <plugin>
- <groupId>org.apache.maven.pluginsgroupId>
- <artifactId>maven-assembly-pluginartifactId>
- <configuration>
- <archive>
- <manifest>
- <mainClass>com.my.jvm.AssemblyTestmainClass>
- manifest>
- archive>
- <descriptorRefs>
- <descriptorRef>
- jar-with-dependencies
- descriptorRef>
- descriptorRefs>
- configuration>
- plugin>
最终打出来的jar包,没有其他依赖包,第三方依赖包集中放在 lib文件夹下。
addClasspath:是否在manifest文件中添加classpath。默认为false。如果为true,则会在manifest文件中添加classpath,这样在启动的时候就不用再手动指定classpath了。
- <plugin>
- <artifactId>maven-jar-pluginartifactId>
- <executions>
- <execution>
- <id>make-a-jarid>
- <phase>compilephase>
- <goals>
- <goal>jargoal>
- goals>
- execution>
- executions>
- <configuration>
- <archive>
- <manifest>
- <addClasspath>trueaddClasspath>
- <classpathPrefix>lib/classpathPrefix>
- <mainClass>com.my.jvm.AssemblyTestmainClass>
- manifest>
- archive>
- <excludes>
- <exclude>*.propertiesexclude>
- <exclude>*.txtexclude>
- excludes>
- configuration>
- plugin>
这个插件似乎并不能直接打成可执行jar,它的常见的作用是把依赖集中起来放在lib文件夹下,经常配合maven-jar-plugin或者spring-boot-maven-plugin使用。
- <plugin>
- <groupId>org.apache.maven.pluginsgroupId>
- <artifactId>maven-dependency-pluginartifactId>
- <executions>
- <execution>
- <id>copy-dependenciesid>
- <phase>prepare-packagephase>
- <goals>
- <goal>copy-dependenciesgoal>
- goals>
- <configuration>
- <outputDirectory>
- ${project.build.directory}/libs
- outputDirectory>
- configuration>
- execution>
- executions>
- plugin>
/META-INF/ MANIFEST.MF
普通的jar包:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: Administrator
Created-By: Apache Maven 3.3.3
Build-Jdk: 1.8.0_171
要变成可执行jar包,需要指定main
Main-Class:com.my.jvm.AssemblyTest
如果jar包有依赖其他第三方jar,还要指定Class-Path
Class-Path:lib/log4j-1.2.17.jar lib/slf4j-log4j12-1.7.21.jar lib/slf4
把固定的不会经常变的第三方jar提前放在服务器上,每次只要更新自己的业务代码jar包即可
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-maven-pluginartifactId>
- <configuration>
- <executable>trueexecutable>
-
- <layout>ZIPlayout>
-
- <includes>
- <include>
- <groupId>nothinggroupId>
- <artifactId>nothingartifactId>
- include>
- includes>
- configuration>
- plugin>
-
-
- <plugin>
- <groupId>org.apache.maven.pluginsgroupId>
- <artifactId>maven-dependency-pluginartifactId>
- <executions>
- <execution>
- <id>copyid>
- <phase>packagephase>
- <goals>
- <goal>copy-dependenciesgoal>
- goals>
- <configuration>
-
- <outputDirectory>
- ${project.build.directory}/lib
- outputDirectory>
- configuration>
- execution>
- executions>
- plugin>
- plugins>
- build>
执行需要通过-Dloader.path指定lib的路径:
java -Dloader.path=./lib -jar myProj-1.0.0.jar
如果自己的项目里有自己的依赖需要,而且经常会更新,那就不能放在lib里了,lib里保证是放第三方jar包,不会去动版本的。
那怎么做:spring-boot-maven-plugin 里的includes加需要包即可。
- <!--这里是填写需要包含进去的jar,如果没有则nothing -->
- <includes>
- <include>
- <groupId>com.jincou</groupId>
- <artifactId>xiaoxiao-util</artifactId>
- </include>
- </includes>
缺点:多人项目难管理第三方jar,如果第三方jar包有人要更新,就有可能会忘记去服务器上更新。
普通项目用shade、assembly都行,我喜欢用shade。
springboot项目用spring-boot-maven-plugin。
项目瘦身,提升部署效率,也是一项必须要会的技能。