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 插件。但是我不需要在中执行 checkstyle foo-testkit,这是一个继承自的子模块foo,但同时也是一个依赖项..

4

4 回答 4

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您可以通过在该模块上运行来验证它是否正常工作。

于 2010-12-21T22:36:03.607 回答
2

我同意蒂姆克莱蒙斯的回答,但也有另一种选择,让你的项目嵌套。

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

common在pom.xml 中定义依赖sub-root。我并不是说这是最佳实践,但它是您问题的解决方案。

于 2010-12-21T21:12:41.977 回答
1

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

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

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

于 2010-12-21T20:38:29.283 回答
0

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

于 2014-12-21T23:07:19.413 回答