yegor256
yegor256

Reputation: 105193

How to activate profile by means of maven property?

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?

Upvotes: 32

Views: 47856

Answers (7)

griezma
griezma

Reputation: 21

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

Upvotes: 2

revau.lt
revau.lt

Reputation: 2714

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.

Upvotes: 5

rvcoutinho
rvcoutinho

Reputation: 336

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.

Upvotes: 3

Dr. Max V&#246;lkel
Dr. Max V&#246;lkel

Reputation: 1859

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.

Upvotes: 6

Romain Linsolas
Romain Linsolas

Reputation: 81667

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

Upvotes: -3

Prabhjot
Prabhjot

Reputation: 704

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>

Upvotes: -3

moritz
moritz

Reputation: 5234

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

Upvotes: 29

Related Questions