Maven command to run activemq before intergation tests - java

I need to run ActiveMQ in my maven clean install, so that it starts up activemq and then runs my tests. I have included the plugin in pom.xml and added the mq config file for configuring mq details. It runs successfully and all test pass when I run activeMq on one console and run maven clean install on others (2 separate commands on 2 separate consoles). But is there a way I can run activemq and clean install both with 1 command on the same console? Basically I wish that when I do mvn clean test install, it should automatically fire-up mq first and then proceed with the tests...
I have tried using commands like mvn activemq:run clean test install or mvn clean test install activemq:run. But it either does the clean install or runs the activemq...
How do I combine these 2 commands(activemq:run and mvn clean test install)?

Attach the maven goal and plugin using the <build><plugins> construct.:
<build>
<plugins>
<plugin>
<groupId>org.apache.activemq.tooling</groupId>
<artifactId>maven-activemq-plugin</artifactId>
<version>...</version>
<executions>
<execution>
<goals>
<goal>run</goal>
</goals>
<phase>process-test-classes</phase> <!-- or other phase before the test phase -->
</execution>
</executions>
</plugin>
</plugins>
</build>

Related

maven-release-plugin goals ignored in submodule in multi-module project

I have a multi-module Maven project. The project is laid out as follows
project/
pom.xml
types/
pom.xml
client/
pom.xml
service/
pom.xml
The types and client modules are built as JARs, service is built as a WAR. I'm using the maven-release-plugin to create new releases of this project. I would like to have the release plugin invoke extra goals when performing the release of the service module.
The release plugin is configured like so in the root pom (nothing special):
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
</plugin>
... and configured like so in the service pom along with the plugin I'm trying to invoke via the <goals> parameter:
<plugin>
<artifactId>maven-release-plugin</artifactId>
<configuration>
<goals>deploy dockerfile:build dockerfile:push</goals>
</configuration>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<configuration>
<repository>12345.ecr.us-east-1.amazonaws.com/project</repository>
</configuration>
</plugin>
The idea is that I'd like to build a Docker image for the service module but it doesn't make sense to build images for other modules in the project. However, when cutting a new release, the goals configuration in the service pom file are never invoked.
Maven version: 3.3.3
maven-release-plugin: 2.5.3
JDK: Oracle 1.8.0u144
The command being used:
mvn -Pstaging -B clean release:clean release:prepare release:perform
I'm not able to share the output from this command.
I've verified that the relevant configurations seem be be applied via
mvn -Pstaging help:effective-pom
My question is: Is what I'm trying to accomplish possible with the release plugin? I haven't found any questions or articles that indicate it's impossible.
With the caveat that I have never used the dockerfile-maven-plugin, try release profiles instead of goals.
Step 1. Edit the release plugin config in project/pom.xml.
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version> <!-- should define in pluginManagement -->
<configuration>
<releaseProfiles>publish-docker</releaseProfiles>
<!-- other plugin config -->
</configuration>
</plugin>
Choose a name for the profile that makes sense. I'll use publish-docker as the name here.
Step 2. Add a profile with that name to service/pom.xml:
<profiles>
<profile>
<id>publish-docker</id>
<build>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<configuration>
<repository>12345.ecr.us-east-1.amazonaws.com/project</repository>
</configuration>
<executions>
<execution>
<id>docker-publish</id>
<phase>deploy</phase> <!-- important -->
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
</plugin>
</build>
</profile>
</profiles>
This profile includes plugin configuration that binds the dockerfile:build and dockerfile:push goals to the deploy phase. The release plugin will enable the publish-docker profile for each module. The profile will only exist in the service module, so that's where it will run.
One other thing I notice. In the release command:
mvn -Pstaging -B clean release:clean release:prepare release:perform
I suspect the -Pstaging part is not actually being applied during the release. The release plugin forks another process for each goal run. To pass the argument to the fork, the arguments parameter is required:
mvn -Pstaging -Darguments="-Pstaging" -B clean release:clean release:prepare release:perform

maven: able to execute sonar goal for "post-integration-test" but not for "site" phase

