Gradle - 获取MavenDeployer URL

时间:2015-09-22 20:27:55

标签: maven gradle task gradle-plugin

我目前正在尝试在任务中获取Maven Deployer的URL,但它失败了。我能够获得Maven Deployer本身,但是在调用方法mavenDeployer.getRepository()时,我显然无法创建RemoteRepository类型的对象。

以下是build.gradle文件:

uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: "file:///path/to/maven/repo/")
        }
    }
}

在我的任务中:

Upload uploadArchives = project.getTasks().withType(Upload.class)
                          .findByName(BasePlugin.UPLOAD_ARCHIVES_TASK_NAME);

for(ArtifactRepository repo : uploadArchives.getRepositories()) {
    if (repo instanceof MavenDeployer) {
        MavenDeployer mavenDeployer = (MavenDeployer) repo;
        System.out.println(repo) //Returns org.apache.maven.artifact.ant.RemoteRepository

        RemoteRepository l = (RemoteRepository) mavenDeployer.getRepository() // Crashes here
    }
}

显示:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':myTask'.
> org/apache/maven/artifact/ant/RemoteRepository

stacktrace结束于:

Caused by: java.lang.ClassNotFoundException: org.apache.maven.artifact.ant.RemoteRepository

显然,Gradle找不到班级org.apache.maven.artifact.ant.RemoteRepository,即使我成功导入它之后也是如此。如果我注释掉创建类MavenDeployer的行,它就可以正常工作。

有没有办法解决这个问题?如果没有,是否有其他方法可以获取Maven部署者的存储库URL?

1 个答案:

答案 0 :(得分:1)

构建脚本类加载器不公开RemoteRepository。但是你应该能够通过groovys ducktyping来解决这些信息:

task printDeployerUrl << {
    tasks.withType(Upload) { uploadTask ->
        for(ArtifactRepository repo : uploadArchives.getRepositories()) {
            if (repo instanceof MavenDeployer) {
                MavenDeployer mavenDeployer = (MavenDeployer) repo;
                System.out.println(repo.repository.url)
            }
        }
    }
}