【问题标题】:How to resolve cyclic dependency in supplementary Maven sub-module?如何解决补充 Maven 子模块中的循环依赖?
【发布时间】:2011-05-29 01:32:53
【问题描述】:

有一个多模块 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的子模块,但同时也是一个依赖..

【问题讨论】:

    标签: java maven-2 maven maven-3


    【解决方案1】:

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

    <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 来验证它是否正常工作。

    【讨论】:

    • 据我了解,只有两个选项可用,谢谢。第一个看起来更有效,因为它不会导致许多 pom.xml 文件中的代码重复(我有 12 个子模块)
    【解决方案2】:

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

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

    sub-root pom 中定义对common 的依赖。我并不是说这是最佳做法,但它是您问题的解决方案。

    【讨论】:

    • 我喜欢这个想法,但它会让项目比现在更混乱。我最好将我的foo-testkit 移出继承树。这是直接但不优雅的解决方案。 (见上面更新的问题)
    【解决方案3】:

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

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

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

    【讨论】:

    • 我需要在&lt;build&gt; 中使用&lt;plugins&gt; 部分,因为我必须在那里声明插件&lt;executions&gt;,这对于所有子模块都是通用的(请参阅我的问题更新)。
    • 请注意,没有什么可以阻止您在 &lt;pluginManagement&gt; 部分中指定 &lt;executions&gt;
    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 2013-05-04
      • 1970-01-01
      • 2016-08-27
      • 1970-01-01
      • 2014-04-15
      • 2021-08-13
      • 1970-01-01
      • 2021-06-20
      相关资源
      最近更新 更多