How to resolve circular dependency in the optional Maven submodule?

There is a multi-module Maven-3 project in which one of the submodules is used as a <dependency> in all other modules. At the same time, all submodules are inherited from the parent module. Such a structure leads to a cyclic dependence. How can I solve it?

The structure of the project is quite typical:

 /foo /foo-testkit /foo-core 

This is the 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 the parent foo/pom.xml I specify how and when the checkstyle plugin should be executed in each submodule. But I do not need a checkstyle to execute in foo-testkit , which is a submodule inheriting from foo , but at the same time dependent.

+4
source share
4 answers

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

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

If you don’t like it, another way is to move the checkstyle plugin configuration from build / plugins to create / pluginManagment / plugins in the parent pom.xml file. Then in each module you want to checkstyle, add it to the build / plugins section of each module pom.xml file:

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

This calls the plugin for this module, and the configuration specified in the parent pom.xml in the pluginManagement section will be applied. You can verify that it works by running mvn help:effective-pom in this module.

+4
source

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 a dependency on common in sub-root pom. I am not saying this is the best practice, but it is the solution to your problem.

+2
source

So, I understand that the parent pom refers to one of the submodules as a dependency? I would advise that if you have the assembly logic in the parent module, you are pushing it into a new submodule. The parent should limit itself to specifying the <modules> , <pluginManagement> and <dependencyManagement> sections. All other work must be processed by submodules.

For additional recommendations on organizing multi-module projects, see the following:

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

+1
source

If you really do not need this in foo (only in its sub-blocks), you can solve the cyclical problem by moving the plugin definition from the assembly segment to the pluginManagement segment in the file foo / pom.xml.

0
source

Source: https://habr.com/ru/post/1332958/


All Articles