How to trigger a specific execution of a Maven plugin? - maven

I am trying to figure out whether it is possible to define executions of a Maven plugin in a <pluginManagement> section of a parent pom and pick a specific execution, and only that one, in a child project.
To be more specific, I have several multi-module projects that inherit from our company-wide parent pom. In the <pluginManagement> section of the parent pom, I have several executions of maven-resources-plugin using the copy-resources goal, all bound to phase validate but using different configurations:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<inherited>true</inherited>
<executions>
<execution>
<id>copy-codedeploy</id>
<phase>validate</phase>
...
</execution>
<execution>
<id>copy-settings</id>
<phase>validate</phase>
...
</execution>
<execution>
<id>copy-logback</id>
<phase>validate</phase>
...
</execution>
</executions>
<plugin>
After experimenting many hours on a child project, I would like to:
Avoid having needless executions of copy-logback in all modules, which happens when I define execution copy-logback in the parent POM and has the drawback of creating the destination folder hierarchy in all modules.
Avoid, in case that execution is not defined in the parent POM, defining it in multiple cloned copies in all modules where it is needed.
An example child project has these modules, where the first one is the only module where copy-logback is really needed:
webapp
rest
services
persistence
The best I have arrived at now is to leave common executions in the parent POM, which gives me item #1 in the first list above, but still leaves me cloning the exact same plugin configuration in multiple modules. The configuration that I'm cloning is this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<!-- Copy logback.xml from the parent to the main resources folder, filtering the application name used in log file names. -->
<execution>
<id>copy-logback-file</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.basedir}/src/main/resources</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/../logback-templates</directory>
<filtering>true</filtering>
<includes>
<include>logback.xml</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
This unfortunately diminishes the usefulness of a <pluginManagement> section in the parent POM as the identical configuration cannot be defined only once. I have to define it in each module of our Portfolio that uses logback, which amounts to tens of times.
To solve my duplication problem, I am thinking that maybe, it's possible to define an execution of a plugin that can be selectively triggered where it's needed. Is it possible?

You can add a skip parameter to the configuration of each execution like:
<skip>${copy.logback.skip}</skip>
And then you can choose in each module whether to use or skip the execution by setting the property <copy.logback.skip> to true or false.

Related

Maven multi module and plugin calls

my multi module project has the following structure:
parent
module 1 <-- inherits from parent via <relativePath>../parent/pom.xml</relativePath>
module 2 <-- inherits from parent via <relativePath>../parent/pom.xml</relativePath>
aggregator <-- aggregates modules with <modules><module>
The aggregator calls the following plugin:
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>some path</destFile>
</configuration>
</execution>
</executions>
<configuration>
<append>true</append>
</configuration>
</plugin>
</plugins>
</build>
The plugin is called for the aggregator, but i want to call it for all modules (1 and 2).
I know that i can change the parent pom, but i want to keep it clean (that change is part of a bigger implementation).
Is it possible to configure the plugin in such a way, that its called for all modules (just like being placed in parent pom)?
In short: No.
The only way for a module to get a plugin in a lifecycle not defined in its own POM is by inheriting the configuration from its parent.
You should really consider merging parent and aggregator into a single project, i.e. making the aggegrator also the parent. It is much cleaner that way.

Is it possible to specify a dependency on a particular maven phase?

