SpringBoot 打包成jar,并引用外部配置文件

pom:

<build>
<!--  打包后的文件名 –>
<finalName>demo</finalName>
<plugins>
<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>
<!-- 打JAR包 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<!-- 不打包资源文件(配置文件和依赖包分开) excludes 不包含, includes 包含 -->
<excludes>
<exclude>**/*.sql</exclude>
<exclude>**/*.properties</exclude>
<exclude>*.txt</exclude>
</excludes>
<includes>
<include>**/*.*</include>
</includes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<!-- MANIFEST.MF 中 Class-Path 加入前缀 -->
<classpathPrefix>../lib/</classpathPrefix>
<!-- jar包不包含唯一版本标识 -->
<useUniqueVersions>false</useUniqueVersions>
<!--指定入口类 -->
<mainClass>${start-class}</mainClass>
</manifest>
<manifestEntries>
<!--MANIFEST.MF 中 Class-Path 加入资源文件目录 -->
<Class-Path>./</Class-Path>
</manifestEntries>
</archive>
<!-- 输出目录 -->
<outputDirectory>${project.build.directory}/jre/lib/ext/</outputDirectory>
</configuration>
</plugin>

<!-- 该插件的作用是用于复制依赖的jar包到指定的文件夹里 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/jre/lib/lib/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>

<!-- 该插件的作用是用于复制指定的文件 -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution> <!-- 复制配置文件 -->
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>*.txt</include>
</includes>
<!-- <excludes>
<exclude>**/*.xml</exclude>
</excludes> -->
</resource>
</resources>
<outputDirectory>${project.build.directory}/jre/lib/ext/</outputDirectory>
</configuration>
</execution>
<execution> <!-- 复制配置文件 -->
<id>copy-resources-static</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>**/*.properties</exclude>
<exclude>*.txt</exclude>
</excludes>
<!-- <excludes>
<exclude>**/*.xml</exclude>
</excludes> -->
</resource>
</resources>
<outputDirectory>${project.build.directory}/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!-- SpringBoot 打包插件,把 maven-jar-plugin 打成的jar包重新打成可运行jar包 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<!-- 是否跳过Springboot打包 -->
<skip>true</skip>
<includeSystemScope>true</includeSystemScope>
<!--重写包含依赖,包含不存在的依赖,jar里没有pom里的依赖 -->
<includes>
<include>
<groupId>non-exists</groupId>
<artifactId>non-exists</artifactId>
</include>
</includes>
<layout>ZIP</layout>
<mainClass>${start-class}</mainClass>
<!--使用外部配置文件,jar包里没有资源文件 -->
<addResources>true</addResources>
<outputDirectory>${project.build.directory}/jre/lib/lib/</outputDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<!--配置jar包特殊标识 配置后,保留原文件,生成新文件 *-run.jar -->
<!--配置jar包特殊标识 不配置,原文件命名为 *.jar.original,生成新文件 *.jar -->
<!--<classifier>run</classifier> -->
</configuration>
</execution>
</executions>
</plugin>

<plugin> 
<artifactId>maven-assembly-plugin</artifactId> 
<configuration> 
<!-- not append assembly id in release file name --> 
<appendAssemblyId>false</appendAssemblyId> 
<descriptors> 
<!-- 注意这里的路径 打包后输出的最终文件配置 -->
<descriptor>src/main/build/package.xml</descriptor> 
</descriptors> 
</configuration>
<executions> 
<execution> 
<id>make-assembly</id> 
<phase>package</phase> 
<goals> 
<goal>single</goal> 
</goals> 
</execution> 
</executions> 
</plugin> 

<!-- 3、打包过程忽略Junit测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>lib</directory>
<!-- <targetPath>../lib/</targetPath> -->
<includes>
<include>**/*.jar</include>
</includes>
</resource>
<resource>
<directory>src/main/java</directory>
<targetPath>./</targetPath>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
<excludes>
<exclude>**/*.xml</exclude>
</excludes>
<filtering>false</filtering>
</resource>
</resources>
</build>

package.xml

