11

Hi I am trying to achieve something like this:

In a parent pom, I have

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

so that if I run mvn -DCompile=mod1 install under the parent pom, it will only apply the profile settings to module 1, but not the 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 to be packed as war but not jar nor pom.

I tried but it didnt work as expected. Did I miss anything? Please help.

P.S. no need to suggest workarounds as I am only interested on this method. Simply answer it is not possible with reason if thats the case. Thank you

user1589188
  • 5,316
  • 17
  • 67
  • 130
  • 1
    As of Maven 3.9.0, "packaging" may also be used as a property for activation: https://maven.apache.org/guides/introduction/introduction-to-profiles.html – Dan Armstrong Aug 18 '23 at 01:17

3 Answers3

19

It won't work since...

To begin with:

And why it would not work from the parent down to the children:

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

Many have gone here before you and failed.

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

Community
  • 1
  • 1
Sander Verhagen
  • 8,540
  • 4
  • 41
  • 63
  • Are you saying ${project.packaging} is static and nothing like at parent it expands to pom, then at child, it expands to jar, etc.? – user1589188 Sep 19 '13 at 08:37
  • As the question was tagged "java", let's be careful what we mean with "static" :) I mean that the profile activation is only evaluated once and thus on basis of the `${project.packaging}` of the parent, probably `pom`. – Sander Verhagen Sep 19 '13 at 08:43
  • And whats really funny is that, I actually have to use `mvn -DCompile=${project.packaging}`. So it is not expending at all... – user1589188 Sep 19 '13 at 08:51
11

Yes you can activate the profile depending one or variety of parameters like env variables.

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

If you are trying to have different packaging depending on X than you can use the assembly plugin and do your magic there http://maven.apache.org/plugins/maven-assembly-plugin/

More on the activation property

Mite Mitreski
  • 3,596
  • 3
  • 29
  • 39
3

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

You can check the sample given here :

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

avianey
  • 5,545
  • 3
  • 37
  • 60