We have a use-case where we want to do sonar analysis as part of site generation. That is whenever "mvn install site:site" is invoked, we want sonar:sonar to be invoked as well as part of that.
We tried following plugin configuration, but that doesn't work to execute sonar goal as part of site phase (we tried "pre-site" phase too but that did not work as well):
<build>
<plugins>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.3.0.603</version>
<executions>
<execution>
<id>sonar-site</id>
<phase>site</phase>
<goals>
<goal>sonar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<build>
Just for experimenting, when we changed: <phase>site</phase> to <phase>post-integration-test</phase> in the above snippet, sonar goal started getting executed after IT execution.
Is there something wrong in compatibility between sonar-maven-plugin and maven-site-plugin. Will appreciate any input or alternate approach to resolve this.
You bound the SonarQube Maven plugin to a site phase, but you didn't execute that phase. You passed site:site which means execute a site goal of the maven-site-plugin.
You have to execute: mvn install site
The solution with post-integration-test works, because that stage is executed by install phase (more or less).
Read more about Maven Lifecycle Reference.

How to force maven to run build-lifecycle before site-lifecycle?

After spending several hours trying to find out whats the problem I come to the conclusion that I need to know:
How can I force maven to execute the lifecycle phases clean, build and site in exact this order: clean -> build -> site?
Problem / scenario:
I have to multimodule projects with identical moduls:
pSuccess
|-pSuccessClient
|-pSuccessEJB (has pSuccessClient as dependency)
|-pSuccessEAR (has pSuccessEJB as dependency)
and
pFail
|-pFailClient
|-pFailEJB (has pFailClient as dependency)
|-pFailEAR (has pFailEJB as dependency)
Both projects have distribution repositories for snapshots and releases in our central nexus artefact repository but none of them has been deployed to it, meaning the nexus is empty.
When I run mvn clean package site on the pSuccess-project the target directory is deleted (clean-lifecycle), then the modules are all build (build-lifecycle) and finally the reports are generated (site-lifecycle) correctly on the freshly build modules - BUILD SUCCESS! While "debugging" the process I figured out that during the build-lifecycle the needed clientDependency is placed in the lokal maven repository and then used for the EJB, same with the EJB for the EAR module. Working smoothly as expected.
But when I do the same on the pFail-project maven executes the site-lifecycle after the clean-lifecycle and before the build-lifecycle. As you can expect this results in a failed build as maven can't find the needed dependency (Client) for the EJB. This is quite locically as it hasn't been build yet. I can enforce this result every time I run the command. There's not phase of the build-lifecycle be run - no compilation, just nothing. Maven tries to execute the site-lifecycle first. The build is only successfull when I run a mvn clean deploy and then another mvn clean package site, becuase then the artefact is read from the nexus. But again site-lifecycle is executed before build-lifecycle. Note Building the project only via mvn clean package works fine without any problems. All modules are build in the correct order. But when I add the site lifecycle it fails.
I read the maven documentation about lifecycle but I can't figure out why site is run before build. In some questions here on SO I read that plugins, which shall be executed in the same phase, are executed in the order they are listed in the pom.xml. So I checked that too, but the <build> tag is definied before the <reporting> tag.
So why is maven executing the site-lifecycle before the build-lifecycle in one of my project and how can I force maven to execute the lifecycles in the right order: clean -> build -> site ?
P.S: I run all maven commands on command line in windows 7.
edit
I know about the lifecycles and phases, meaning I know what's the differenze between package, install, deploy is - that is not part of the question!
For those who do not believe about the execution order: This is the output when I run mvn clean install site, showing that site-lifecycle is executed before build-lifecycle. It doesn't matter if I run mvn clean package site or mvn clean install site. Again, running single mvn clean package(or install) works fine, but not when I want to generate site too. Then site is executed first.
After digging several more hours I found a solution for my problem:
Both projects use the maven-javadoc-plugin during for reporting
<reporting>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>${maven.javadoc.version}</version>
<!-- DocLint je nach Profil ausschalten (siehe oben) -->
<configuration>
<additionalparam>${javadoc.opts}</additionalparam>
</configuration>
</plugin>
...
</reporting>
In the EJB of the failing project we also use the build-helper-maven-plugin in the generate-sources phase of the build lifecycle.
<build>
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
....
</plugin>
...
</build>
It seems that both plugins collide during the generate-sources phase.
After chaning the maven-javadoc-plugin to use the following ReportSet the site generation works fine
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>${maven.javadoc.version}</version>
<!-- Disable DocLint correspondening to java version -->
<configuration>
<additionalparam>${javadoc.opts}</additionalparam>
</configuration>
<reportSets>
<reportSet>
<reports>
<report>javadoc-no-fork</report>
<report>test-javadoc-no-fork</report>
</reports>
</reportSet>
</reportSets>
</plugin>
I still don't really understand why it collades but at least my project is working.

