How to activate profile by means of maven property?

痴心易碎 提交于 2019-12-17 15:55:09

问题


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?


回答1:


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




回答2:


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.




回答3:


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.




回答4:


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




回答5:


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.




回答6:


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




回答7:


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>


来源:https://stackoverflow.com/questions/5676231/how-to-activate-profile-by-means-of-maven-property

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!