ThinJar
大约 2 分钟java
概述
springboot 项目将依赖统一打包到 lib 中,将自己的代码单独打包为 jar 方便上传部署
代码
将下面代码拷贝到项目 pom.xml 中,注意下列需要修改的点:
- 项目名称
- 第一个
plugin中的include自己制作的jar包 - 包含本地
jdk依赖的路径bootclasspath,windows系统不需要变更,如果开发电脑是linux需要修改 - 修改
mainClass项目启动类 - 在
windows系统中运行,要添加启动参数-Dloader.path="D:/projs/showa\/backend/lib",启动脚本完整代码
@echo off
start javaw -jar -Xms512m -Xmx512m -Dfile.encoding=UTF-8 -Dloader.path="D:/projs/showa\/backend/lib" showa.jar --spring.config.location=D:\projs\showa\backend\application.yml
exit
下面是 pom.xml 中的打包相关代码
<build>
<finalName>showa</finalName>
<plugins>
<!--1、编译出不带lib文件夹的Jar包-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!--表示编译版本配置有效-->
<fork>true</fork>
<!--引入第三方jar包时,不添加则引入的第三方jar不会被打入jar包中-->
<includeSystemScope>true</includeSystemScope>
<!--排除第三方jar文件-->
<includes>
<include>
<groupId>nothing</groupId>
<artifactId>nothing</artifactId>
</include>
<include>
<groupId>com.cc</groupId>
<artifactId>cc-alltype</artifactId>
</include>
</includes>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<!--2、完成对Java代码的编译,可以指定项目源码的jdk版本,编译后的jdk版本,以及编码-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<!--源代码使用的JDK版本-->
<source>${java.version}</source>
<!--需要生成的目标class文件的编译版本-->
<target>${java.version}</target>
<!--字符集编码-->
<encoding>UTF-8</encoding>
<!--用来传递编译器自身不包含但是却支持的参数选项-->
<compilerArguments>
<verbose/>
<!--windwos环境(二选一)-->
<bootclasspath>${env.JAVA_HOME}/jre/lib/rt.jar;${env.JAVA_HOME}/jre/lib/jce.jar</bootclasspath>
<!--Linux环境(二选一)
<bootclasspath>${java.home}/lib/rt.jar:${java.home}/lib/jce.jar</bootclasspath>-->
</compilerArguments>
</configuration>
</plugin>
<!--3、将所有依赖的jar文件复制到target/lib目录-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<!--复制到哪个路径,${project.build.directory}缺醒为target,其他内置参数见下面解释-->
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<!--4、指定启动类,指定配置文件,将依赖打成外部jar包-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<!--是否要把第三方jar加入到类构建路径-->
<addClasspath>true</addClasspath>
<!--外部依赖jar包的最终位置-->
<classpathPrefix>lib/</classpathPrefix>
<!--项目启动类-->
<mainClass>com.xdf.showa.ShowaApplication</mainClass>
</manifest>
</archive>
<!--资源文件不打进jar包中,做到配置跟项目分离的效果-->
<excludes>
<!--业务jar中过滤application.properties/yml文件,在jar包外控制
<exclude>*.properties</exclude>
<exclude>*.xml</exclude>
<exclude>*.yml</exclude>-->
</excludes>
</configuration>
</plugin>
</plugins>
</build>
