57

I'm looking to find a way of disabling a plugin execution if running with a particular profile.

This is the opposite of running a plugin if a profile is selected.

My use case: My Maven build has a whole load of plugins, but when running on my dev machine, I want to skip some of them. Instead of commenting those plugins out locally, I want to be able just run the build with a "dev" profile. The plugins would continue to run on my continuous build.

Ideas?

Lii
  • 11,553
  • 8
  • 64
  • 88
jrharshath
  • 25,975
  • 33
  • 97
  • 127
  • 3
    There is feature request for a element in all plugins. http://jira.codehaus.org/browse/MNG-3102 – ben75 Jan 23 '13 at 10:10
  • That would involve all plugins having to support this features. I was thinking more in terms of ` dev ` – jrharshath Jan 23 '13 at 10:13
  • Can you elaborate a little bit more what exactly you would like to achieve? Some kind of packaging? Tests etc. ? or What? May be you can give excerpts of your pom file(s)? – khmarbaise Jan 23 '13 at 10:59
  • 2
    in brutal detail: I want to set it up so that when my maven build runs with "risky" profile enabled, I want pmd, checkstyle, findbugs, surefire and failsafe plugins to not run. – jrharshath Jan 23 '13 at 11:01

3 Answers3

117

There is a neat way to disable plugin execution when specific profile is active.

Firstly you need to add an identifier to plugin execution like:

<build>
    <plugins>
        <!-- (...) -->
        <plugin>
            <groupId>nl.geodienstencentrum.maven</groupId>
            <artifactId>sass-maven-plugin</artifactId>
            <version>2.1</version>
            <executions>
                <execution>
                    <id>styles-compilation</id> <!-- plugin execution identifier -->
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>update-stylesheets</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

Then you need to define a profile in which this plugin will NOT be executed:

<profiles>
    <profile>
        <id>no-sass</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>nl.geodienstencentrum.maven</groupId>
                    <artifactId>sass-maven-plugin</artifactId>
                    <version>2.1</version>
                    <executions>
                        <execution>
                            <id>styles-compilation</id> <!-- here there must be the same identifier as defined in <build><plugins> section -->
                            <phase>none</phase> <!-- this disables plugin -->
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

Now if you run standard maven build:

mvn clean package

the sass-maven-plugin will be executed, yet when running:

mvn clean package -P no-sass

the sass-maven-plugin will not be executed.

Krzysiek
  • 7,895
  • 6
  • 37
  • 38
  • Very good answer. This even allows to remove the standard plugins from the life-cycle, such as the `maven-compiler-plugin`. – Michael Piefel Jun 11 '18 at 12:19
  • 3
    I would like to extend this answer. It is worth to mention that: 1) with none you can disable not the whole plugin, but a concrete , 2) It is possible to use a variable like ${enablePhase} and set or not set this variable in different profiles. This way one can run a certain execution conditionally. – Alexander Samoylov Jul 21 '20 at 11:19
24
  • Define your pom so that is has only the plugins you need in dev mode
  • Define a dev profile
  • Define a production profile which contains all plugins you want/need
  • Define the production profile as default

example pom:

<profiles>
  <profile>
    <id>production</id>

    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>

    <build>
      <plugins>
        <!-- 
        <plugin>
          ...
        </plugin>
        -->
      </plugins>
    </build>
    </profile>

    <profile>
      <id>dev</id>
      <!-- Some other logic here, if necessary.
           Otherwise, there's no need for another profile. -->
    </profile>
</profiles>

To run in Dev Mode you can call the following:

mvn -Pdev compile

To run in Production Mode just use the normal steps:

mvn compile

In case you don't want/need to define anything special in your dev profile, you can omit its declaration and call your Dev Mode like this (! disables a profile):

mvn -P!production compile

Be aware: you may need to escape the exclamation mark since it is a special character in bash:

mvn -P\!production compile
carlspring
  • 31,231
  • 29
  • 115
  • 197
Absurd-Mind
  • 7,884
  • 5
  • 35
  • 47
  • Please, add these steps: 1) To run the project with the plugins, just build as usual; 2) To run the project without the plugins, execute `mvn clean package ... -P !production`. Note: The `dev` profile isn't necessary, if it won't contain anything in particular. The `-P !profileName` should be sufficient to exclude the profile. – carlspring Feb 24 '14 at 15:22
  • 1
    @carlspring Could you please help me on why i get this error? `$ mvn -P !production clean -bash: !production: event not found` – Absurd-Mind Feb 24 '14 at 16:25
  • You are right, my bad! I could almost swear I'd done it like that before. :) – carlspring Feb 24 '14 at 16:43
  • You can disable history substitution with: `set +H` – Absurd-Mind Feb 24 '14 at 17:03
  • is it possible to create plugin definition and just "call" it from profile so that we can reuse some xml definitions? – Kalpesh Soni Jun 23 '14 at 22:18
10

Building on Krzysiek's answer, you don't need to define explicit executions, just have a look at the output maven gives you and disable the default executions.

For instance, given the following output from maven:

[INFO] --- maven-resources-plugin:2.7:copy-resources (prepare-dockerfile) @ tilbud ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
...
[INFO]
[INFO] --- maven-resources-plugin:2.7:resources (default-resources) @ tilbud ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
....
[INFO]
[INFO] --- maven-compiler-plugin:3.5.1:compile (default-compile) @ tilbud ---
...
[INFO]
[INFO] --- maven-resources-plugin:2.7:testResources (default-testResources) @ tilbud ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
...
[INFO]
[INFO] --- maven-compiler-plugin:3.5.1:testCompile (default-testCompile) @ tilbud ---
....

The generated default execution names is listed in parenthesis after the plugin and goal. The following profile disables the plugins above:

<profiles>
    <profile>
        <id>packageOnly</id>
        <build>
            <pluginManagement>
                <plugins>
                    <plugin>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>default-compile</id>
                                <phase>none</phase>
                            </execution>
                            <execution>
                                <id>default-testCompile</id>
                                <phase>none</phase>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>default-test</id>
                                <phase>none</phase>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <artifactId>maven-resources-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>default-resources</id>
                                <phase>none</phase>
                            </execution>
                            <execution>
                                <id>default-testResources</id>
                                <phase>none</phase>
                            </execution>
                            <execution>
                                <id>prepare-dockerfile</id>
                                <phase>none</phase>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </pluginManagement>
        </build>
    </profile>
</profiles>
gogstad
  • 3,607
  • 1
  • 29
  • 32
  • It should be mentioned that his works with the default plugins of a lifecycle only, since their bindings are declared in [default-bindings.xml](https://git-wip-us.apache.org/repos/asf?p=maven.git;a=blob;f=maven-core/src/main/resources/META-INF/plexus/default-bindings.xml) and their `default-...` IDs are created by Maven on the fly. `//` declared in a POM cannot be overriden by `////`, since they work the other way round: (specialized) `` overrides (general) ``. – Gerold Broser Feb 14 '19 at 15:13