views:

137

answers:

2

This is my parent pom.xml (part of it) in a multi-module project:

...
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-checkstyle-plugin</artifactId>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>check</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
…

This configuration instructs mvn to execute checkstyle plugin in the root project and every sub-module. I don't want it to work this way. Instead, I want this plugin to be executed only for the root project, and be skipped for every sub-module. At the same time, I have many sub-modules, and I don't like the idea of explicitly skipping the plugin execution in every one of them.

Documentation for checkstyle says "..ensure that you do not include the Maven Checkstyle Plugin in your sub modules..". But how can I ensure that if my sub-modules inherit my root pom.xml? I'm lost, please help.

+1  A: 

Perhaps you should separate your root pom into 2 separate entities: parent pom and aggregator pom. Your aggregator pom may even inherit from parent pom.

If you download latest project layout for hibernate, you will see this design pattern in action.

After this separation is done, you can define and execute checkstyle plugin just in aggregator/root pom. Because it is no longer a parent of your submodules it will not get inherited by them.

EDIT
Use <relativePath> when declaring <parent>

Just for demonstration, below is an example taken from the hibernate project structure.
The whole distribution can be found here-> http://sourceforge.net/projects/hibernate/files/hibernate3

Just so, that you have some context, here is a subset of their directory layout

project-root
   |
   +-pom.xml
   |
   + parent
   |  |
   |  +-pom.xml
   |
   + core
      |
      +-pom.xml

   .. rest is scipped for brevity

project-root/pom.xml fragment

<parent>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-parent</artifactId>
    <version>3.5.4-Final</version>
    <relativePath>parent/pom.xml</relativePath>
</parent>

<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<packaging>pom</packaging>

<name>Hibernate Core Aggregator</name>
<description>Aggregator of the Hibernate Core modules.</description>

<modules>
    <module>parent</module>
    <module>core</module>

project-root/parent/pom.xml fragment

<groupId>org.hibernate</groupId>
<artifactId>hibernate-parent</artifactId>
<packaging>pom</packaging>
<version>3.5.4-Final</version>

project-root/core/pom.xml fragment

<parent>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-parent</artifactId>
    <version>3.5.4-Final</version>
    <relativePath>../parent/pom.xml</relativePath>
</parent>

<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<packaging>jar</packaging>
Alexander Pogrebnyak
@Alexander, thanks, the suggestion is really good, but now there is another problem. My root/aggregator project can't inherit from "parent pom" since it's _under_ root and is not available during first cycle of building.. Any ideas?
Vincenzo
@FaZend.com I've added some examples.
Alexander Pogrebnyak
This is not necessary, you can tell maven to not inherit a plugin configuration.
Pascal Thivent
+1  A: 

But how can I ensure that if my sub-modules inherit my root pom.xml?

To strictly answer this question, you can specify an <inherited> element inside a <plugin> definition. From the POM Reference:

inherited: true or false, whether or not this plugin configuration should apply to POMs which inherit from this one.

Something like this:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-checkstyle-plugin</artifactId>
  <!-- Lock down plugin version for build reproducibility -->
  <version>2.5</version>
  <inherited>true</inherited>
  <configuration>
    ...
  </configuration>
</plugin> 

Some more advices/remarks (that may not apply):

Pascal Thivent
Pascal, many thanks for suggestions, they are very helpful (I'm already using all of them in my project)!
Vincenzo
@FaZend.com Well, you're welcome.
Pascal Thivent