Disable maven plugins when using a specific profile

Maven

Maven Problem Overview


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?

Maven Solutions


Solution 1 - Maven

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.

Solution 2 - Maven

  • 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

Solution 3 - Maven

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>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjrharshathView Question on Stackoverflow
Solution 1 - MavenKrzysiekView Answer on Stackoverflow
Solution 2 - MavenAbsurd-MindView Answer on Stackoverflow
Solution 3 - MavengogstadView Answer on Stackoverflow