Maven发布配置-根据不同的环境配置生成不同环境的War包

本文介绍了如何使用Maven管理不同环境(开发、测试、生产)的配置,通过配置多个profile,并在POM.xml中定义环境变量,实现快速切换和打包。例如,使用命令`mvn clean install package -Dmaven.test.skip=true -Pdeploytest`可打包集测环境的WAR文件,而`mvn clean install package -Dmaven.test.skip=true -Pprod`则用于生产环境。

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

根据项目的需要,开发,测试,生产各环境的配置是不一样的。如何做到快速切换又方便管理呢。让我们来看下Maven相关的配置


1.项目中配置不同的多套配置

如下统一放置到在env目录下



2.在POM中配置不同的profile信息



<description />
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- 设置默认的打包环境 -->
<package.environment>test</package.environment>
</properties>
<profiles>
<profile>
<id>deploy138</id>
<properties>
<package.environment>192.168.4.138</package.environment>
</properties>
</profile>
<profile>
<id>deploytest</id>
<properties>
<package.environment>test</package.environment>
</properties>
</profile>
<profile>
<id>deploy139</id>
<properties>
<package.environment>192.168.4.139</package.environment>
</properties>
</profile>
<profile>
<id>deploy199</id>
<properties>
<package.environment>192.168.7.199</package.environment>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<package.environment>prod</package.environment>
</properties>
</profile>
</profiles>


3.打包执行命令如下:

集测环境打包:

mvn clean install package -Dmaven.test.skip=true -Pdeploytest

生产环境打包:
mvn clean install package -Dmaven.test.skip=true -Pprod




使用Spring Boot Maven插件可以很方便地根据不同环境使用不同配置文件来打WAR文件。 首先,在项目的资源目录下创建多个不同环境配置文件,如application-dev.properties、application-prod.properties、application-test.properties等。 然后,在pom.xml文件中,为每个环境配置一个profile,指定相应的配置文件。 ``` <profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>application-dev.properties</include> </includes> </resource> </resources> </build> </profile> <profile> <id>prod</id> <build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>application-prod.properties</include> </includes> </resource> </resources> </build> </profile> <profile> <id>test</id> <build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>application-test.properties</include> </includes> </resource> </resources> </build> </profile> </profiles> ``` 接下来,在插件配置中,使用resource元素来指定每个环境配置文件。 ``` <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <executable>true</executable> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> <configuration> <finalName>my-project-${env}</finalName> <appendAssemblyId>false</appendAssemblyId> <descriptors> <descriptor>src/main/assembly/assembly.xml</descriptor> </descriptors> </configuration> </execution> </executions> </plugin> </plugins> </build> ``` 在上面的配置中,使用了一个单独的assembly.xml文件来定义如何打WAR文件。在assembly.xml中,可以根据不同环境配置不同配置文件。 ``` <assembly xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/ASSEMBLY/2.0.0" xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd"> <id>war</id> <formats> <format>war</format> </formats> <fileSets> <fileSet> <directory>${project.basedir}/src/main/resources</directory> <outputDirectory>/</outputDirectory> <includes> <include>application*.properties</include> </includes> </fileSet> </fileSets> </assembly> ``` 最后,使用命令mvn -P dev package可以选择不同的profile进行打生成WAR文件会根据不同环境使用相应的配置文件。例如,使用mvn -P dev package会使用application-dev.properties配置文件进行打生成WAR文件名为my-project-dev.war
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值