I am using the maven-docbkx-plugin to generate HTML and PDF output from docbook sources. I have several books, and these link to each other using olinks.
The olink database is generated in one maven phase (generate-resources), and the actual HTML and PDF generation, which looks up this database is executed in a subsequent maven phase (compile).
I have divided the maven project into a multi-module project, as each book has tens of included sub-documents. The docbkx-maven-plugin configuration is all done in the parent, then it is just the top-level docbook source that needs to be specified in the child POM.
But ... this does not work dependency-wise, as each module requires that the generate-resources of every other module has been run before it runs its compile phase, so that it can access the olink database of each of the other books.
Is there a way to do this in maven? Or will I need to re-structure into two maven projects (which will break the modularity of this project considerably, as all of the configuration will need to be declared in each project)?
The structure of the parent POM is:
...
<build>
<plugins>
<pluginManagenent>
<plugin>
<groupId>com.agilejava.docbkx</groupId>
<artifactId>docbkx-maven-plugin</artifactId>
<version>2.0.14</version>
<executions>
<execution>
<id>xrefdb</id>
<phase>generate-resources</phase>
<configuration>
...
</configuration>
<goals>
<goal>generate-html</goal>
</goals>
</execution>
<execution>
<id>html</id>
<phase>compile</phase>
<configuration>
...
</configuration>
<goals>
<goal>generate-html</goal>
</goals>
</execution>
</executions>
</plugin>
</pluginManagement>
</plugins>
</build>
</project>
And the modules:
<project>
...
<build>
<plugins>
<plugin>
<groupId>com.agilejava.docbkx</groupId>
<artifactId>docbkx-maven-plugin</artifactId>
<version>2.0.14</version>
<configuration>
...
</configuration>
</plugin>
</plugins>
</build>
</project>
I've done a bit more research on this, and from what I have read, what I am asking is not possible (but I would be happy to be advised otherwise). I have split my project into two, and given them a common parent from which they can draw their common configuration.
Another way I've solved this problem is to use maven profiles. I perform the first pass of all the modules in the first profile, then perform the second pass in a second profile.
It means the project has to be run twice to build all of its artifacts, but it is much more maintainable than spreading the sources over multiple projects.

Maven issue with buildnumber:hgchangeset properties not propagating to children

I have a multi-module project which uses the buildnumber:hgchangeset plugin to generate changeSet and changeSetDate properties, which then get blatted out into the manifest for each module, like so:
pom.xml (parent):
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>hgchangeset</goal>
</goals>
</execution>
</executions>
</plugin>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<ProjectVestion>${project.version}</ProjectVestion>
<ChangeSet>${changeSet}</ChangeSet>
<ChangeSetDate>${changeSetDate}</ChangeSetDate>
</manifestEntries>
</archive>
</configuration>
</plugin>
After a bit of experimentation, I discovered that having buildnumber:hgchangeset execute for each module contributed a significant amount of time to the overall build time, presumably because it's forking hg.exe every time it needs to get the changeset ID of the local repo.
I then thought it would be a good idea to set the inherited property to false on the buildnumber plugin, in order to only have it run once for the parent. Unfortunantly, doing this causes the changeSet and changeSetDate properties to not be "visible" to the child modules.
My question is: is it possible to set things up in such a way that buildnumber:hgchangeset runs only once, but the properties that it sets become visible to children modules?
I suppose alternatively I could write things out to a property file, and have each module read it back in, but this does not seem like idiomatic Maven to me.
Thanks in advance,

Can I configure multiple plugin executions in pluginManagement, and choose from them in my child POM?

I have 2 common plugin-driven tasks that I want to execute in my projects. Because they're common, I want to move their configuration to the pluginMangement section of a shared parent POM. However, both of the 2 tasks, whilst otherwise completely distinct, use the same plugin. In some of my projects I only want to do 1 of the 2 tasks (I don't always want to run all executions of the plugin).
Is there a way to specify multiple different executions of a plugin, within the pluginManagement section of a parent pom, and choose in my child pom one (and only one) of those executions to actually run? If I configure 2 executions in pluginManagement, it seems that both executions will run.
Note: I think this may, or may not, be a duplicate of question Maven2 - problem with pluginManagement and parent-child relationship, but as the question is nearly 4 screenfuls long (TL;DR), a succinct duplicate might be worthwhile.
You're correct, by default Maven will include all of the executions you have configured. Here's how I've dealt with that situation before.
<pluginManagement>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>some-maven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<id>first-execution</id>
<phase>none</phase>
<goals>
<goal>some-goal</goal>
</goals>
<configuration>
<!-- plugin config to share -->
</configuration>
</execution>
<execution>
<id>second-execution</id>
<phase>none</phase>
<goals>
<goal>other-goal</goal>
</goals>
<configuration>
<!-- plugin config to share -->
</configuration>
</execution>
</executions>
</plugin>
</pluginManagement>
Note, the executions are bound to phase none. In the child, you enable the parts that should execute like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>some-maven-plugin</artifactId>
<executions>
<execution>
<id>first-execution</id> <!-- be sure to use ID from parent -->
<phase>prepare-package</phase> <!-- whatever phase is desired -->
</execution>
<!-- enable other executions here - or don't -->
</executions>
</plugin>
If the child doesn't explicitly bind the execution to a phase, it won't run. This allows you to pick and choose the executions desired.

