How to include plugin in maven for test build only? - maven

I want to add the plugin in the maven project. I want it to be added to build only when I build the project for testing purpose.
I found that <scope> can be used for dependency as shown below
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback</artifactId>
<version>0.5</version>
<scope>test</scope>
</dependency>
As you can see here <scope>test</scope>is used.
I want similar thing for plugin. For example this is my plugin code snipplet
<build>
<plugins>
<plugin>
<groupId>org.jibx</groupId>
<artifactId>jibx-maven-plugin</artifactId>
<version>1.2.4</version>
<executions>
<execution>
<goals>
<goal>bind</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

You can create a profile which you activate in your test builds:
<profiles>
<profile>
<id>testing</id>
<build>
<plugins>
<plugin>
<groupId>org.jibx</groupId>
<artifactId>jibx-maven-plugin</artifactId>
<version>1.2.4</version>
<executions>
<execution>
<goals>
<goal>bind</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profile>
Then you invoke mvn with the parameter -P testing to activate your profile:
mvn test -P testing
In addition to activating a profile manually, profiles can also be activated automatically based on conditions such as the existence of a environment variable or a specific file. You can find more information on this in the Maven introduction on profiles.

You can bind a plugin via executions to a phase:
<build>
<plugins>
<plugin>
<groupId>org.jibx</groupId>
<artifactId>jibx-maven-plugin</artifactId>
<version>1.2.4</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>bind</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
The problem I see that usually this plugin runs in process-classes phase which has been done on purpose by the plugin authors..So I don't the request to use it only in the test phase...

Related

Perform deployment of an application on multiple servers through the same Jenkins Job

I'm using Jenkins CI to automate the deployment of my Java application, and I'm currently using the following build configurations on the pom.xml file:
<!-- teste1 config -->
<deploy.jboss.teste1.host>192.168.0.1</deploy.jboss.teste1.host>
<deploy.jboss.teste1.port>9999</deploy.jboss.teste1.port>
<deploy.jboss.teste1.user>admin</deploy.jboss.teste1.user>
<deploy.jboss.teste1.password>admin</deploy.jboss.teste1.password>
<liquibase.teste1.database>db_01</liquibase.teste1.database>
<liquibase.teste1.host>192.168.0.2</liquibase.teste1.host>
<liquibase.teste1.user>admin</liquibase.teste1.user>
<liquibase.teste1.password>admin</liquibase.teste1.password>
<!-- teste2 config -->
<deploy.jboss.teste2.host>192.168.0.3</deploy.jboss.teste2.host>
<deploy.jboss.teste2.port>9999</deploy.jboss.teste2.port>
<deploy.jboss.teste2.user>admin</deploy.jboss.teste2.user>
<deploy.jboss.teste2.password>admin</deploy.jboss.teste2.password>
<liquibase.teste2.database>db_02</liquibase.teste2.database>
<liquibase.teste2.host>192.168.0.4</liquibase.teste2.host>
<liquibase.teste2.user>admin</liquibase.teste2.user>
<liquibase.teste2.password>admin</liquibase.teste2.password>
<profile>
<id>teste1</id>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.jboss.as.plugins</groupId>
<artifactId>jboss-as-maven-plugin</artifactId>
<version>7.7.Final</version>
<configuration>
<hostname>${deploy.jboss.teste1.host}</hostname>
<port>${deploy.jboss.teste1.port}</port>
<username>${deploy.jboss.teste1.user}</username>
<password>${deploy.jboss.teste1.password}</password>
<name>${backend.deployment-name}</name>
<filename>${project.build.finalName}.war</filename>
<skip>${skipDeployment}</skip>
</configuration>
<executions>
<execution>
<id>deploy-jar</id>
<phase>install</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>3.4.1</version>
<configuration>
<changeLogFile>${liquibase.changelog.file}</changeLogFile>
<diffChangeLogFile>${liquibase.changelog.file}</diffChangeLogFile>
<promptOnNonLocalDatabase>false</promptOnNonLocalDatabase>
<driver>com.mysql.jdbc.Driver</driver>
<url>jdbc:mysql://${liquibase.teste1.host}:3306/${liquibase.teste1.database}?zeroDateTimeBehavior=convertToNull</url>
<username>${liquibase.teste1.user}</username>
<password>${liquibase.teste1.password}</password>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>update</id>
<phase>deploy</phase>
<goals>
<goal>update</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>teste2</id>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.jboss.as.plugins</groupId>
<artifactId>jboss-as-maven-plugin</artifactId>
<version>7.7.Final</version>
<configuration>
<hostname>${deploy.jboss.teste2.host}</hostname>
<port>${deploy.jboss.teste2.port}</port>
<username>${deploy.jboss.teste2.user}</username>
<password>${deploy.jboss.teste2.password}</password>
<name>${backend.deployment-name}</name>
<filename>${project.build.finalName}.war</filename>
<skip>${skipDeployment}</skip>
</configuration>
<executions>
<execution>
<id>deploy-jar</id>
<phase>install</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>3.4.1</version>
<configuration>
<changeLogFile>${liquibase.changelog.file}</changeLogFile>
<diffChangeLogFile>${liquibase.changelog.file}</diffChangeLogFile>
<promptOnNonLocalDatabase>false</promptOnNonLocalDatabase>
<driver>com.mysql.jdbc.Driver</driver>
<url>jdbc:mysql://${liquibase.teste2.host}:3306/${liquibase.teste2.database}?zeroDateTimeBehavior=convertToNull</url>
<username>${liquibase.teste2.user}</username>
<password>${liquibase.teste2.password}</password>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>update</id>
<phase>deploy</phase>
<goals>
<goal>update</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
And in Jenkins I created two Jobs, one for each profile, and in each Job the following maven command is executed respectively:
clean package jboss-as:deploy javadoc:javadoc liquibase:update -P teste1
clean package jboss-as:deploy javadoc:javadoc liquibase:update -P teste2
But I would like to deploy the application on two servers at the same time using the same Job, but so far I have not been able to figure out the method to perform such a task, I already tried the following commands but without success:
clean package jboss-as:deploy javadoc:javadoc liquibase:update -P teste1,teste2
clean package jboss-as:deploy javadoc:javadoc liquibase:update -P teste1 -P teste2
clean package jboss-as:deploy javadoc:javadoc liquibase:update -P teste1;clean package jboss-as:deploy javadoc:javadoc liquibase:update -P teste2
In all cases the deployment is done only in one server. Every help is welcome. Thanks.
EDIT:
To accomplish the task I wish I ended up using the "Post Steps" instructions as shown in the image below. It was not the most elegant solution but worked as expected. If someone has a better solution please share.

