无法使用Gradle

时间:2017-10-03 09:25:02

标签: java intellij-idea gradle build.gradle nexus

我正在尝试使用Nexus(版本2.X)作为本地Maven存储库。 我一直在寻找一种方法来自动将我的依赖项上传到Nexus,但我无法让它工作。

如果重要,请使用Gradle 4.1,Java 8和InteliJIdea。

我的build.gradle文件如下:

group 'myGroupName'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin:'maven'

sourceCompatibility = 1.8

repositories {
    maven {
        url "http://localhost:8081/nexus/content/repositories/thirdparty/"
    }
}

dependencies {
    testCompile 'org.testng:testng:6.9.4'
    compile group: 'org.apache.commons', name: 'commons-math3', version: '3.5'
    compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.6'
}

uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: "http://localhost:8081/nexus/content/repositories/thirdparty/") {
                authentication(userName: "admin", password: "admin123")
            }
        }
    }
}

我已手动上传commons-math3-3.5.jar,并尝试使用uploadArchives自动上传commons-lang3-3.6

我得到的错误是

* What went wrong:
Could not resolve all files for configuration ':compileClasspath'.
> Could not resolve org.apache.commons:commons-lang3:3.6.
  Required by:
      project :
   > Could not resolve org.apache.commons:commons-lang3:3.6.
      > Could not get resource 'http://localhost:8081/nexus/content/repositories/thirdparty/org/apache/commons/commons-lang3/3.6/commons-lang3-3.6.pom'.
         > Could not GET 'http://localhost:8081/nexus/content/repositories/thirdparty/org/apache/commons/commons-lang3/3.6/commons-lang3-3.6.pom'. Received status code 401 from server: Unauthorized

我还尝试了compile "org.apache.commons:commons-lang3:3.6"而不是compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.6',以便与Nexus Build Grade Example匹配,并尝试添加

task wrapper( type: Wrapper, description: "create a gradlew" ) {
gradleVersion = '4.1'
}

但它不起作用。

有谁知道我错过了什么?

2 个答案:

答案 0 :(得分:1)

没有完整的错误消息,但看起来您在编译阶段遇到错误。我假设它无法从编译所需的Nexus依赖项下载。尝试在存储库部分添加jcenter()以解决此问题。 或者,您可以在maven配置中为Nexus提供有效的凭据

repositories {
    maven {
        url "http://localhost:8081/nexus/content/repositories/thirdparty/"
        credentials {
                username "user"
                password "password"
        }
    }
}

我建议也使用maven-publish插件https://docs.gradle.org/current/userguide/publishing_maven.html

BTW您使用的示例已过时。我建议使用与Gradle捆绑的示例或其文档。

答案 1 :(得分:0)

这对我有用

{{1}}