2012-12-13

Maven: Profile activation and properties

I was wondering whether you can use both of these scenarios with the same property:

  1. Activate the profile using -P and set the property in the profile
  2. Activate the profile using -D + <activation>.

„Transitive“ activation: Does not work.

By transitive activation, I mean that one profile sets a property which activates the other profile. That does not work.

<profiles>
  <profile>
    <id>foo</id>

    <properties>
      <foo>BBBBBB</foo>
      <foo2>true</foo2>
    </properties>

    <build>
      <plugins>
        <plugin> <artifactId>maven-antrun-plugin</artifactId>
          <executions> <execution> <id>E1</id> <phase>initialize</phase>  <goals><goal>run</goal></goals> <configuration> <tasks>
            <echo>  *** foo:  ${foo} ***  </echo>
          </tasks> </configuration> </execution> </executions>
        </plugin>
      </plugins>
    </build>

  </profile>


  <profile>
    <id>foo2</id>
    <activation><property> <name>foo2</name> </property> </activation>

    <build>
      <plugins>
        <plugin> <artifactId>maven-antrun-plugin</artifactId>
          <executions> <execution> <id>E2</id> <phase>initialize</phase>  <goals><goal>run</goal></goals> <configuration> <tasks>
            <echo>  *** FOO2 activated too! ***  </echo>
          </tasks> </configuration> </execution> </executions>
        </plugin>
      </plugins>
    </build>

  </profile>
</profiles>

Two-way activation: Works.

A couple of details:

  • The <activation> by property is not triggered when the property is not set on the command line –

thus, setting it in `<properties> does not activate the profile.

  • When you set the prop on cmdline, it activates the profile; however, it's <properties> does not override the value of the prop.
<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>cz.dynawest.maven.playground</groupId>
  <artifactId>props</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <name>Maven Playground - props</name>

  <properties>
    <foo>AAAAA</foo>
  </properties>

  <profiles>
    <profile>
      <id>foo</id>
      <activation>
        <property>
          <name>foo</name>
        </property>
      </activation>

      <properties>
        <foo>BBBBBB</foo>
      </properties>

      <build>
        <plugins>
          <plugin> <artifactId>maven-antrun-plugin</artifactId>
            <executions> <execution> <phase>initialize</phase>  <goals><goal>run</goal></goals> <configuration> <tasks>
              <echo>  *** foo:  ${foo} ***  </echo>
            </tasks> </configuration> </execution> </executions>
          </plugin>
        </plugins>
      </build>

    </profile>
  </profiles>

</project>
ondra@ondra-redhat:~/work-unrelated/MavenPlayground/props$ mvn test -Dfoo
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Playground - props
[INFO]    task-segment: [test]
[INFO] ------------------------------------------------------------------------
[INFO] [antrun:run {execution: default}]
[INFO] Executing tasks
     [echo] *** foo:  true ***
[INFO] Executed tasks
ondra@ondra-redhat:~/work-unrelated/MavenPlayground/props$ mvn test -Pfoo
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Playground - props
[INFO]    task-segment: [test]
[INFO] ------------------------------------------------------------------------
[INFO] [antrun:run {execution: default}]
[INFO] Executing tasks
     [echo] *** foo:  BBBBBB ***
[INFO] Executed tasks
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Playground - props
[INFO]    task-segment: [test]
[INFO] ------------------------------------------------------------------------
[INFO] No goals needed for project - skipping

0