Run Maven goal only in parent POM by activation

I am working on integrating a plugin into a multi-module project.
I am using a 3rd party plugin that essentially needs to only by run from the parent project (based on my understanding and usage of it). I tried to accomplish this by using a profile, like so:
<profiles>
<profile>
<id>run-my-guy</id>
<build>
<plugins>
<plugin>
<groupId>com.myproject</groupId>
<artifactId>myproject-maven-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>runThing</goal>
</goals>
<inherited>false</inherited>
</execution>
</executions>
<inherited>false</inherited>
</plugin>
</plugins>
</build>
</profile>
</profiles>
I have several <inherited>false</inherited>, but if I run mvn help:all-profiles I can still see this profile in every single module. If I run my mvn package -P run-my-guy I see this get executed in every single subproject. I want the ability to activate this and I do not want it to be on by default.
If I try to add it the <build> section, like this:
<build>
<plugins>
<plugin>
<groupId>com.myproject</groupId>
<artifactId>myproject-maven-plugin</artifactId>
<inherited>false</inherited>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>runThing</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Here, I also have a few <inherited>false</inherited>, just to try and enforce that the plugin and the execution are not inherited. However, whenI run the package phase, or anything that includes that phase, the runThing goal is included.
How do I run a goal only by activation (like profile or some other feature, or just by explicitly running the goal) and only in the parent?
As shown in an answer for "Run a single Maven plugin execution?", it is now possible (since Maven 3.3.1) to specify an execution Id for a direct goal invocation.
pom.xml
<build>
<plugins>
<plugin>
<groupId>com.myproject</groupId>
<artifactId>myproject-maven-plugin</artifactId>
<inherited>false</inherited>
<executions>
<id>myproject-exec-id</id> <!-- note the execution Id -->
<execution>
<phase>none</phase>
<goals>
<goal>runThing</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
And then invoking the goal from the command line uses the optional #executionId parameter:
mvn myproject:runThing#myproject-exec-id

Configuration for maven and flyway in multiple environments, plus integration tests

