tomcat-maven-plugin with multi module and war overlay - maven

I have a maven war project (call it projectA) that depends on another maven war (call it projectB). They get integrated using an overlay with the maven-war-plugin. Both projects have the same parent and the same aggregator (different from the parent). I can package the final war successfully without any issue. This is the command I'm running to do that (I'm at the aggregator level when running mvn):
mvn -am -pl projectA package
The reactor build order is parent ➡ projectB ➡ projectA and all is fine.
I'm now trying to use the tomcat-maven-plugin in this build. So I added the tomcat-maven-plugin in the parent pom pluginManagement section and used it in the projectA pom. I'm launching tomcat with:
mvn -am -pl projectA tomcat:run
The reactor build order remains the same. But the tomcat execution is run on projectB (which is also a war) and not on projectA. Actually, projectA build is not even reached.
I've tried with both the org.codehaus.mojo.tomcat-maven-plugin v1.1 and org.apache.tomcat.maven.tomcat6-maven-plugin v2.1, I've found the same behavior.
How can I make tomcat run the final projectA?
Note: The project dependencies are in fact a bit more complicated and there are other sub-projects involved (jars), that's why I'm calling maven with -am (also make).

Ok, so I found a solution. It is to disable the maven-tomcat-plugin for the pojectB, this answer is based on this other question.
Linking the plugin to a specific phase
First I had to link the maven-tomcat-plugin to a phase of my build cycle, so that it is called even if I don't call the plugin directly from the CLI, obviously I choose the integration-test phase. I can now run mvn integration-test -pl projectA to have tomcat being launched with the just build war.
This is how the plugin is declared in the parent:
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat6-maven-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<id>run-war</id>
<!-- We link this plugin to some non default phase so that we can disable it in some modules. -->
<phase>integration-test</phase>
<goals>
<goal>run-war</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
and in projectA I just make use of it as declared in the parent:
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat6-maven-plugin</artifactId>
</plugin>
</plugin>
</build>
Deactivating the plugin in projectB
Then, in projectB, I link the same plugin (with the same id) to a null/void/empty phase, which ends up disabling the plugin for this project when built for this integration-test phase:
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat6-maven-plugin</artifactId>
<executions>
<execution>
<id>run-war</id>
<!-- We don't want this plugin to be called from another module build -->
<phase/>
</execution>
</executions>
</plugin>
</plugins>
</build>
With that setup, I can now do a complete build (both projectB, projectA and other dependencies) and launch tomcat with the resulting war in a single run:
mvn -am -pl projectA clean integration-test

Related

How can I make Maven/Surefire run the tests only in the modules specified by the -pl argument

I have a multi-module Maven projects. And I often run maven for a single module (pl) plus its dependencies (-am):
mvn test -pl module -am
This will start running the tests on all the dependencies as well. Is there a way to tell Surefire to only run the tests in the module/s (the one/s specified by the -pl argument, module in the example above) and skip all the tests in all the modules collected by using the -am option.
Notes:
I cannot use test whitelisting using the -Dtest=... because the package do guarantee that no tests from another module will be executed.
If I don't use the -am option, the build will fail because of the dependencies from the same project that will not be found (and since I am in a Dockerized environments, I don't have the luxury of the cached artifacts in the .m2/repository)
I have applied the following sed workaround in my scripts to solve this problem. Basically, I add the <skipTests>true</skipTests> configuration for the maven-surefire-plugin in all modules of my project with the exception (hence the git checkout command) of my current module (denoted below by variable MAVEN_MODULE):
echo "==> Forcibly disabling of tests in all dependency modules"
for pom in */pom.xml
do
sed -i '/<plugins>/a <plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><configuration><skipTests>true</skipTests></configuration></plugin>' $pom
done
git checkout "${MAVEN_MODULE}"
where the sed command adds the following XML fragment in the <plugins> section of each child module pom.xml file:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
I solved this issue with profiles and properties.
parent pom.xml,
properties
skip.test - to skip surefire and/or failsafe plugin.
skip.x.test - x is one of modules, define this for each sub-modules.
For example,
<properties>
<skip.tests>false</skip.tests>
<skip.x.tests>false</skip.x.tests>
<skip.y.tests>false</skip.y.tests>
<skip.z.tests>false</skip.z.tests>
</properties>
plugin configs
Put this element into surefire and/or failsafe plugin execution.
<skipTests>${skip.tests}</skipTests>
For example,
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
<executions>
<execution>
<id>default-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skipTests>${skip.tests}</skipTests>
</configuration>
</execution>
</executions>
</plugin>
profiles
Define a profile to enable tests for only the specified module. You need this for each module.
For example,
module pom.xml
properties
In module x, define this to override the skip.tests property.
<properties>
<skip.tests>${skip.x.tests}</skip.tests>
</properties>
command
maven test -pl x -am -Dtest.x

