Paweł Dulęba
Paweł Dulęba

Reputation: 1056

Define different value per profile for same Maven property

I'm facing a problem with maven property per profiles. I have two profiles, each one has the same property 'prop.key' with different values. When I call mvn clean package -PA -PB or mvn clean package -PB -PA both are using the same value 'B-1.0-SNAPSHOT'. I'm using maven 3.0.4.

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

    <groupId>com.test.module</groupId>
    <artifactId>test-module</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <properties>
        <prop.key>UNKNOWN</prop.key>
    </properties>

    <profiles>
        <profile>
            <id>A</id>
            <properties>
                <prop.key>A-${project.version}</prop.key>
            </properties>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <version>1.2</version>
                        <executions>
                            <execution>
                                <id>A</id>
                                <phase>package</phase>
                                <configuration>
                                    <tasks name="a" description="a-desc">
                                        <echo message="RUN A = ${prop.key}" level="info"/>
                                    </tasks>
                                </configuration>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>

        <profile>
            <id>B</id>
            <properties>
                <prop.key>B-${project.version}</prop.key>
            </properties>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <version>1.2</version>
                        <executions>
                            <execution>
                                <id>B</id>
                                <phase>package</phase>
                                <configuration>
                                    <tasks name="b" description="b-desc">
                                        <echo message="RUN B = ${prop.key}" level="info"/>
                                    </tasks>
                                </configuration>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

I have found 'similar topic' but with opposite result! Is it a bug or a feature of maven?

Upvotes: 6

Views: 2391

Answers (3)

Hino
Hino

Reputation: 76

When you run mvn package -PA,B, maven will read both profiles A and B, and the properties from later one in the pom.xml file will take precedence (B is defined below A in pom.xml), result in <prop.key>B-${project.version}</prop.key> will be used.

Upvotes: 0

terafor
terafor

Reputation: 1626

I would think you get this behavior because last parameter for profile gets picked. Try

mvn clean package -PA,B

See Maven introduction to profiles.

Profiles can be explicitly specified using the -P CLI option.

This option takes an argument that is a comma-delimited list of profile-ids to use. When this option is specified, no profiles other than those specified in the option argument will be activated.

Upvotes: 0

Marc von Renteln
Marc von Renteln

Reputation: 1257

You can write mvn package -PA,B for short.

The result is the same: [echo] RUN A = B-1.0-SNAPSHOT and [echo] RUN B = B-1.0-SNAPSHOT

This is the correct behaviour of maven.

One property can only have one specific value per run. You can overwrite a "default value" with a version in a profile. But if you redefine it in two profiles and activate both, one of the profiles "wins".

It is not possible to have one value per profile for one and the same property. Profiles do not have their own variable scope. Properties are always global.

Upvotes: 4

Related Questions