如何解决Maven附属子模块中的循环依赖问题?

4

有一个多模块的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个回答

4

一种方法是通过在foo-testkit的pom.xml文件中添加以下内容来禁用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/pluginManagement/plugins中。然后在每个想要执行checkstyle的模块中,将以下内容添加到每个模块的pom.xml文件的build/plugins部分:

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

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


据我所知,只有两个选项可用,谢谢。第一个选项看起来更有效,因为它不会在许多“pom.xml”文件中导致代码重复(我有12个子模块)。 - yegor256

2

我同意Tim Clemons的回答,但也有另一种选择,将项目嵌套。

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

sub-root pom中定义对common的依赖关系。我不是说这是最佳实践,但这是解决您问题的一种方法。

我喜欢这个想法,但它会使项目比现在更混乱。我最好把我的 foo-testkit 移出继承树。 这是一种直接而不优雅的解决方案。(请参见上面更新的问题) - yegor256

1

我需要使用<build>中的<plugins>部分,因为我需要在那里声明插件<executions>,这对于所有子模块都是通用的(请参见我的更新问题)。 - yegor256
请注意,您可以在<pluginManagement>部分中指定<executions>,没有任何限制。 - Tim Clemons

0

如果你只需要在foo的子模块中使用它,而不是在foo本身中使用,那么你可以通过将插件定义从build段移动到foo/pom.xml中的pluginManagement段来解决循环依赖问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接