Maven: build war but deploy a different artifact?

I have a maven project which builds a WAR and then packs it into a zip file which targets a specific deployment tool. I don't want to upload the WAR as it will be a waste of space.
Is it possible to build the WAR but only upload/deploy the zip in the same pom file?
Edit:
No, the "duplicate" question suggested above does not help the slightest. I have to specify <packaging>war</packaging> and even if I don't, as soon as I use the maven war plugin, it's going to make the war as part of the deployment.
And I also want the other artifacts in the build (source, tests, etc.). I just do not need the war.
Here is a suggested approach to deploy the war or the zip, depending on the need:
The default build will still provide a WAR as output
A profile is added to skip the normal Maven Deploy Plugin execution as part of the deploy phase and add a further execution (obviously not skipped) deploying only the ZIP file via the deploy-file goal, as suggested by #Michal, but not via command line (better as part of the build).
Depending on the need you can of course switch the default behavior from the profile to the default build or even get rid of the default build (just the WAR) at all.
An example:
<build>
<finalName>test-war-zip</finalName>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<descriptor>src/assembly/descriptor.xml</descriptor>
</configuration>
<executions>
<execution>
<id>create-zip</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>deploy-zip</id>
<build>
<plugins>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
<configuration>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>deploy-zip</id>
<phase>deploy</phase>
<goals>
<goal>deploy-file</goal>
</goals>
<configuration>
<skip>false</skip>
<file>${project.build.directory}/your.file.here</file>
<repositoryId>your.id.here</repositoryId>
<url>http://something.here</url>
<groupId>${groupId}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${version}</version>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Note the global configuration for the Maven Deploy Plugin and its skip to true. It will effectively skip the deploy goal. A further execution will take care of the ZIP file.
With the approach above, executing the normal build, Maven will keep on deploying the generated WAR, while switching on the profile as following:
mvn clean deploy -Pdeploy-zip
Maven will skip the default execution of the Maven Deploy Plugin (its deploy goal) and execute instead the deploy-file goal during the deploy phase.
As part of the build you will then have:
[INFO] --- maven-deploy-plugin:2.8.2:deploy (default-deploy) # test-war-zip ---
[INFO] Skipping artifact deployment
[INFO]
[INFO] --- maven-deploy-plugin:2.8.2:deploy-file (deploy-zip) # test-war-zip ---
Uploaded: http://the.repository.here (986 B at 3.9 KB/sec)

How to configure FindBugs to run only on one project in a multi-module maven project

I use the findbugs-maven-plugin to check for bugs with maven. My maven project is a multi-module project that roughly looks as follows:
java-module
pom.xml
src/ ...
pom.xml
scala-module
pom.xml
src/ ...
I use Jenkins to build and test the project, and Jenkins runs goal findbugs:findbugs in the top-most directory. Since FindBugs reports many spurious warnings for code that is generated by the Scala compiler, I would like to tell FindBugs not to analyze the code in scala-module. However, when I run findbugs:findbugs in the top-most directory, it always analyzes all classes in java-module and scala-module. How can I tell maven to ignore scala-module as a whole? I know about FindBugs exclude filters but I would to have a configuration option for FindBugs that tells it to simply not analyze the code in a certain submodule.
FindBugs is configured in pom.xml in subdirectory java-module as follows:
<reporting>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>${version.plugin.codehaus.findbugs}</version>
<configuration>
<findbugsXmlOutput>true</findbugsXmlOutput>
<findbugsXmlWithMessages>true</findbugsXmlWithMessages>
<xmlOutput>true</xmlOutput>
</configuration>
</plugin>
</plugins>
</reporting>
Despite the configuration being done only for the java-module, FindBugs will always also analyze scala-module.
Add a configuration the scala-module pom.xml that explicitly instructs findbugs to skip the module, i.e.
<reporting>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</reporting>
Note that Maven often requires you to repeat boilerplate XML for cases like this.
Noahlz's answer did not work for me, but adding the following snippet to the sub-module's POM.xml did the trick.
<properties>
<findbugs.skip>true</findbugs.skip>
</properties>

