我有一个gradle插件来实现自定义版本控制方案,几乎与gradle文档的第49.8.2.2节中的建议相同。使用gradle的公司项目应用插件,然后按照建议声明依赖关系:compile'group:name:{default}'其中{default}是一个字符串,表示插件应提供实际版本。该插件在配置时将{default}替换为正确的版本,由插件中的逻辑确定。这一切都很有效。
问题在于,当我运行使用mavenDeployer的uploadArchives任务时,生成的pom文件中产生的依赖关系会得到{default}的版本,即指示符字符串而不是实际的依赖关系版本。当我们的maven项目试图将这个pom作为依赖项时,他们无法解决它的依赖性。
浏览网页并自己弄乱它,看来在运行uploadArchives任务时,gradle首先转储所有其他构建的配置。因此,即使项目配置是由插件在配置阶段使用正确的依赖版本构建的,生成pom文件时uploadArchives任务也不会使用这些版本的配置。
如何使用自定义版本插件在配置时解析的依赖项版本来获取uploadArchives任务以生成pom文件?
这是我的build.gradle文件:
buildscript {
repositories {
maven {url "somewhere/overtherainbow/releases/"}
}
dependencies {
classpath 'com.overtherainbow:custom-dependency-resolution-plugin:1.0-SNAPSHOT'
}
}
subprojects {
apply plugin: 'custom-dependency-resolution'
apply plugin: 'java'
apply plugin: 'maven'
group = "com.overtherainbow"
version ="1.0-SNAPSHOT"
repositories {
maven {url "somewhere/overtherainbow/releases/"}
mavenCentral()
}
dependencies {
compile 'javax.servlet:servlet-api:default'
compile 'log4j:log4j:default'
}
uploadArchives {
repositories {
mavenDeployer {
uniqueVersion = false
repository(url: "somewhere/overtherainbow/releases)
snapshotRepository(url: "somewhere/overtherainbow/snapshots")
}
}
}
}
project(':bar') {
dependencies {
compile project(':common')
compile project(':foo')
}
}
project(':foo') {
dependencies {
compile project(':common')
}
}
以下是插件代码:
package com.overtherainbow
import org.gradle.api.Project
import org.gradle.api.Plugin
import org.gradle.api.artifacts.DependencyResolveDetails
class DefaultVersionPlugin implements Plugin<Project> {
// This is where dependency versions are defined
def defaultVersionsMap = [
'javax.servlet:servlet-api' : '2.5',
'log4j:log4j' : '1.2.16']
void apply(Project project) {
project.configurations.all {
resolutionStrategy.eachDependency {
DependencyResolveDetails details -> resolveDependencyVersion(project, details)
}
}
}
def resolveDependencyVersion(Project project, DependencyResolveDetails details) {
if (details.requested.version == 'default') {
def version = resolveDefaultVersion(project, details.requested.group, details.requested.name)
details.useVersion version
}
}
def resolveDefaultVersion(Project project, String group, String name) {
project.logger.debug("Resolving default dependency for $group:$name")
println "Resolving default dependency for $group:$name"
defaultVersionsMap["$group:$name"]
}
}
运行uploadArchives的输出,注意插件在每个项目的第一个任务(compileJava)期间运行,但不是uploadArchives:
$ gradle uploadArchives
:common:compileJava
Resolving default dependency for javax.servlet:servlet-api
Resolving default dependency for log4j:log4j
:common:compileJava UP-TO-DATE
:common:processResources UP-TO-DATE
:common:classes UP-TO-DATE
:common:jar UP-TO-DATE
:foo:compileJava
Resolving default dependency for javax.servlet:servlet-api
Resolving default dependency for log4j:log4j
:foo:compileJava UP-TO-DATE
:foo:processResources UP-TO-DATE
:foo:classes UP-TO-DATE
:foo:jar UP-TO-DATE
:bar:compileJava
Resolving default dependency for javax.servlet:servlet-api
Resolving default dependency for log4j:log4j
:bar:compileJava UP-TO-DATE
:bar:processResources UP-TO-DATE
:bar:classes UP-TO-DATE
:bar:jar UP-TO-DATE
:bar:uploadArchives
Uploading: com/overtherainbow/bar/1.0-SNAPSHOT/bar-1.0-SNAPSHOT.jar to...
Transferring 19K from remote
Uploaded 19K
:common:uploadArchives
Uploading: com/overtherainbow/common/1.0-SNAPSHOT/common-1.0-SNAPSHOT.jar to...
Transferring 5K from remote
Uploaded 5K
:foo:uploadArchives
Uploading: com/overtherainbow/foo/1.0-SNAPSHOT/foo-1.0-SNAPSHOT.jar to...
Transferring 15K from remote
Uploaded 15K
以下是生成的pom文件的依赖项部分,请注意版本设置为默认值:
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>default</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>default</version>
<scope>compile</scope>
</dependency>
</dependencies>