Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable maven plugins when using a specific profile

Tags:

maven

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?

like image 858
jrharshath Avatar asked Jan 23 '13 09:01

jrharshath


People also ask

How do I run a specific profile in Maven?

Open Maven settings. m2 directory where %USER_HOME% represents the user home directory. If settings. xml file is not there, then create a new one. Add test profile as an active profile using active Profiles node as shown below in example.

Which element activates a profile by default in Maven?

Profiles can be activated in the Maven settings, via the <activeProfiles> section. This section takes a list of <activeProfile> elements, each containing a profile-id inside. Profiles listed in the <activeProfiles> tag would be activated by default every time a project use it.


2 Answers

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.

like image 89
Krzysiek Avatar answered Sep 28 '22 00:09

Krzysiek


  • 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 
like image 25
Absurd-Mind Avatar answered Sep 27 '22 23:09

Absurd-Mind