开发者

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 f开发者_如何学运维rom the command line:

mvn -Drun.it

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


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


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.


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.


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.


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


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