如何在已发布的Gradle build的ivy.xml中获取动态依赖关系的rev和revConstraint

时间:2016-01-13 13:45:56

标签: gradle

我有一个关于在Gradle中发布常春藤的问题。

从Ivy,我希望如果我发布一个带有例如这种依赖的工件:

<dependency org="org.apache.ant" name="ant" rev="1+"/>

我发布的ivy.xml获得了固定版本和动态版本:

<dependency org="org.apache.ant" name="ant" rev="1.9.6" revConstraint="1+"/>

我也希望在Gradle中。我有Gradle 2.10。

这是我的Gradle项目:

apply plugin: "java"

group = 'org.wibble'
version = "1.2.3"

repositories {
    mavenCentral()
}

dependencies {
  compile group: 'org.apache.ant', name: 'ant', version: '1+' // resolves to version 1.9.6 at the time of writing
}

uploadArchives.repositories {
  ivy { name "testrepo"; url "$buildDir/testrepo" }
}

如果我运行gradle uploadArchives,生成的ivy.xml就是这样:

<dependency org="org.apache.ant" name="ant" rev="1+" conf="compile-&gt;default"/>

source code of Gradle中我确实看到有写一个rev和revConstraint的工具:

if (!dep.getDynamicConstraintDependencyRevisionId().equals(dependencyRevisionId)) {
    <...>
    writer.attribute("revConstraint", dep.getDynamicConstraintDependencyRevisionId().getRevision());
}

通过调试我也看到这个代码被命中了,但在我的例子中,getDynamicConstraintDependencyRevisionId和dependencyRevisionId都给出了&#39; 1 +&#39;在这一点上,此时忘记了1.9.6版本。

如何在已发布的ivy.xml中记录依赖项版本,就像在Ivy中一样?

1 个答案:

答案 0 :(得分:0)

不确定这是否有任何帮助。但我最近不得不解决类似的问题,并提出了以下方法。希望它可以帮助其他人寻找解决方案。我根据RaGe的Pom solutiongradle forum solution的线索得出了这个答案。

我必须实现这一点的原因是,gradle发布的常春藤有rev,但不是rev​​Constraint属性。但是rev总是说&#34; latest.release&#34;这是我们内部库的用途。所以我不得不手动将rev转换为实际修订版,将revConstraint转换为latest.release。

publishing {
publications {
    ivyJava(IvyPublication) {            
        from components.java
        configurations.create('sources')
        artifact(sourceJar) {
            type "source"
            conf "sources"
            classifier "sources"
        }
        configurations.create('javadoc')
        artifact(javadocJar) {
            type "javadoc"
            conf "javadoc"
            classifier "javadoc"
        }
        descriptor {
            withXml {
                if (project.configurations.findByName("runtime") != null) {
                    Map resolvedVersionMap = [:]
                    Configuration runtimeConfiguration = project.configurations.getByName('runtime')
                    ResolutionResult resolution = runtimeConfiguration.incoming.resolutionResult
                    resolution.getAllComponents().each { ResolvedComponentResult versionResult ->
                        resolvedVersionMap.put("${versionResult.moduleVersion.group}:" +
                                ":${versionResult.moduleVersion.name}", versionResult.moduleVersion.version)
                    }

                    asNode().dependencies.dependency.each { dep ->
                        if ("latest.release".equalsIgnoreCase(dep.@rev) || "latest.integration".equalsIgnoreCase(dep.@rev)
                                || "latest.snapshot".equalsIgnoreCase(dep.@rev)) {
                            dep.@revConstraint = dep.@rev
                            dep.@rev = resolvedVersionMap.get("${dep.@org}:" +
                                    ":${dep.@name}")
                            dep.@changing = "true"
                        }
                    }
                }
            }
        }
    }
}

}