42

I have some Maven plugins configured in my pom.xml. I only want to execute these plugins if the tests are being run (tests may be skipped using either -Dmaven.test.skip=true or -DskipTests).

One of these plugins is bound to the process-classes build lifecycle phase and the other is bound to the pre-integration-test phase.

Dónal
  • 185,044
  • 174
  • 569
  • 824
  • 1
    I had a similar situation where I wanted to have a plug-in _(tomcat7-maven-plugin)_ available for use in local development, but not referenced in a CI build _(TeamCity, using only approved artifacts from an Artifactory repo)_. I added a `profile` element with `id=localhost-server` as suggested by @tenshi, but excluded the `activation` element. I then added an `activeProfile` element to my `settings.xml` so that it would only be active in my local environment. – Ryan Ransford Apr 08 '13 at 14:13
  • Please see my post with a detailed example. http://stackoverflow.com/questions/7513319/maven-command-line-arguments/33807842#33807842 – Abhishek2k6 Nov 19 '15 at 15:29

2 Answers2

56

You can use profile with special activation conditions like this:

<project>
  ...
  <profiles>
    <profile>
      <id>my-test-plugins</id>

      <activation>
        <property><name>!maven.test.skip</name></property>
        <property><name>!skipTests</name></property>
      </activation>
      <build>
        <plugins>

      <!-- define your plugins here -->

        </plugins>
      </build>
    </profile>
  </profiles>
</project>

More info you can find here:

http://books.sonatype.com/mvnref-book/reference/profiles-sect-activation.html

raffael
  • 2,427
  • 4
  • 26
  • 42
tenshi
  • 26,268
  • 8
  • 76
  • 90
  • Thanks for the link to the Maven book. Really helpful to learn beyond what was asked/answered here. – Marcel Stör Jul 28 '12 at 11:23
  • 5
    How do I use IF conditions without using profile, since they aren't suitable for me? Basically, I have set of profiles for the whole project. For one submodule I need to choose configuration for aspect-j plugin based on the current profile. How can I do that with the proposed model, not sure... – Zorkus Jun 26 '13 at 10:49
  • 4
    Does this actually work? It looks like you can only have one element within . – Marplesoft May 22 '14 at 16:26
  • 8
    this is fine if only 1 profile needs the plugin but what if i need to run the same plugin in 2 different profiles and i dont want to duplicate the code? – Or Gal Feb 18 '15 at 13:53
  • 3
    Seems this answers how to conditionally activate a profile. But that was not the original question. The original question was how to conditionally activate a plugin (one of the plugins in the profile, not all of them) – inor Apr 23 '17 at 15:47
  • Does not work: `Non-parseable POM /path/to/pom.xml: Duplicated tag: 'name'` – ach Aug 14 '20 at 17:23
-3

the last example worked only when I set also value:

<property>
  <name>wsdl2java</name>
  <value>true</value>
</property>
Rizier123
  • 58,877
  • 16
  • 101
  • 156
Rasto
  • 11
  • 1
  • 1
    This is **not** an answer to the original question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). – DavidPostill Feb 03 '15 at 15:52