如何解决补充Maven子模块中的循环依赖?

时间:2010-12-21 20:29:00

标签: java maven-2 maven maven-3

有一个多模块Maven-3项目,其中一个子模块在所有其他模块中用作<dependency>。同时,所有子模块都从父模块继承。这种结构导致循环依赖性。我该如何解决?

项目结构相当典型:

/foo
  /foo-testkit
  /foo-core

这是父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>
[...]

在父foo/pom.xml中,我指定了必须在每个子模块中执行checkstyle插件的方式和时间。但我不需要在foo-testkit中执行checkstyle,这是一个继承自foo的子模块,但同时也是一个依赖项。

4 个答案:

答案 0 :(得分:4)

一种方法是通过将以下内容添加到foo-testkit的pom.xml文件来禁用模块foo-testkit的checkstyle插件。

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

如果这不符合您的喜好,另一种方法是将checkstyle插件配置从build / plugins移动到父pom.xml文件中的build / pluginManagment / plugins。然后在每个模块中执行checkstyle,将其添加到每个模块的pom.xml文件的build / plugins部分:

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

这将调用该模块的插件,并将应用pluginManagement部分下的父pom.xml中指定的配置。您可以通过在该模块上运行mvn help:effective-pom来验证其是否正常工作。

答案 1 :(得分:2)

我同意Tim Clemons's answer,但也有另一种选择,让您的项目嵌套。

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

common pom中定义sub-root的依赖关系。我不是说这是一种最佳做法,但它可以解决您的问题。

答案 2 :(得分:1)

所以我认为父pom引用其中一个子模块作为依赖项?我建议如果你在父模块中有任何构建逻辑,你将它推入一个新的子模块。父级应限制为指定<modules><pluginManagement><dependencyManagement>部分。所有其他工作都应该用于子模块。

有关组织多模块项目的更多建议,请参阅以下内容:

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

答案 3 :(得分:0)

如果你实际上并不需要它在foo中(仅在其子模块中),你可以通过将插件定义从构建段移动到foo / pom.xml中的pluginManagement段来解决循环问题。

相关问题