Maven - activate profile based on project property

Hi, I am trying to achieve something like this:

In parent pom i

<profile> <activation> <property> <name>Compile</name> <value>${project.artifactId}</value> ... 

so if I run mvn -DCompile=mod1 install under the parent pom, it will only apply the profile settings to module 1, but not others.

Similarly, if I have

 <profile> <activation> <property> <name>Compile</name> <value>${project.packaging}</value> ... 

then running mvn -DCompile=war install under the parent pom, it will only apply the profile settings to those that will be packaged as war, but not jar or pom.

I tried, but it did not work properly. Did I miss something? Please, help.

PS there is no need to suggest workarounds, since I am only interested in this method. It’s just impossible to answer, if so. thank you

+6
source share
3 answers

This will not work with ...

To start:

And why it won’t work from parent to children:

  • The properties of the parent POM are expanded before your child POM even appears in the picture.

Many went here before you and could not.

You might think what you are really trying to do. Are you building different artifacts from the same source project? You might need a few more Maven modules to take care of them.

+6
source

Yes, you can activate a profile depending on one or more parameters, such as env variables.

 <project> ... <profiles> <profile> <id>development</id> <activation> <property> <name>!environment.type</name> </property> </activation> </profile> </profiles> </project> 

If you try to use different packaging depending on X, you can use the build plugin and do your magic there http://maven.apache.org/plugins/maven-assembly-plugin/

More on activation

+2
source

As with Maven 3.0, profiles in POM can also be activated based on properties from active profiles from settings.xml.

You can check the sample given here:

http://maven.apache.org/guides/introduction/introduction-to-profiles.html

+1
source

Source: https://habr.com/ru/post/954147/


All Articles