Maven 2 如何在依赖冲突上失败maven构建
我正在处理一个大型多模块项目,该项目使用内部框架作为其依赖项之一。框架版本在项目开始时在顶层pom中设置,并且必须保持不变。如果任何子模块使用不同的版本,我希望构建失败 我尝试将依赖项声明为单个版本:Maven 2 如何在依赖冲突上失败maven构建,maven-2,locking,dependencies,Maven 2,Locking,Dependencies,我正在处理一个大型多模块项目,该项目使用内部框架作为其依赖项之一。框架版本在项目开始时在顶层pom中设置,并且必须保持不变。如果任何子模块使用不同的版本,我希望构建失败 我尝试将依赖项声明为单个版本: <dependency> <groupId>framework_snt</groupId> <artifactId>SFP</artifactId> <version>[6.1]</version>
<dependency>
<groupId>framework_snt</groupId>
<artifactId>SFP</artifactId>
<version>[6.1]</version>
<type>pom</type>
</dependency>
框架
,但这些方法都不起作用
鉴于这个顶级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>
glb
GLB
聚甲醛
全球的
1
........
框架
SFP
[6.1.2]
聚甲醛
.....
以及此(无效)子模块:
<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>
glb
果心
罐子
果心
1
glb
GLB
1
框架
SFP
6.3
聚甲醛
如何设置maven,以便在使用上面的子模块时生成失败,但当我删除标记6.3
时,生成成功(或者我更改版本以匹配顶级pom中的版本?在对各种插件进行了大量搜索和尝试后,我决定编写自己的插件来进行冲突检查。使用dependency:tree插件源代码作为基础,我编写了以下内容:
/**
* 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);
}
您可以使用以下方法从现有插件调用此功能:
/**
* 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);
}
然后在您的plugin pom.xml中,包含来自现有依赖插件源的依赖项和您的good to go
这适用于我们的项目,但如果有人知道更干净或更好的方法,请告诉我。框架是贵公司内部的吗?您可以通过nexus设置块吗?是的,框架是我公司内部的。在nexus中设置块不起作用,原因有二:1)我们使用的是artifactory,2)我需要使用这个概念,而不仅仅是内部框架版本。