简体   繁体   中英

Update war name based on activated maven profile

In my pom, I have two profiles.

  1. test1

  2. test2

Now I want my war name to change based on the activated profile.

Expected Result

When test1 profile is activated, war name should be prefix-test1.war .
When test1 and test2 are activated, war name should be prefix-test1-test2.war .
When no profile is activated, war name should be prefix.war .

My POM file ....

<?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>

Right now if I run command mvn clean install the war name is prefix-null.war . If I run command mvn clean install -P test1,test2 the war name is prefix-test2.war .

The result is different to what expected.

Firstly, to avoid showing null when no profiles, we can provide a default value for the property rp.build.warname , as mentioned in Setting default values for custom Maven 2 properties .

For the case running mvn clean install -P test1,test2 , the war name is prefix-test2.war as the value of rp.build.warname is overridden, you may read How are conflicting properties resolved if multiple profiles are activated for more details. In Order to have multiple values, we can use two properties( rp.build.warname1 , rp.build.warname2 ) instead.

The following pom.xml includes the above changes

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM