2012-02-01 46 views
1

我正在使用内部框架作为其依赖关系之一的大型多模块项目。框架版本在项目开始时设置在顶层pom中,并且必须保持不变。如果任何子模块使用不同的版本,我想构建失败。如何失败Maven构建依赖冲突

我用尽声明依赖作为一个单一的版本:

<dependency> 
    <groupId>framework_snt</groupId> 
    <artifactId>SFP</artifactId> 
    <version>[6.1]</version> 
    <type>pom</type> 
</dependency> 
使用实施者插件与禁止依赖

我用尽:

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-enforcer-plugin</artifactId> 
    <version>1.0.1</version> 
    <executions> 
    <execution> 
     <id>enforce-versions</id> 
     <goals> 
     <goal>enforce</goal> 
     </goals> 
     <configuration> 
     <rules> 
      <requireJavaVersion> 
      <version>[1.6.0-21]</version> 
      </requireJavaVersion> 
      <requireMavenVersion> 
      <version>[3.0.3]</version> 
      </requireMavenVersion> 
      <bannedDependencies> 
      <excludes> 
       <exclude>framework_snt:SFP</exclude> 
      </excludes> 
      <includes> 
       <include>framework_snt:SFP:6.1.2</include> 
      </includes> 
      </bannedDependencies> 
     </rules> 
     </configuration> 
    </execution> 
    </executions> 
</plugin> 

香港专业教育学院也尝试添加了<DependencyConvergence/>标签提到here,但这些方法都不起作用。



所以给这个顶层POM片段:

<project> 
    <groupId>glb</groupId> 
    <artifactId>GLB</artifactId> 
    <packaging>pom</packaging> 
    <name>Global</name> 
    <version>1.0</version> 
    ........ 
    <dependencies> 
    <dependency> 
     <groupId>framework_snt</groupId> 
     <artifactId>SFP</artifactId> 
     <version>[6.1.2]</version> 
     <type>pom</type> 
    </dependency> 
    </dependencies> 
    ..... 
</project> 

而这个(无效)submmodule:

<project> 
    <groupId>glb</groupId> 
    <artifactId>CORE</artifactId> 
    <packaging>jar</packaging> 
    <name>Core</name> 
    <version>1.0</version> 

    <parent> 
    <groupId>glb</groupId> 
    <artifactId>GLB</artifactId> 
    <version>1.0</version> 
    </parent> 

    <dependencies> 
    <dependency> 
     <groupId>framework_snt</groupId> 
     <artifactId>SFP</artifactId> 
     <version>6.3</version> 
     <type>pom</type> 
    </dependency> 
    </dependencies> 
</project> 

我怎么设置的Maven这样的构建将使用儿童在失败模块上面,但是当我删除标签<version>6.3</version>它成功了(或者我改变了版本以匹配顶级pom的版本?

+0

该框架是否属于贵公司?你可以通过nexus设置一个块吗? – 2012-02-22 21:05:15

+0

是的框架是我公司内部的。在nexus中设置模块不会有效,原因有二:1)我们使用artifactory,2)我需要使用这个概念不仅仅是内部框架版本。 – Jon 2012-02-25 01:02:54

回答

0

经过多次搜索并尝试使用各种插件并且无处不在之后,我决定编写自己的插件来执行冲突检查。使用依赖性:树插件源为基础,我写了以下内容:

/** 
    * Walks the dependency tree looking for anything marked as a conflict, and fails the build if one is found 
    * code ripped from the dependency:tree plugin 
    * 
    * @param rootNode 
    * the dependency tree root node to check 
    * 
    */ 
    private void checkForConflicts(DependencyNode rootNode) throws MojoExecutionException 
    { 
    StringWriter writer = new StringWriter(); 
    boolean conflicts=false; 

    // build the tree 
    DependencyNodeVisitor visitor = new SerializingDependencyNodeVisitor(writer, SerializingDependencyNodeVisitor.STANDARD_TOKENS); 
    visitor = new BuildingDependencyNodeVisitor(visitor); 
    rootNode.accept(visitor); 

    getLog().debug("Root: "+rootNode.toNodeString()); 

    DependencyNode node; 
    Artifact actual, conflict; 
    DependencyTreeInverseIterator iter = new DependencyTreeInverseIterator(rootNode); 
    while (iter.hasNext()) 
    { 
     node = (DependencyNode)iter.next(); 
     if (node.getState() == DependencyNode.OMITTED_FOR_CONFLICT) 
     { 
     actual = node.getArtifact(); 
     conflict = node.getRelatedArtifact(); 
     getLog().debug("conflict node: " + node.toNodeString()); 
     getLog().debug("conflict with: "+conflict.toString()); 

     getLog().error(actual.getGroupId()+":"+actual.getArtifactId()+":"+actual.getType()+" Conflicting versions: "+actual.getVersion()+" and "+conflict.getVersion()); 
     conflicts=true; 
     } 
    } 

    if (conflicts) 
     throw new MojoExecutionException("version conflict detected"); 
    } 

您可以从现有的插件使用以下称之为:

try 
    { 
    rootNode = dependencyTreeBuilder.buildDependencyTree(project, localRepository, artifactFactory, artifactMetadataSource, null, artifactCollector); 
    checkForConflicts(rootNode); 
    } 
catch (DependencyTreeBuilderException e) 
    { 
    throw new MojoExecutionException("Cannot build project dependency tree", e); 
    } 

然后在你的插件的pom.xml,包括从现有的依赖插件源的依赖关系和你的好去。

这适用于我们的项目 - 但如果有人知道更清洁或更好的方式,请让我知道。