Maven打包(瘦身打包部署),不包含第三方依赖jar包
原创
©著作权归作者所有:来自51CTO博客作者Jaemon的原创作品,请联系作者获取转载授权,否则将追究法律责任
方式1
pom 配置
<properties>
<startClass>com.answer.jaemon.Application</startClass>
</properties>
<build>
<plugins>
<!-- 生成不包含依赖jar的可执行jar包 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<!--
<goals>
<goal>repackage</goal>
</goals>
-->
<configuration>
<!-- 不加的话最终包名为: ${artifactId}-${version}.jar, 加了的话最终包名: ${artifactId}-${version}-${classifier}.jar -->
<classifier>execute</classifier>
<!-- 不指定生成路径的话, 默认保存在 ${build.directory} 下 -->
<outputDirectory>
${project.build.directory}/execute
</outputDirectory>
</configuration>
</execution>
</executions>
<configuration>
<finalName>${artifactId}-${version}</finalName>
<layout>ZIP</layout>
<mainClass>${startClass}</mainClass>
<includes>
<include>
<groupId>bframe-parent</groupId>
<artifactId>bframe-aut-dao</artifactId>
</include>
</includes>
</configuration>
</plugin>
<!-- 拷贝依赖的jar包到lib目录 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<!-- ${project.build.directory}是maven变量,内置的,表示target目录,如果不写,将在跟目录下创建/lib -->
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<!-- excludeTransitive:是否不包含间接依赖包,比如我们依赖A,但是A又依赖了B,我们是否也要把B打进去 默认不打-->
<excludeTransitive>false</excludeTransitive>
<!-- 复制的jar文件去掉版本信息 -->
<stripVersion>true</stripVersion>
</configuration>
</execution>
</executions>
</plugin>
<!-- 设置源文件编码方式 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- 解决资源文件的编码问题 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.3</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
打包命令
mvn clean install -Dmaven.test.skip=true
启动jar包服务
java -Dloader.path=./lib -jar execute\jaemon-system-1.0-execute.jar
java -Dloader.path=./lib -jar execute\jaemon-system-1.0-execute.jar --spring.profiles.active=dev
方式2
pom 配置
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<layout>ZIP</layout>
<mainClass>${Start-Class}</mainClass>
</configuration>
</plugin>
</plugins>
</build>
使用 -Dloader.path
时需要在打包的时候增加<layout>ZIP</layout>
, 不指定的话 -Dloader.path 不生效
打包
mvn clean install dependency:tree -DoutputFile=.\target\dependency_tree.txt dependency:copy-dependencies -DoutputDirectory=.\target\lib -DincludeScope=runtime -Dmaven.test.skip=true
# 拷贝工程依赖包到 /data/lib 目录
# mvn dependency:copy-dependencies -DoutputDirectory=/data/lib -DincludeScope=runtime
- dependency:tree: 输出工程jar包依赖关系
- outputFile: 指定输出文件路径
- outputType: 指定输出文件格式,默认text,还支持dot,graphml,tgf
- appendOutput: 追加输出,而不是覆盖
- dependency:copy-dependencies: 拷贝依赖包到 lib 目录
- outputDirectory: 导出目录
- includeScope: 设置依赖级别
- runtime scope gives runtime and compile dependencies,
- compile scope gives compile, provided, and system dependencies,
- test (default) scope gives all dependencies,
- provided scope just gives provided dependencies,
- system scope just gives system dependencies.
启动jar包服务
####################################
# 删除 jar包中 BOOT-INF/lib 目录的jar包
####################################
java -Dloader.path=./target/ib -jar target\jaemon-system-1.0-SNAPSHOT.jar
nohup java -Xms256m -Xmx512m -Dloader.path=./target/lib -jar target\jaemon-system-1.0-SNAPSHOT.jar > ./logs/log.out 2>&1 &
# 后续更新服务器jar包(在没有外部依赖包更新的情况下), 直接执行以下命令打包(注意: 上传jar包前先删除jar把中lib目录下的所有外部依赖包)
mvn clean package -Dmaven.test.skip=true
现有项目瘦身打包
将现有jar包中第三方依赖包解压出来
# 解压jar包到当前目录
jar -xvf demo-0.0.1.jar
# 解压jar包到指令路径libs目录下
unzip demo-0.0.1.jar -d libs/
修改pom.xml配置,编译出不带 lib 文件夹的Jar包
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<layout>ZIP</layout>
<includes>
<include>
<groupId>nothing</groupId>
<artifactId>nothing</artifactId>
</include>
</includes>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
执行编译
# 执行编译, 发现生成的jar包大小明显变小
mvn clean package
运行编译后jar包
# 执行运行命令
java -Dloader.path=./libs -jar demo-0.0.1.jar
扩展
如果想引入内部编译的依赖包可以通过includes属性进行添加
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<layout>ZIP</layout>
<includes>
<include>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
</include>
</includes>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
如果引入内部包过多,也可以通过excludeGroupIds属性去掉不变的依赖包。
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<layout>ZIP</layout>
<excludeGroupIds>
org.redisson,
com.alibaba
</excludeGroupIds>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
参考网址