在我的 pom 中,我有两个配置文件。
测试1
测试2
现在我希望我的 war 名称根据激活的配置文件进行更改。
预期结果
激活test1
配置文件时,war名称应为prefix-test1.war
。
当 test1
和 test2
激活时,war 名称应为 prefix-test1-test2.war
。
当激活无配置文件时, war 名称应为prefix.war
。
我的 POM 文件 ....
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.sbill</groupId>
<artifactId>sbill-wrapper</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<packaging>war</packaging>
<artifactId>executable</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<profiles>
<profile>
<id>test1</id>
<properties>
<rp.build.warname>test1</rp.build.warname>
</properties>
</profile>
<profile>
<id>test2</id>
<properties>
<rp.build.warname>test2</rp.build.warname>
</properties>
</profile>
</profiles>
<build>
<finalName>prefix-${rp.build.warname}</finalName>
</build>
</project>
现在,如果我运行命令mvn clean install
,则 war 名称为prefix-null.war
。如果我运行命令mvn clean install -P test1,test2
,则 war 名称为prefix-test2.war
。
结果与预期不同。
最佳答案
首先,为了避免在没有配置文件时显示 null,我们可以为属性 rp.build.warname
提供默认值,如 Setting default values for custom Maven 2 properties 中所述。 .
对于运行mvn clean install -P test1,test2
的情况,war名称为prefix-test2.war
,作为rp.build的值。 warname
被覆盖,您可以阅读 How are conflicting properties resolved if multiple profiles are activated更多细节。
为了拥有多个值,我们可以使用两个属性(rp.build.warname1
、rp.build.warname2
)。
以下 pom.xml 包含上述更改
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.sbill</groupId>
<artifactId>sbill-wrapper</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<packaging>war</packaging>
<artifactId>executable</artifactId>
<!-- Provide default value here to avoid null in file name -->
<properties>
<rp.build.warname1/>
<rp.build.warname2/>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<profiles>
<profile>
<id>test1</id>
<properties>
<rp.build.warname1>-test1</rp.build.warname1>
</properties>
</profile>
<profile>
<id>test2</id>
<properties>
<rp.build.warname2>-test2</rp.build.warname2>
</properties>
</profile>
</profiles>
<build>
<finalName>prefix${rp.build.warname1}${rp.build.warname2}</finalName>
</build>
</project>
关于java - 根据激活的 Maven 配置文件更新 war 名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59132426/