Run a specific plugin before running tests in maven - maven

I have a requirement, where i need to execute a plugin before running unit tests or system tests. Is there any way i can make sure that my plugin executes before mvn test

Sure. Maven build lifecycle runs in phases. Your plugin can be configure to run in a specific phase.
So you can configure it to run in compile phase or in test phase (just declare it before the maven-surefire-plugin for test phase)
Example of configuring plugin phase:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

Related

Skip maven-cucumber-reporting plugin when integration test is skipped

This is a basic level question about maven plugin lifecycle.
When I run the command mvn clean install -Dskip.integration.tests=false then cucumber report is generated by the following plugin:
<plugin>
<groupId>net.masterthought</groupId>
<artifactId>maven-cucumber-reporting</artifactId>
<executions>
<execution>
<id>execution</id>
<phase>verify</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<projectName>${project.name}</projectName>
<inputDirectory>${project.build.directory}/cucumber</inputDirectory>
<outputDirectory>${project.build.directory}</outputDirectory>
<jsonFiles>
<jsonFile>cucumber.json</jsonFile>
</jsonFiles>
</configuration>
</execution>
</executions>
</plugin>
If I skip the integration tests using the maven command mvn clean install -Dskip.integration.tests=true then the maven build fails with the following error:
Execution execution of goal net.masterthought:maven-cucumber-reporting:5.5.0:generate failed: basedir app/target/cucumber does not exist
To what phase should the plugin be configured so that the maven build does not fail when integration test is skipped?
Add the skip variable to the plugin config whose value is based on whether the integration tests are skipped or not.
<plugin>
<groupId>net.masterthought</groupId>
<artifactId>maven-cucumber-reporting</artifactId>
<executions>
<execution>
<id>execution</id>
<phase>verify</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<skip>${skip.integration.tests}</skip>
<projectName>${project.name}</projectName>
<inputDirectory>${project.build.directory}/cucumber</inputDirectory>
<outputDirectory>${project.build.directory}</outputDirectory>
<jsonFiles>
<jsonFile>cucumber.json</jsonFile>
</jsonFiles>
</configuration>
</execution>
</executions>
</plugin>
in root pom.xml in the properties section add:
<skip.integration.tests>true</skip.integration.tests>
Now the plugin generates cucumber-html-reports folder when you run the maven command mvn clean install -Dskip.integration.tests=false. If you skip integration tests using the maven command mvn clean install -Dskip.integration.tests=true then the cucumber-html-reports folder will not be generated but maven build would still succeed.

How to create a custom maven phase, that will not be run during build?

I would like a plugin to only be run during a new phase. Mainly, I don't want the plugin to run during the build lifecycle.
Something like install:
maven clean build custom:meOnly
GC
Note: I am attempting to call this plug-in:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>runbatchfile</id>
<phase>package</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>deploy.bat</executable>
</configuration>
</plugin>
Plugins consist of goals. If you don't bind a goal to a lifecycle phase, it will not be executed during build. You can, though, start it as plugin:goal from the command line.
There is no need to create an additional phase, a goal will do just fine.

Grails Unit Testing occurring on Wrong Phase of Maven Life Cycle

I'm trying to run unit tests on a Grails app and they happen to run during the verify phase of our maven life cycle as opposed to the test phase (To be clear the maven life cycle goes as: "clean"->"validate"->"compile"->"test"->"package"->"verify"->"install"->"site"->"deploy"). Is there any way to re-associate the plugins responsible for testing (i.e. grails:test-app) to the "test" phase?
If you run grails tests from execution plugin, you can specify phase:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<executions>
<execution>
<id>Unit-test-app</id>
<phase>test</phase>
<configuration>
<executable>grails</executable>
<commandlineArgs>test-app -unit --non-interactive </commandlineArgs>
</configuration>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Maven tell plugin to not run during phase

Is there any phase I can use to prevent Maven from running a plugin goal or any other way I can tell Maven to skip a plugin goal?
Basically, I want to just run it manually.
<groupId>com.googlecode.maven-download-plugin</groupId>
<artifactId>download-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<phase >pre-integration-test</phase>
<goals>
<goal>wget</goal>
</goals>
</execution>
</executions>
<configuration>
<outputDirectory>${project.build.directory}/output</outputDirectory>
<url>http://some.url</url>
<outputFileName>filename</outputFileName>
</configuration>
</plugin>
When I use pre-integration-test it runs during mvn install. However, I just want to run it manually.
Phase none worked for my scenario to keep test-jars from being created for specific modules in my multi-module maven project. Like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>test-jar</id>
<phase>none</phase>
</execution>
</executions>
</plugin>

How does maven deploy snapshot without dependencies with maven-assembly-plugin

I am using mvn release-plugin and assembly-plugin to deploy jar with dependency. It works fine, when I use it with mvn release. It creates two files: normal XXX.jar and XXX.jar-with-dependencies.jar and deploys them both.
But I need also deploy snapshot to another repository by using mvn deploy. In this repository I only need the XXX.jar without dependencies.
So I hope that I could use mvn deploy to deploy snapshot version without dependencies with followed setting.
POM.xml setting:
...
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.2</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>install</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
...
PS: mvn clean install deploy will be called by jenkins scm pulling schedule every morning.
Artifacts produced by maven-assmbly-plugin are automatically attached to the project and hence are deployed when you call mvn deploy.
What you can do is to define a profile (say 'with-dependencies') where you put the assembly plugin execution. In this case if you call mvn deploy it will build a -SNAPSHOT version and push it to the snapshot repository and for the release you will have to call mvn release:prepare release:perform -Pwith-dependencies
<profile>
<id>with-dependencies</id>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.2</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>install</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</profile>

Resources