31

I'm trying to activate a maven profile using a property defined inside pom.xml:

<project>
  [...]
  <properties>
    <run.it>true</run.it>
  </properties>
  [...]
  <profiles>
    <profile>
      <activation>
        <property><name>run.it</name></property>
      </activation>
      [...]
    </profile>
  </profiles>
  [...]
</project>

Apparently it doesn't work. However, activation works from the command line:

mvn -Drun.it

Is it "by design"? If it is, what is a possible workaround?

Romain Linsolas
  • 79,475
  • 49
  • 202
  • 273
yegor256
  • 102,010
  • 123
  • 446
  • 597
  • Here is a workaround: http://stackoverflow.com/a/14386303/290918 – kldavis4 Jan 17 '13 at 21:40
  • I have created a feature request that would add the `` tag from the `settings.xml` to `pom.xml` so that you can define profiles you want to activate. https://issues.apache.org/jira/browse/MNG-6000 – flungo Apr 14 '16 at 15:36

7 Answers7

29

Edit, complete rewrite, as i understand the question now.

See this forum post:

profile activation is based on SYSTEM properties. you cannot activate profiles based on properties defined in your pom you cannot activate profiles based on system properties defined after the build plan has started execution

moritz
  • 5,094
  • 1
  • 26
  • 33
6

What about using an activation like this

    <profile>
        <id>gwt</id>
        <activation>
            <file>
                <exists>uses-gwt.marker</exists>
            </file>
        </activation>

and adding the file 'uses-gwt.marker' into source control, right next to pom.xml. That gives all developers the same state and sort of allows an aspect-oriented pom. we're using this technique in the parent pom and put hte marker files in the childs svn. Not ideal, but works.

Dr. Max Völkel
  • 1,780
  • 2
  • 17
  • 24
5

In recent maven versions (3.5+?) you can create a .mvn folder at the root of your project and a file .mvn/maven.config. In this file you can then activate profiles or set properties as you would do on the command line.

activate a profile through a property:
-Dactivate.myprofile=true
activate a profile directly
-Pmyprofile

Hopefully maven5 will get support for Mixins, which will probably make build settings more reusable.

revau.lt
  • 2,674
  • 2
  • 20
  • 31
  • Thank you, this made my day! I needed to be able to configure a child project to enable a profile from the parent POM that didn't require extraneous args in the command line. – Paulo Sep 16 '19 at 13:36
2

As mentioned in previous answers, profile activation only works with system properties.

But, with a little creativity you can achieve a similar result (conditional plugin execution) using pom properties. To achieve that, use the phase tag of the plugin you want to conditionally execute:

<project>

    ...

    <properties>
        <run.it>none</run.it>
        <!-- <run.it>compile</run.it> -->
        <!-- <run.it>package</run.it> -->
    </properties>

    ...

    <build>

        ...

        <plugins>
            <plugin>
            <artifactId>your-plugin</artifactId>
            <executions>
                <execution>
                    <phase>${run.it}</phase>
                </execution>
            </executions>

            </plugin>
        </plugins>

        ...

    </build>

</project>

The only difference is that you have to use a phase name instead of true/false. But you can change the property or override it freely as a property.

rvcoutinho
  • 326
  • 1
  • 10
2
  • Add a directory ".mvn" to your project root.
  • Add a file ".mvn/jvm.config"
  • Insert "-Drun.it=false" into jvm.config.

Now it should work. You can overwrite the "run.it" property in your pom.xml and it will be picked up for profile activation.

I am not sure if this is a bug or a feature.

griezma
  • 21
  • 2
-2

As Moritz Heuser explained, profile activation is based on system properties. However, you can try something like that:

<project>
  ...
  <properties>
    <run.it>true</run.it>
  </properties>
  ...
  <profiles>
    <profile>
      <activation>
        <activeByDefault>${run.it}</activeByDefault>
      </activation>
      ...
    </profile>
  </profiles>
  ...
</project>

The idea is to define the activeByDefault flag in the <properties>.

Romain Linsolas
  • 79,475
  • 49
  • 202
  • 273
  • 1
    I don't see how this helps. Defining run.it in the pom is too late for the profile to take effect. Per https://jira.codehaus.org/browse/MNG-5235 – Jeanne Boyarsky May 15 '13 at 01:31
  • I agree. There is a classical chicken-and-egg problem: profiles my their nature may define other properties. Should they also activate other profiles recursively? What if profile was already activated but new property definition instructs to deactivate it? – dma_k Jun 06 '13 at 09:57
  • This does not work for the same reason the OP's example doesn't work (as explained by @moritz) – flungo Apr 14 '16 at 15:42
-3

I think your question is similar to this one.

Activation of maven profile based on multiple properties

As mentioned, you can activate a profile and set various properties as per your requirement and use command mvn -Prun-it to set property to true .

<project>
  [...]

  [...]
  <profiles>
    <profile>
    <id>don't-run</id>
<properties>
    <run.it>false</run.it>
  </properties>


      [...]
    </profile>


    <profile>
    <id>run-it</id>
<properties>
    <run.it>true</run.it>
  </properties>

      [...]
    </profile>


  </profiles>
  [...]
</project>
Community
  • 1
  • 1
Prabhjot
  • 695
  • 3
  • 8
  • 21
  • 1
    Kindly review my question. I'm interested to turn the profile on/off from inside `pom.xml`, not from command line or `settings.xml`. – yegor256 Apr 15 '11 at 14:19
  • But how does it make any difference to you, can you elaborate please? Also, as mentioned by Moritz, activation is done based on system property and not by command line. See this reference http://maven.apache.org/guides/introduction/introduction-to-profiles.html – Prabhjot Apr 15 '11 at 14:26