java - 根据激活的 Maven 配置文件更新 war 名称

标签 java maven war

在我的 pom 中,我有两个配置文件。

  1. 测试1

  2. 测试2

现在我希望我的 war 名称根据激活的配置文件进行更改。

预期结果

激活test1配置文件时,war名称应为prefix-test1.war
test1test2 激活时,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.warname1rp.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/

相关文章:

java - 无法在 Intellij 中导入 Maven 模块

maven war插件将我的资源放在war根目录中

java - 手动编辑tomcat中的文件夹内容?

java - 为 Maven 项目获取 java.lang.ClassNotFoundException : com. google.gson.Gson

maven - 将pcap二进制文件读入HDFS

spring - 如何在 Spring Batch Admin UI 中创建和部署 Spring Batch 作业

java - 如何在 Play 中将所有入站请求转换为 UTF-8!框架

java - 在一个 JOptionPane 消息框中转换一堆 System.out.prints

java - 无法从 javascript 调用支持 bean 方法

java - 读取从 HttpResponse 返回的正文在 java 中不稳定?