Most examples for profile activation are quite straightforward, not dealing with more complex scenarios. This may be due to the fact, that Maven profile activation is kept quite simple and there are not much sophisticated scenarios to describe with out-of-the box profile activation.
The proof-of-concept POMs you will find in here are for the following scenario:
-
You have a multi-module project building different applications.
-
There are other modules containing integration and system tests which sometimes test only one application, sometimes they test two or more applications and how they interact with each other.
-
The plugin to run the integration tests has a flag which tells if to run the tests or not (like
maven-surefire-pluginand the propertyskipTests). -
Modifying application "client" you now want to run all tests, which provide sufficient confidence, that you did not break the client. You do so by telling your Maven build, that your application under test (AUT) is "client":
$ mvn verify -Daut=client -
Advanced scenarios:
-
Now you did changes to "client" and "server". Assuming that there are even more applications (not covered by these examples) you want to run all tests in one Maven run, which are required. Suggested Maven call:
$ mvn verify -Daut=client,server -
Sometimes you don't want to specify any AUT but instead run all tests. For example, before merging a pull request. To do so, you just skip specifying any AUT:
$ mvn verify
-
-
client-only-itest:
This is the most simple use case. These tests will run if the only AUT specified is "client".
-
client-or-server-exclusive-itest:
These tests will run if the AUT is either "client" or "server" but not both.
-
client-contained-multi-properties-itest:
This approach will switch test execution on property name rather than property value. Benefit: We can now cover the scenario to run tests, if the module applies to one of several specified AUTs without using an extension.
Caveat: We either end up in huge POMs with many profiles if we have a bunch of AUTs to support (we need to explicitly list all AUTs not covered by this module), or we need an extra property to actually activate selective AUT execution.
In the example we have chosen the second option, as the POMs will be much less verbose. The AUT selection will then look like as given in the examples below:
$ mvn verify -Duse-aut -Daut-client $ mvn verify -Duse-aut -Daut-server $ mvn verify -Duse-aut -Daut-client -Daut-serverWhere only in the second example the tests will not run.
-
client-or-server-contained-multi-properties-itest:
Very similar to the previous approach, but this time tests will run if either
aut-clientoraut-serveris set, or both. It may be interesting to know that the order of properties given does not influence the profile parsing. Instead the order of profiles as given in the POM determines which profile gets checked/activated last. So, executing this module with either-Daut-client -Daut-serveror-Daut-server -Daut-clientwill both cause the debugging propertypropertySourceIdto be set toaut-serveras this is the profile which comes last.Motivation: This approach may be used for example to not only activate or deactivate a complete module depending on a given AUT, but to also select certain tests to run. Based on the
<include>configuration for Maven Surefire Plugin the example POM shows, how such an include string may be built.Note, that a naive approach using
<testIncludes>${testIncludes},**/*ServerTest.java</testIncludes>will fail, because Maven forbids cyclic references in properties. Thus, we have to use several properties which are later merged into one.
As stated before, standard Maven profile activation is rather restricted. That is why we may require to use extensions for profile activation (see Custom Profile Activators).
The Profile Activator Extension by random-maven seems to be the only alive-and-kicking extension in 2019 which provides support for script-based profile activation. Previous approaches seem to be dead since 2016.
-
client-contained-itest:
Based on this extension, these tests are able to run, if "client" is contained in the specified AUTs provided as CSV value. Thus, these tests will run for
aut=clientas well as foraut=client,server.
- Apache Software Foundation: Custom Profile Activators - Maven
- Apache Software Foundation: Maven Surefire Plugin
- Apache Software Foundation: [MNG-3328] Allow multiple profile activation properties.
- Apache Software Foundation: [MNG-6345] Support profile activation via script.
- Baeldung: Guide to Maven Profiles
- GitHub: random-maven/profile-activator-extension: Provide flexible Maven profile activation via script
- Stack Overflow: Activation of maven profile based on multiple properties
- Wikipedia: MVEL