Configuring Javadoc aggregation in Maven

I'm trying to create an aggregate Javadoc site for all the modules in my project, but I can't seem to configure the plugin in a way that is satisfactory. Mainly, I can't seem to get it to aggregate the javadocs all the while detecting links and excluding certain packages. Essentially, it appears the configuration of the plugin is ignored entirely.
I have a root pom.xml that refers to a bunch of submodules and contains the following configuration:
<modules>
<module>foo</module>
<module>bar</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.maven.apache.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9</version>
<executions>
<execution>
<id>aggregate</id>
<phase>site</phase>
<goals>
<goal>aggregate</goal>
</goals>
<configuration>
<links>
<link>http://docs.oracle.com/javase/6/docs/api</link>
<link>http://static.netty.io/3.5/api</link>
<link>http://google-guice.googlecode.com/git/javadoc</link>
<link>http://docs.guava-libraries.googlecode.com/git-history/release/javadoc</link>
<link>http://fasterxml.github.com/jackson-databind/javadoc/2.0.4</link>
<link>https://developers.google.com/protocol-buffers/docs/reference/java</link>
</links>
<bootclasspath>${sun.boot.class.path}</bootclasspath>
<additionalJOption>-J-Xmx1024m</additionalJOption>
<detectJavaApiLink>true</detectJavaApiLink>
<detectLinks>true</detectLinks>
<excludePackageNames>*.testing.*</excludePackageNames>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
But when I run mvn javadoc:aggregate with this setup, I end up with a javadoc site that has no links to any of the referenced libraries and still includes all the testing classes.
I don't even see the plugin attempting to download the package-list for each declared link source.
On the other hand, generating the javadoc for each individual module works well and as expected.
What am I getting wrong?
Plugin configurations can be placed on two levels; inside the execution tag or outside of it ("global").
When the configuration is inside the execution tag it belongs to that particular execution. In your case you will have to run mvn site for it to execute since it is bound to that phase.
When the mvn javadoc:aggregate command is used it looks for the "global" configuration. In your pom there is no such configuration and thus it uses the default configuration.
Change your plugin configuration to this instead:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9</version>
<configuration>
<links>
<link>http://docs.oracle.com/javase/7/docs/api</link>
<link>http://static.netty.io/3.5/api</link>
<link>http://google-guice.googlecode.com/git/javadoc</link>
<link>http://docs.guava-libraries.googlecode.com/git-history/release/javadoc</link>
<link>http://fasterxml.github.com/jackson-databind/javadoc/2.0.4</link>
<link>https://developers.google.com/protocol-buffers/docs/reference/java</link>
</links>
<bootclasspath>${sun.boot.class.path}</bootclasspath>
<additionalJOption>-J-Xmx1024m</additionalJOption>
<detectJavaApiLink>true</detectJavaApiLink>
<detectLinks>true</detectLinks>
<excludePackageNames>*.testing.*</excludePackageNames>
</configuration>
<executions>
<execution>
<id>aggregate</id>
<phase>site</phase>
<goals>
<goal>aggregate</goal>
</goals>
</execution>
</executions>
</plugin>
You can place a configuration inside the execution part to override and specialize the configuration for that execution.
BTW The <groupId> is wrong in your pom. It should be
<groupId>org.apache.maven.plugins</groupId>
and not
<groupId>org.maven.apache.plugins</groupId>

Resources