2012-02-01 44 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,包括從現有的依賴插件源的依賴關係和你的好去。

這適用於我們的項目 - 但如果有人知道更清潔或更好的方式,請讓我知道。