2010-12-21 646 views
4

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

项目结构比较典型:

/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

一种方法是通过将以下内容添加到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插件配置从构建/插件移动打造父pom.xml文件/ pluginManagment /插件。然后,你要CHECKSTYLE执行每个模块中,将它添加到每个模块的pom.xml文件的生成/插件部分:

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

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

1

因此,我认为父pom是引用其中一个子模块作为依赖?我会建议如果你有任何构建逻辑在父模块中进行,你把它推到一个新的子模块。家长应限制为指定<modules>,<pluginManagement><dependencyManagement>部分。所有其他的工作应该放在子模块上。

有关详情,请咨询以下关于组织多模块项目:

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

+0

我需要在``中使用``部分,因为我必须在那里声明插件``,这对所有子模块都很常见(请参阅我的问题更新)。 – yegor256 2010-12-21 21:17:25

+0

请注意,没有什么能够阻止您在``部分中指定``。 – 2010-12-22 07:40:24

2

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

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

定义依赖性commonsub-root POM。我并不是说这是最佳做法,但它是解决您的问题的方法。

+0

我喜欢这个想法,但它会使项目比现在更混乱。我最好将我的`foo-testkit`移出继承树。这是直接的解决方案,但不够优雅。 (见上面更新的问题) – yegor256 2010-12-21 21:16:12

0

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