4

We are developing a Maven archetype for applications that only consume web services. This archetype offers three profiles, one for each environment (dev, pre, pro).

The point is that we would like to offer the possibility of having ORM dependencies included optionally (JPA, Hibernate) for those project that may require them in the future. We had though of creating an additional profile containing those dependencies.

When we build our project we use mvn package -Denvironment=dev. Is it possible to specify more than one profile such as: mvn package -Denvironment=dev,orm?

2
  • you don't specify a profile via -D, you specify a profile via -P, yes you can have multiple profiles, -Pprofile1,profile2. In your case a profile was probably activated via property (-D), then you can either have two profiles activated on the same property (still via -D), on two properties (-Dprop1 -Dprop2) or via -P explicitely Commented Mar 2, 2016 at 13:03
  • Take a look at this, it may help you
    – haihui
    Commented Mar 2, 2016 at 13:06

1 Answer 1

8

Yes, this is possible. But it seems you are confused about how profiles are activated in the first place.

The command

mvn package -Denvironment=dev

will not activate any profile without further configuration. In your case, it works because there must be a profile definition in your POM that is activated by the presence of the system property environment with a value of dev. The configuration you have would look like:

<profiles>
  <profile>
    <activation>
      <property>
        <name>environment</name>
        <value>dev</value>
      </property>
    </activation>
  </profile>
</profiles>

This is the magic that makes the profile activates when you pass the system property with -Denvironment. With that in mind, you can activate multiple profiles with the same idea: declare multiple <profile> element that are activated by the presence of a system property.

<profiles>
  <profile>
    <activation>
      <property>
        <name>myAwesomeProperty1</name>
        <value>true</value>
      </property>
    </activation>
  </profile>
  <profile>
    <activation>
      <property>
        <name>myAwesomeProperty2</name>
        <value>true</value>
      </property>
    </activation>
  </profile>
</profiles>

The above configuration would activate both profile if myAwesomeProperty1 and myAwesomeProperty2 is a system property with the value true.

In this particular case though, it seems that what you want is to activate a build depending on your environment so it could perhaps be a better idea to activate the profiles based on the -P command line switch, instead of a system property.

From Introduction to Build Profiles:

Profiles can be explicitly specified using the -P CLI option.

This option takes an argument that is a comma-delimited list of profile-ids to use. When this option is specified, the profile(s) specified in the option argument will be activated in addition to any profiles which are activated by their activation configuration or the <activeProfiles> section in settings.xml.

mvn groupId:artifactId:goal -P profile-1,profile-2

With this solution, you invoke Maven with multiple profile ids. That is to say, if you have in your configuration

<profiles>
  <profile>
    <id>profile-1</id>
    <!-- rest of config -->
  </profile>
  <profile>
    <id>profile-2</id>
    <!-- rest of config -->
  </profile>
</profiles>

The above invocation would activate both profile-1 and profile-2.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.