pom:
<build>
<!--  打包后的文件名 –>
<finalName>Data_Analysis</finalName>
<plugins>
<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>
<!-- 打JAR包 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<!-- 不打包资源文件(配置文件和依赖包分开) excludes 不包含, includes 包含 -->
<excludes>
<exclude>**/*.sql</exclude>
<exclude>**/*.properties</exclude>
<exclude>*.txt</exclude>
</excludes>
<includes>
<include>**/*.*</include>
</includes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<!-- MANIFEST.MF 中 Class-Path 加入前缀 -->
<classpathPrefix>../lib/</classpathPrefix>
<!-- jar包不包含唯一版本标识 -->
<useUniqueVersions>false</useUniqueVersions>
<!--指定入口类 -->
<mainClass>${start-class}</mainClass>
</manifest>
<manifestEntries>
<!--MANIFEST.MF 中 Class-Path 加入资源文件目录 -->
<Class-Path>./</Class-Path>
</manifestEntries>
</archive>
<!-- 输出目录 -->
<outputDirectory>${project.build.directory}/jre/lib/ext/</outputDirectory>
</configuration>
</plugin>

<!-- 该插件的作用是用于复制依赖的jar包到指定的文件夹里 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/jre/lib/lib/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>

<!-- 该插件的作用是用于复制指定的文件 -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution> <!-- 复制配置文件 -->
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>*.txt</include>
</includes>
<!-- <excludes>
<exclude>**/*.xml</exclude>
</excludes> -->
</resource>
</resources>
<outputDirectory>${project.build.directory}/jre/lib/ext/</outputDirectory>
</configuration>
</execution>
<execution> <!-- 复制配置文件 -->
<id>copy-resources-static</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>**/*.properties</exclude>
<exclude>*.txt</exclude>
</excludes>
<!-- <excludes>
<exclude>**/*.xml</exclude>
</excludes> -->
</resource>
</resources>
<outputDirectory>${project.build.directory}/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!-- SpringBoot 打包插件,把 maven-jar-plugin 打成的jar包重新打成可运行jar包 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<!-- 是否跳过Springboot打包 -->
<skip>true</skip>
<includeSystemScope>true</includeSystemScope>
<!--重写包含依赖,包含不存在的依赖,jar里没有pom里的依赖 -->
<includes>
<include>
<groupId>non-exists</groupId>
<artifactId>non-exists</artifactId>
</include>
</includes>
<layout>ZIP</layout>
<mainClass>${start-class}</mainClass>
<!--使用外部配置文件,jar包里没有资源文件 -->
<addResources>true</addResources>
<outputDirectory>${project.build.directory}/jre/lib/lib/</outputDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<!--配置jar包特殊标识 配置后,保留原文件,生成新文件 *-run.jar -->
<!--配置jar包特殊标识 不配置,原文件命名为 *.jar.original,生成新文件 *.jar -->
<!--<classifier>run</classifier> -->
</configuration>
</execution>
</executions>
</plugin>

<plugin> 
<artifactId>maven-assembly-plugin</artifactId> 
<configuration> 
<!-- not append assembly id in release file name --> 
<appendAssemblyId>false</appendAssemblyId> 
<descriptors> 
<!-- 注意这里的路径 打包后输出的最终文件配置 -->
<descriptor>src/main/build/package.xml</descriptor> 
</descriptors> 
</configuration>
<executions> 
<execution> 
<id>make-assembly</id> 
<phase>package</phase> 
<goals> 
<goal>single</goal> 
</goals> 
</execution> 
</executions> 
</plugin> 

<!-- 3、打包过程忽略Junit测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>lib</directory>
<!-- <targetPath>../lib/</targetPath> -->
<includes>
<include>**/*.jar</include>
</includes>
</resource>
<resource>
<directory>src/main/java</directory>
<targetPath>./</targetPath>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
<excludes>
<exclude>**/*.xml</exclude>
</excludes>
<filtering>false</filtering>
</resource>
</resources>
</build>
package.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>zip</format>
</formats>
<!-- 改为false不会出现两层相同的目录 -->
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>bin</directory>
<outputDirectory>${file.separator}</outputDirectory>
</fileSet>
<fileSet>
<directory>src/main/resources</directory>
<outputDirectory>${file.separator}</outputDirectory>
<excludes>
<exclude>static/**</exclude>
<exclude>templates/**</exclude>
<exclude>mapper/**</exclude>
<exclude>conf</exclude>
<exclude>sql</exclude>
<exclude>**/*.sql</exclude>
<exclude>**/*.properties</exclude>
<exclude>**/*.txt</exclude>
</excludes>
</fileSet>
<fileSet>
<directory>src/main/resources</directory>
<outputDirectory>${file.separator}jre/lib/ext</outputDirectory>
<includes>
<include>**/*.properties</include>
<include>**/*.txt</include>
<include>**/*.sql</include>
</includes>
</fileSet>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>${file.separator}jre/lib/lib</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
<fileSet>
<directory>target/jre/lib/ext</directory>
<outputDirectory>${file.separator}jre/lib/ext</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>${file.separator}jre/lib/lib</outputDirectory>
<scope>runtime</scope>
<!--<unpack>false</unpack> -->
<excludes>
<!--<exclude>${project.name}-${project.version}</exclude> -->
<exclude>${groupId}:${artifactId}</exclude>
</excludes>
</dependencySet>
</dependencySets>
</assembly>


相关推荐

评论 抢沙发

表情
  1. #1

    来自X市的用户(2019-01-06)
    评论暂不开放 ...

首页上一页12下一页尾页