springboot-33-使用maven打瘦包

时间:2025-04-03 13:33:49

直接使用maven的插件打包的话, 打出来一个jar, 会非常大, 有时候在服务上传的时候会非常不方便,

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 指定SpringBoot程序的main函数入口类 -->
<mainClass>com.letv.bigdata.idfa.Main</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>

因此需要打出来的jar包不带以来, 依赖防止在lib目录下, 配置文件放在config目录下

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<mainClass>com.letv.bigdata.idfa.Main</mainClass>
<addClasspath>true</addClasspath>
<classpathPrefix>../libs/</classpathPrefix>
</manifest>
<manifestEntries>
<!--<Class-Path>./</Class-Path>-->
<Class-Path>../config</Class-Path>
</manifestEntries>
</archive>
<excludes>
<exclude>application*.yml</exclude> //指定打包时要排除的文件,支持正则
</excludes>
</configuration>
</plugin> <!--拷贝依赖jar到指定的目录-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<!--<version>2.2</version>-->
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.basedir}/libs</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.</version>
<configuration>
<descriptors>
<descriptor>${project.basedir}/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-package</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

使用的assembly放在项目根目录下, 和src一个级别

aseembly.xml

<?xml version="1.0" encoding="UTF-8"?>
<assembly>
<!-- 最终生成的包名称为artifactId-version-id.format,
id主要用来区分包是由哪个assembly文件生成的,本例中
artifactId:test
version:1.0.
id:bin
format:tar.gz
最终将会在target文件夹下生成一个名为test-1.0.-bin.tar.gz的包 -->
<id>bin</id>
<!-- zip, tar, tar.gz, tar.bz2, jar, dir, war 也是支持的 -->
<formats>
<!-- 可以同时打多种格式的包 -->
<format>tar.gz</format>
<!-- <format>zip</format> -->
</formats>
<!-- 默认值是true,此时将创建一个名为artifactId-version的根文件夹,
所有outputDirectory标签指定的路径将变为相对于该文件夹的路径。
例如:outputDirectoryS的值为/test,则最终径为/artifactId-version/test
如果includeBaseDirectory的值是false,则最终路径为/test -->
<includeBaseDirectory>true</includeBaseDirectory> <dependencySets>
<dependencySet>
<!-- true是默认值,本次构建出来的jar包属于当前这个dependencySet,
此时jar包将会被添加至新的tar.gz包中 -->
<useProjectArtifact>true</useProjectArtifact>
<outputDirectory>/libs</outputDirectory>
<unpack>false</unpack>
</dependencySet>
</dependencySets> <fileSets>
<!-- 如果存在fileSet标签,并且在标签中没有使用includes标签指定打入包中的文件,
默认情况下,工程中的所有文件(源文件、编译后产生的.class文件、配置文件等)
都会被打入包中 -->
<fileSet>
<directory>${project.basedir}/src/main/resources</directory>
<outputDirectory>/config</outputDirectory>
<includes>
<include>**/*.*</include>
</includes>
</fileSet>
<!--
<fileSet>
<directory>${project.basedir}/dic</directory>
<outputDirectory>/dic</outputDirectory>
<includes>
<include>*</include>
</includes>
</fileSet>
-->
<fileSet>
<directory>${project.basedir}/script</directory>
<outputDirectory>/bin</outputDirectory>
<includes>
<include>*</include>
</includes>
</fileSet>
<fileSet>
<directory>${project.basedir}/logs</directory>
<outputDirectory>/logs</outputDirectory>
<includes>
<include>*</include>
</includes>
</fileSet> </fileSets> </assembly>

这样打出来的是tar.gz包, 执行脚本放在bin目录下(http://www.cnblogs.com/wenbronk/p/9021542.html), conf目录下放application.yml文件等, lib目录下放所有的jar包