I have a multi-module Maven project and am using Flyway for db migration. Currently, I have this snippet in my pom.xml:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sql-maven-plugin</artifactId>
<version>1.5</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-java.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>drop-db-before-test-if-any</id>
<phase>process-test-resources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<driver>${datasource.driver}</driver>
<url>${datasource.url}</url>
<username>${datasource.user}</username>
<password>${dbPass}</password>
<autocommit>true</autocommit>
<srcFiles>
<srcFile>${main.basedir}/db/test/drop_create_database_test.sql</srcFile>
</srcFiles>
</configuration>
</execution>
</executions>
</plugin>
How can I convert this to perform the equivalent of the following, such that it will use the flyway migration sql to better mimic production (in test currently it uses a db-test.properties where jpa.generate_ddl=true so that tables are auto-created from the JPA):
mvn flyway:clean -P test
mvn flyway:init -Dflyway.initVersion=1 -Dflyway.initDescription="Initial Version" -P test
mvn flyway:migrate -P test
Thanks!
If I understood your question correctly then this is what you need.
This assumes that you have baselineOnMigrate=true in the plugin configuration.
<profiles>
<profile>
<id>test</id>
<build>
<plugins>
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<configuration>
<driver>${datasource.driver}</driver>
<url>${datasource.url}</url>
<user>${datasource.username}</user>
<password>${datasource.password}</password>
</configuration>
<executions>
<execution>
<id>flyway-clean-database</id>
<phase>process-test-resources</phase>
<goals>
<goal>clean</goal>
<goal>migrate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>

Use dependency command line parameters with maven build

I am using findbugs-maven-plugin in the verify phase of the maven life cycle. i.e. it runs on mvn clean install. This is the code I have in my parent pom.xml (in a multi-module project).
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>findbugs</id>
<phase>verify</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<configuration>
<findbugsXmlOutputDirectory>target/findbugs</findbugsXmlOutputDirectory>
<failOnError>false</failOnError>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>xml-maven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>transform</goal>
</goals>
</execution>
</executions>
<configuration>
<transformationSets>
<transformationSet>
<dir>target/findbugs</dir>
<outputDir>target/findbugs</outputDir>
<stylesheet>plain.xsl</stylesheet>
<fileMappers>
<fileMapper implementation="org.codehaus.plexus.components.io.filemappers.FileExtensionMapper">
<targetExtension>.html</targetExtension>
</fileMapper>
</fileMappers>
</transformationSet>
</transformationSets>
</configuration>
<dependencies>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>findbugs</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
</plugin>
This is working fine and html files are being generated in each module target. However I want to take this a step further by being able to use parameters allowed by findbugs during the maven build (for example onlyAnalyze). I do not want to add configuration in the pom.xml.
I want the build process to remain the same unless I specify by some command that I want to analyze only one class, for example by running:
mvn clean install -Dfindbugs:onlyAnalyze=MyClass
Do you know of a way I can do this?
This is how you can call a standalone goal:
plugin-prefix:goal or groupId:artifactId:version:goal to ensure the right version.
In your case: findbugs:findbugs
With -Dkey=value you can set plugin parameters if they are exposed. http://mojo.codehaus.org/findbugs-maven-plugin/findbugs-mojo.html doesn't show that option. Just to compare: http://mojo.codehaus.org/findbugs-maven-plugin/help-mojo.html does have such options. Here it is still called Expression with ${key}, nowadays it's generated as User property with just key.
If you want onlyAnalyze to be set from commandline, either ask the mojo-team to fix that, or do the following:
<project>
<properties>
<findbugs.onlyAnalyze>false</findbugs.onlyAnalyze> <!-- default value -->
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<configuration>
<onlyAnalyze>${findbugs.onlyAnalyze}</onlyAnalyze>
</configuration>
</plugins>
</build>
</project>
Now you can call mvn findbugs:findbugs -Dfindbugs.onlyAnalyze=true

Maven how to invoke a plugin goal?

In the Tomcat Maven plugin, tomcat7-maven-plugin, how to invoke the goal, tomcat7:deploy, after package phase ? can you please give me concise sample pom file ?
Thanks.
Add an execution for the plugin and tie it to a phase after the package phase, i.e. verify or install..
<build>
<plugins>
<plugin>
<dependency>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>deploy</id>
<phase>install</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Resources