yegor256
yegor256

Reputation: 105053

How to resolve cyclic dependency in supplementary Maven sub-module?

There is a multi-module Maven-3 project, where one of sub-modules is used as <dependency> in all other modules. At the same time, all sub-modules inherit from parent module. Such a structure leads to cyclic dependency. How can I resolve it?

Project structure is rather typical:

/foo
  /foo-testkit
  /foo-core

This is parent foo/pom.xml:

[...]
<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-checkstyle-plugin</artifactId>
      <configuration>
        <configLocation>checkstyle/checks.xml</configLocation>
      </configuration>
      <dependencies>
        <dependency>
          <groupId>${project.groupId}</groupId>
          <artifactId>foo-testkit</artifactId>
          <version>${project.version}</version>
        </dependency>
      </dependencies>
      <executions>
        <execution>
          <phase>prepare-package</phase>
          <goals>
            <goal>check</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>
[...]

In parent foo/pom.xml I specify how and when checkstyle plugin has to be executed in every sub-module. But I don't need checkstyle to be executed in foo-testkit, which is a sub-module inheriting from foo, but is at the same time a dependency..

Upvotes: 4

Views: 8753

Answers (4)

yair
yair

Reputation: 689

If you don't actually need it in foo (only in its sub modules), you can solve the cyclic issue by moving the plugin definition from the build segment to a pluginManagement segment in foo/pom.xml.

Upvotes: 0

jgifford25
jgifford25

Reputation: 2154

One way is to disable the checkstyle plugin for module foo-testkit by adding the below to foo-testkit's pom.xml file.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-checkstyle-plugin</artifactId>
  <configuration>
    <skip>true</skip>
  </configuration>
</plugin>

If that is not to your liking, another way is to move the checkstyle plugin configuration from build/plugins to build/pluginManagment/plugins in the parent pom.xml file. Then in each module you want checkstyle executed, add this to the build/plugins section of each module's pom.xml file:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-checkstyle-plugin</artifactId>
</plugin>

This invokes the plugin for that module and the configuration specified in the parent pom.xml under the pluginManagement section will be applied. You can verify that is working correctly by running mvn help:effective-pom on that module.

Upvotes: 4

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 298838

I agree with Tim Clemons's answer, but there is also an alternative, make your project nested.

                       root
                     /       \
                 common    sub-root
                         /     |    \
                       sub1  sub2  sub3

Define the dependency to common in the sub-root pom. I'm not saying this is a best practice, but it is a solution to your problem.

Upvotes: 2

Tim Clemons
Tim Clemons

Reputation: 6441

So I take it the parent pom is referencing one of the submodules as a dependency? I would suggest if you have any build logic going on in the parent module you push it down into a new submodule. The parent should limit itself to specifying the <modules>, <pluginManagement>, and <dependencyManagement> sections. All other work should be farmed out to submodules.

See the following for more advice on organizing multi-module projects:

http://www.sonatype.com/books/mvnref-book/reference/pom-relationships-sect-pom-best-practice.html

Upvotes: 1

Related Questions