maven-assembly打包方式

本文介绍如何使用Maven的assembly插件进行自定义打包,包括配置assembly.xml和pom.xml来控制打包格式、依赖及文件集等内容,并展示了如何通过构建脚本来启动Spring Boot应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

maven-assembly打包方式

assembly.xml

<assembly>
    <id>myAssembly</id>
    <formats>
        <format>tar.gz</format><!--打包的文件格式,也可以有:war, zip-->
    </formats>
    <!--tar.gz压缩包下是否生成和项目名相同的根目录-->
    <includeBaseDirectory>true</includeBaseDirectory>

    <dependencySets>
        <dependencySet>
            <!--是否把本项目添加到依赖文件夹下-->
            <useProjectArtifact>false</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>
            <!--将scope为runtime的依赖包打包-->
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>

    <fileSets>
        <fileSet>
            <directory>src/main/bin</directory>
            <outputDirectory>bin/</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>
    </fileSets>

</assembly>

pom.xml

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.3.1</version>
    <configuration>
        <archive>
            <manifest>
                <!--运行jar包时运行的主类,要求类全名-->
                <mainClass>com.xmmy.AuthCenterApplication</mainClass>
                <!--是否指定项目classpath下的依赖-->
                <addClasspath>true</addClasspath>
                <!--指定依赖的时候声明前缀-->
                <classpathPrefix>lib/</classpathPrefix>
            </manifest>
        </archive>
    </configuration>
</plugin>

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
        <execution><!--配置执行器-->
            <id>make-assembly</id>
            <phase>package</phase><!--绑定到package生命周期阶段上-->
            <goals>
                <goal>single</goal><!--只运行一次-->
            </goals>
            <configuration>
                <!--not append assembly id in release file name-->
                <appendAssemblyId>false</appendAssemblyId>
                <finalName>${project.name}</finalName>
                <descriptor>src/main/assembly/assembly.xml</descriptor><!--配置描述文件路径-->
            </configuration>
        </execution>
    </executions>
</plugin>

startup.sh

#!/bin/sh
JAVA_HOME=/usr/local/java/jdk1.8.0_171 
JAVA=$JAVA_HOME/bin/java

rm -f ecp-yjzh-app-pid

nohup $JAVA -Dfile.encoding=utf-8 -Xms512m -Xmx1024m -jar ecp-app-command-platform.jar -Djava.ext.dirs=$JAVA_HOME/lib > nohup.out 2>&1 &

echo $! > ecp-yjzh-app-pid

stop.sh

#!/bin/sh 

tpid=`cat ecp-yjzh-app-pid | awk '{print $1}'` tpid=`ps -aef | grep $tpid | awk '{print $2}' |grep $tpid`
if [ ${tpid} ]; then
 kill -9 $tpid
fi
rm -f ecp-yjzh-app-pid

Spring Boot 打包分离配置文件

如何只是想在打成jar包的是偶单独把配置文件分离出来,这个其实简单。按照规定,Spring Boot的配置文件加载优先级如下:

​ 1.当前目录下的config子目录

​ 2.当前目录

​ 3.classpath下的config目录

​ 4.classpath根目录

优先级自上而下递减

所以,要实现配置文件分离,只需要在编译出来的jar文件的同级目录创建一个config目录,然后把配置文件复制到改目录即可,运行时,会优先使用config目录下的配置文件

优化

如果只是像上边那样配置,jar文件当中其中还有一份配置文件,只不过加载的时候被优先级更高的config目录的配置文件覆盖了。如果要消除jar文件中的配置文件,可以再pom.xml文件中指定剔除的配置文件,实例如下:

<bulid>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <excludes>
                <exclude>**/*.properties</exclude>
                <exclude>**/*.yml</exclude>
                <exclude>**/*.xml</exclude>
            </excludes>
        </resource>
    </resources>
</build>

这样在打包的时候就把.properties, .yml, .xml几种类型的配置文件都过滤掉了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值