How to execute maven plugin from command line?

Is it possible to execute a Maven plugin from the command line? I need to run dependency plugin:
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${RPTBIN}/.tools/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<excludeTransitive>true</excludeTransitive>
</configuration>
</execution>
</executions>
Is there any way to execute this plugin just like this plugin executes during Maven build?
You should be able to run it with just mvn dependency:copy-dependencies and just add the relevant configuration parameters with -Dparameter=value, i.e. -DoverWriteReleases=false
Yes that is possible. Maven is a Java tool, so you must have Java installed in order to proceed. Please go through the installation process here.
mvn dependency:copy-dependencies
A sample dependency plugin command. By the way how you have been running the Maven command till now?
You can use below command. It worked for me:
mvn {your groupId}:{your artifactId}:{your version}:{your goal}
But remember this command is good if your plugin main class is extending AbstractMojo.
It will run the execute() method of your plugin main class.
Also, before running this command please run
mvn clean install to build the jar

Generating a maven site including a Cobertura Report

I've got some projects that are already doing site generation via maven, and I want to integrate cobertura reports in them, but no maven goal I seem to run will generate a local preview for me to look at that includes the Cobertura reports in the site. I want to be sure they're generating correctly before I commit the pom changes to the repo and have broken site generated.
Below is what I've added to the maven poms (parent and module), but the site I see when I run mvn site:run does not include the cobertura reports:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<configuration>
<check>
<haltOnFailure>false</haltOnFailure>
<regexes>
<regex>
<pattern>parent-package-name-here.*</pattern>
<branchRate>80</branchRate>
<lineRate>80</lineRate>
</regex>
</regexes>
</check>
<instrumentation>
<includes>
<include>parent-package-name-here/**/*.class</include>
</includes>
</instrumentation>
</configuration>
<executions>
<execution>
<id>clean</id>
<phase>pre-site</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
<execution>
<id>instrument</id>
<phase>site</phase>
<goals>
<goal>instrument</goal>
<goal>cobertura</goal>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
<reporting>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
</plugin>
</plugins>
</reporting>
...
</project>
What maven command should I use to generate the site with cobertura reports? Or, what should I add (additionally) to get the site generation to include the cobertura reports?
Should do:
mvn site
To elaborate, running mvn a:b runs the goal b in plugin a. Saying mvn c means to run the lifecycle phase c, which runs all of the bound goals in all of the phases up to c. As a result, this will trigger a lot more things to happen (such as doing the necessary preparation to produce cobertura reports).
I figured out how to do this.
It seems there are a lot of bugs in the link generation within the maven site generation plugin.
The only way I've found to make maven generate a local copy of the site with working module links is to modify the distributionManagement/site tag to point to some local directory instead of the real-live deploy directory, then use maven site:deploy.
Every attempt to use mvn site:stage generates broken links. Same goes for mvn site:run.
The report links work with mvn site:run / mvn site:stage but the links to modules do not.
mvn site
should do what you are looking for. You configure the plugin to run in the pre-site and site phases of the life cycle but your are then executing the site:run goal not site. We are doing similar things with clover (commercial coverage tool) and mvn site does the trick.
site:stage module links don't work in my experience either for multi module builds but site:deploy does. Try this:
Use a property for the site URL in the parent pom, e.g. ${site.url}. Then call this
mvn clean site site:deploy -Dsite.url=file://`pwd`/target/site-deployed
The pwd is a -nix command that will substitute the current directory. This is because the URL that you use must be absolute.
We use
mvn site-deploy
This builds the site and deploys it (copies it to the place we have configured).
mvn site:site should produce what you are after, in the target directory, there will be a site directory containing all reports linked with an index.html in that directory.

Categories