在Gradle构建中定义Maven存储库后,如何用本地目录替换它?

时间:2018-11-07 22:45:43

标签: gradle

我正在尝试为构建过程编写测试,因此我的单元测试希望用本地位置替换实际的存储库位置,以免污染真实的服务器。 (另外,我想,运行测试的人可能仍然无权发布。)

在内部版本中:

publishing {
    repositories {
        maven {
            name = 'snapshot'
            url = "${artifactory_contextUrl}/libs-snapshot-local"
            credentials {
                username artifactory_user
                password artifactory_password
            }
        }

        maven {
            name = 'release'
            url = "${artifactory_contextUrl}/libs-release-local"
            credentials {
                username artifactory_user
                password artifactory_password
            }
        }
    }
}

在我的测试版本中,我试图以此替换它:

publishing {
    repositories {
        getByName('snapshot') {
            url = uri('/tmp/local-repo/snapshots')
        }
        getByName('release') {
            url = uri('/tmp/local-repo/release')
        }
    }
}

当我尝试运行构建时,我得到:

Execution failed for task ':publishMavenJavaPublicationToReleaseRepository'.
> Failed to publish publication 'mavenJava' to repository 'release'
   > Authentication scheme 'all'(Authentication) is not supported by protocol 'file'

网络上有很多关于此特定错误的文章,但似乎总是有人不小心在应该放置URI的情况下放置了文件路径。不过,我是故意放入URI,所以有办法使它起作用吗?

我也尝试过这个:

publishing {
    repositories {
        clear()
        maven {
            name = 'snapshot'
            url = uri('/tmp/local-repo/snapshots')
        }
        maven {
            name = 'release'
            url = uri('/tmp/local-repo/release')
        }
    }
}

失败的原因:

A problem occurred configuring root project 'test-common-plugin1913987501683151177'.
> Exception thrown while executing model rule: PublishingPluginRules#publishing(ExtensionContainer)
  > Cannot add task 'publishMavenJavaPublicationToSnapshotRepository' as a task with that name already exists.

令我惊讶的是,删除所有存储库不会同时删除它们拥有的所有任务。当我尝试以编程方式删除它抱怨的任务时,Gradle声称它不存在。

1 个答案:

答案 0 :(得分:1)

好的,我最终不得不再次阅读Gradle的源代码,但是我找到了一种方法。本质上,您可以将credentials直接设置回null,如下所示:

publishing {
    repositories {
        getByName('snapshot') {
            url = uri('/tmp/local-repo/snapshots')
            configuredCredentials = null
        }
        getByName('release') {
            url = uri('/tmp/local-repo/release')
            configuredCredentials = null
        }
    }
}