springboot jar分离部署
- 注意:spring boot web项目别使用jsp页面,可以使用模板代替,jsp打包时访问页面会报404错误。
1.具体配置如下:
xml
<build>
<plugins>
<!--更换maven的jar打包插件
先前使用的是spring-boot-maven-plugin来打包,这个插件会将项目所有的依赖打入BOOT-INF/lib下,
替换为maven-jar-plugin-->
<!-- 1. springboot应用与jar分离部署配置-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<archive>
<manifest>
<!--需要加入到类构建路径-->
<addClasspath>true</addClasspath>
<!--指定生成的Manifest文件中Class-Path依赖lib前面都加上路径,构建出lib/xx.jar-->
<classpathPrefix>lib/</classpathPrefix>
<!--程序入口--> <mainClass>com.ms.serviceapi.ServiceapiApplication</mainClass>
</manifest>
</archive>
<!-- 3.排除resources配置文件 在jar同级目录增加配置文件-->
<excludes >
<exclude>**/*.properties</exclude>
<exclude>**/*.xml</exclude>
<exclude>**/*.yml</exclude>
</excludes>
</configuration>
</plugin>
<!-- 2.拷贝依赖到jar外面的lib目录-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-lib</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>target/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!--自动化配置实现内容拷贝 可用assembly-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<property name="dist" value="target/publish"></property>
<property name="dist-tmp" value="${dist}/tmp"></property>
<property name="app-name" value="${project.artifactId}-${project.version}"></property>
<copy file="target/${app-name}.jar" tofile="${dist}/${app-name}.jar" />
<delete dir="${dist}/${app-name}-classes.jar" />
<!--web项目启用
<mkdir dir="${dist-tmp}" />
<copy file="target/${app-name}.jar" tofile="${dist-tmp}/${app-name}.jar" />
<unzip src="${dist-tmp}/${app-name}.jar" dest="${dist-tmp}" />
<zip destfile="${dist}/${app-name}-pages.jar">
<zipfileset dir="${dist-tmp}/META-INF" prefix="META-INF" />
<zipfileset dir="target/classes/static" prefix="static" />
<zipfileset dir="target/classes/templates" prefix="templates" />
</zip>-->
<delete dir="${dist-tmp}" />
<move todir="${dist}/lib">
<fileset dir="target/lib" />
</move>
<copy todir="${dist}">
<fileset dir="target/classes">
<include name="**/*.properties" />
<include name="**/*.xml" />
<include name="**/*.yml" />
</fileset>
</copy>
</target>
</configuration>
</execution>
</executions>
</plugin>
<!--属性替换-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<delimiters>
<!-- 指定过滤的表达式
<delimit>$</delimit> : 表示 过滤开头为 $ 结束为 $ 的内容,例如 $project.versionb$
<delimit>${*}</delimit> 表示 过滤${}包裹的内容,例如 ${project.vserion}
-->
<delimit>${*}</delimit>
</delimiters>
</configuration>
</plugin>
<!--4.启动项目 java -jar -Dloader.path=.,lib xx.jar --debug 查看项目日志 -->
</plugins>
</build>
2.打包目录如下:
txt
lib
xxx.yml
xxx-1.0.jar
3.运行:
shell
java -jar -Dloader.path=lib xxx.jar
4.assembly复制打包后的文件
xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<!-- The configuration of the plugin -->
<configuration>
<!-- Specifies the configuration file of the assembly plugin -->
<descriptors>
<descriptor>src/main/resources/assembly/package.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
4.1 package.xml配置文件
xml
<?xml version="1.0" encoding="UTF-8"?>
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>package</id>
<formats>
<format>tar.gz</format>
<format>dir</format>
<format>zip</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<!--依赖jar包以及项目打包文件存储文件-->
<dependencySets>
<dependencySet>
<!--存储在projectName-assembly-version/lib下-->
<outputDirectory>lib</outputDirectory>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>${project.build.directory}/classes</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>**/*.properties</include>
</includes>
</fileSet>
<!-- 把项目自己编译出来的jar文件,打包进zip文件的根目录 -->
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
</fileSets>
<files>
<!--针对单个文件-->
<file>
<!--源文件地址,相对于项目地址-->
<source>pom.xml</source>
<!--输出目录为projectName-assembly-version/-->
<outputDirectory>.</outputDirectory>
<!--文件的权限-->
<fileMode>0755</fileMode>
<!--重命名为-->
<destName>pom.xml</destName>
</file>
</files>
</assembly>