Spring Boot 打包,分离依赖jar,配置文件

maven-jar-plugin,负责将应用程序打包成可执行的jar文件 
maven-assembly-plugin,负责将整个项目按照自定义的目录结构打成最终的压缩包,方便实际部署 

需求1,将依赖的jar提取到可运行的jar文件之外,我们使用maven-jar-plugin来实现 

最终的可运行文件jar文件并不包含依赖的jar包,所有依赖的jar包都放在和ps.jar平行的lib文件夹内,这样如果以后想快速部署,就不用每一次都把体积很大的lib包都要传一遍,除非依赖包有所变化。这里的maven-jar-plugin的配置文件如下 

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-jar-plugin</artifactId>
	<version>2.6</version>
	<configuration>
		<archive>
			<!-- 添加index则不从mainfest中读取classpath,而是从Index.list中读取 -->
<!-- 		<index>true</index> -->
			<manifest>
				<mainClass>com.vmpay.pay.App</mainClass>
					<!-- to create a class path to your dependecies you have to fill true 
								in this field -->
				<addClasspath>true</addClasspath>
				<classpathPrefix>lib/</classpathPrefix>
			</manifest>
			<manifestEntries>
				<Class-Path>./</Class-Path>
			</manifestEntries>
		</archive>
		<excludes>
			<exclude>config/**</exclude>
		</excludes>
	</configuration>
</plugin>

classpath目录是./ 
mainClass配置表示,哪个class作为程序的入口来执行 
addClasspath配置表示,是否将依赖的classpath一起打包 
classpathPrefix配置表示,依赖的classpath的前缀,也就是打包后生成的MANIFEST.MF文件里,引入的jar文件都会加上前缀,lib/,比如fastjson-1.2.7.jar,在mainfest文件里就会是lib/fastjson-1.2.7.jar 
excludes配置表示,排除哪些文件夹不被打包进去 

需求2,定义目录结构

<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>

重点的就是package.xml的路径了,使用maven-assembly-plugin的相关配置实际上都在这个文件里面 
package.xml的文件内容

<?xml version="1.0" encoding="UTF-8"?>
<assembly>
    <id>package</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>true</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>bin</directory>
            <outputDirectory>/</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>src/main/resources</directory>
            <outputDirectory>/</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
        <!-- 把项目的脚本文件目录( src/main/scripts )中的启动脚本文件,打包进zip文件的跟目录 -->
        <fileSet>
            <directory>${project.build.scriptSourceDirectory}</directory>
            <outputDirectory></outputDirectory>
            <includes>
                <include>startup.*</include>
            </includes>
        </fileSet>
    </fileSets>
    <dependencySets>
        <dependencySet>
            <outputDirectory>lib</outputDirectory>
            <scope>runtime</scope>
            <!-- 			<unpack>false</unpack> -->
            <excludes>
                <!-- 				<exclude>${project.name}-${project.version}</exclude> -->
                <exclude>${groupId}:${artifactId}</exclude>
            </excludes>
        </dependencySet>
    </dependencySets>
</assembly>

生成结果

mvn clean package

运行

java -jar hawaii_client_log_kafka-0.0.1-SNAPSHOT.jar --spring.profiles.active=local

修改application-local.yml中的配置,有效

猜你喜欢

转载自my.oschina.net/u/2000675/blog/2872654