How to 'mvn jetty:run' from a parent pom?

I have a multi-module project that includes several .war packages. I would like to be able to 'mvn jetty:run' on the parent pom and have each of the sub-modules's .wars deployed on the same embedded jetty instance.
I am able to successfully run 'mvn jetty:run' from each of the the sub-modules, but when I run it on the parent pom it fails and skips the sub-modules.
Trying to run 'mvn jetty:run' from the parent pom results in the following:
[ERROR] Failed to execute goal
org.mortbay.jetty:maven-jetty-plugin:6.1.16:run default-cli) on
project FlashCards_App: Webapp source directory C:\dev\sour
ce_code\FlashCards_App\src\main\webapp does not exist -> [Help 1]
It's true there is no webapp directory on the parent pom.
Here's an excerpt from my pom. The full file can be found here.
<modules>
<module>FlashCards_Domain</module>
<module>FlashCards_GWT</module>
<module>FlashCards_Service</module>
<module>FlashCards_Service_SpringData</module>
<module>FlashCards_Service_Jpa</module>
<module>FlashCards_WebServices</module>
<module>FlashCards_Struts</module>
<module>FlashCards_Test</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>${jetty.version}</version>
</plugin>
</plugins>
</build>
This is basically the same question asked in 2009 in this post. It's been a few years and I'm wondering if there are any other options available now. The previous post proposes two solutions (1) using cargo plugin and (2) building sister wars from a sub-module.
Your best shot is probably to configure the jetty plugin to run multiple webapps. I'm not sure if it would work from your parent pom though, so you might have to use on of your modules as the "launcher" webapp, or create a "dummy webapp" in your parent project.
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.6.v20141205</version>
<configuration>
<scanIntervalSeconds>0</scanIntervalSeconds>
<webApp>
<contextPath>/</contextPath>
</webApp>
<contextHandlers>
<contextHandler implementation="org.eclipse.jetty.maven.plugin.JettyWebAppContext">
<war>${project.basedir}/app1/target/app1.war</war>
<contextPath>/app1</contextPath>
</contextHandler>
<contextHandler implementation="org.eclipse.jetty.maven.plugin.JettyWebAppContext">
<war>${project.basedir}/app2/target/app2.war</war>
<contextPath>/app2</contextPath>
</contextHandler>
</contextHandlers>
<stopPort>9999</stopPort>
<stopKey>STOP</stopKey>
</configuration>
</plugin>
cd parent_module
mvn jetty:run -pl sub_module
cd parent_module
mvn jetty:run -pl sub_module
To complete #jiahut answer:
$ mvn jetty:run --help
(...)
-am,--also-make If project list is specified, also
build projects required by the
list
-amd,--also-make-dependents If project list is specified, also
build projects that depend on
projects on the list
(...)
-pl,--projects <arg> Comma-delimited list of specified
reactor projects to build instead
of all projects. A project can be
specified by [groupId]:artifactId
or by its relative path
Example from Apache Archiva:
mvn jetty:run -pl :archiva-webapp -am

Running antrun during war:exploded

For a Maven build I need to copy some files after the exploded directory has been made with the war plugin. Is it possible to run the antrun plugin during/after the war:exploded goal? If so how would I do this? I've tried:
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>war</phase>
<goals>
<goal>exploded</goal>
</goals>
<configuration>
<tasks>
<echo>Running ant task...</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
And several other variation but can't seem to get it to run.
Idealy I'd like the ant task to run if I do a full war:war too but I'll cross this bridge when I come to it.
There are at least two ways to achieve it:
Using direct invocation of plugins
When calling mvn war:exploded oder mvn war:war, you only call a specific goal of a specific plugin. No other plugin is executed. Executions defined in pom.xml are not relevant. As a consequence, you could only call several plugin goals directly, for example mvn war:exploded antrun:run.
But be careful when building several modules: mvn war:exploded antrun:run runs antrun after the war plugin for each module. Whereas mvn war:exploded; mvn antrun:run runs the war plugin for all modules and then antrun for all modules.
Using lifecycle bindings of plugins
When calling mvn pre-integration-test, you call all phases of the default lifecycle up to pre-integration-test. You could define an war plugin execution for goal "exploded" in phase "package" and an antrun execution for goal "run" in phase "pre-integration-test".
There is no phase "war" in the default lifecycle. So your example above won't work with the default lifecycle. And for a custom lifecycle with custom phases, you need custom plugins.

Resources