java - 如何使用 gradle 下载/使用已发布的工件

标签 java gradle build.gradle

我正在尝试将工件发布到具有两个依赖项(Web 和 Actuator)的私有(private) GitLab,如下所示

dependencies {
    compile("org.springframework.boot:spring-boot-starter-actuator")
    compile("org.springframework.boot:spring-boot-starter-web")

}
repositories {
    maven {
        url "https://<gitlab-url>/repo"
        name "GitLab"
        credentials(HttpHeaderCredentials) {
            name = 'Private-Token'
            value = gitLabPrivateToken
        }
        authentication {
            header(HttpHeaderAuthentication)
        }
    }
}

但不确定我是否面临 401 错误。我将 git URL 替换为本地目录,结果成功。我可以看到生成的元数据和 pom 文件。使用以下build.gradle

使用另一个项目中本地目录中的工件时
buildscript {
    repositories {
        mavenCentral()
         maven {
            url "C://<location>//gradle-test"
        }
    }
    dependencies {
        classpath("com.gradle:gradleproject:0.0.1-SNAPSHOT")
    }
}

我遇到以下错误。

C:\Users\root\gradle>gradle build

> Task :buildEnvironment

------------------------------------------------------------
Root project
------------------------------------------------------------

classpath
\--- com.gradle:gradleproject:0.0.1-SNAPSHOT
     +--- org.springframework.boot:spring-boot-starter-actuator:2.3.0.RELEASE
     |    +--- org.springframework.boot:spring-boot-starter:2.3.0.RELEASE
     |    |    +--- org.springframework.boot:spring-boot:2.3.0.RELEASE
     |    |    |    +--- org.springframework:spring-core:5.2.6.RELEASE
     |    |    |    |    \--- org.springframework:spring-jcl:5.2.6.RELEASE
     |    |    |    \--- org.springframework:spring-context:5.2.6.RELEASE
     |    |    |         +--- org.springframework:spring-aop:5.2.6.RELEASE
     |    |    |         |    +--- org.springframework:spring-beans:5.2.6.RELEASE
     |    |    |         |    |    \--- org.springframework:spring-core:5.2.6.RELEASE (*)
     |    |    |         |    \--- org.springframework:spring-core:5.2.6.RELEASE (*)
     |    |    |         +--- org.springframework:spring-beans:5.2.6.RELEASE (*)
     |    |    |         +--- org.springframework:spring-core:5.2.6.RELEASE (*)
     |    |    |         \--- org.springframework:spring-expression:5.2.6.RELEASE
     |    |    |              \--- org.springframework:spring-core:5.2.6.RELEASE (*)
     |    |    +--- org.springframework.boot:spring-boot-autoconfigure:2.3.0.RELEASE
     |    |    |    \--- org.springframework.boot:spring-boot:2.3.0.RELEASE (*)
     |    |    +--- org.springframework.boot:spring-boot-starter-logging:2.3.0.RELEASE
     |    |    |    +--- ch.qos.logback:logback-classic:1.2.3
     |    |    |    |    +--- ch.qos.logback:logback-core:1.2.3
     |    |    |    |    \--- org.slf4j:slf4j-api:1.7.25 -> 1.7.30
     |    |    |    +--- org.apache.logging.log4j:log4j-to-slf4j:2.13.2
     |    |    |    |    +--- org.slf4j:slf4j-api:1.7.25 -> 1.7.30
     |    |    |    |    \--- org.apache.logging.log4j:log4j-api:2.13.2
     |    |    |    \--- org.slf4j:jul-to-slf4j:1.7.30
     |    |    |         \--- org.slf4j:slf4j-api:1.7.30
     |    |    +--- jakarta.annotation:jakarta.annotation-api:1.3.5
     |    |    +--- org.springframework:spring-core:5.2.6.RELEASE (*)
     |    |    \--- org.yaml:snakeyaml:1.26
     |    +--- org.springframework.boot:spring-boot-actuator-autoconfigure:2.3.0.RELEASE
     |    |    +--- org.springframework.boot:spring-boot-actuator:2.3.0.RELEASE
     |    |    |    \--- org.springframework.boot:spring-boot:2.3.0.RELEASE (*)
     |    |    +--- org.springframework.boot:spring-boot:2.3.0.RELEASE (*)
     |    |    +--- org.springframework.boot:spring-boot-autoconfigure:2.3.0.RELEASE (*)
     |    |    +--- com.fasterxml.jackson.core:jackson-databind:2.11.0
     |    |    |    +--- com.fasterxml.jackson.core:jackson-annotations:2.11.0
     |    |    |    \--- com.fasterxml.jackson.core:jackson-core:2.11.0
     |    |    +--- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.11.0
     |    |    |    +--- com.fasterxml.jackson.core:jackson-annotations:2.11.0
     |    |    |    +--- com.fasterxml.jackson.core:jackson-core:2.11.0
     |    |    |    \--- com.fasterxml.jackson.core:jackson-databind:2.11.0 (*)
     |    |    +--- org.springframework:spring-core:5.2.6.RELEASE (*)
     |    |    \--- org.springframework:spring-context:5.2.6.RELEASE (*)
     |    \--- io.micrometer:micrometer-core:1.5.1
     |         +--- org.hdrhistogram:HdrHistogram:2.1.12
     |         \--- org.latencyutils:LatencyUtils:2.0.3
     \--- org.springframework.boot:spring-boot-starter-web:2.3.0.RELEASE
          +--- org.springframework.boot:spring-boot-starter:2.3.0.RELEASE (*)
          +--- org.springframework.boot:spring-boot-starter-json:2.3.0.RELEASE
          |    +--- org.springframework.boot:spring-boot-starter:2.3.0.RELEASE (*)
          |    +--- org.springframework:spring-web:5.2.6.RELEASE
          |    |    +--- org.springframework:spring-beans:5.2.6.RELEASE (*)
          |    |    \--- org.springframework:spring-core:5.2.6.RELEASE (*)
          |    +--- com.fasterxml.jackson.core:jackson-databind:2.11.0 (*)
          |    +--- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.11.0
          |    |    +--- com.fasterxml.jackson.core:jackson-core:2.11.0
          |    |    \--- com.fasterxml.jackson.core:jackson-databind:2.11.0 (*)
          |    +--- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.11.0 (*)
          |    \--- com.fasterxml.jackson.module:jackson-module-parameter-names:2.11.0
          |         +--- com.fasterxml.jackson.core:jackson-core:2.11.0
          |         \--- com.fasterxml.jackson.core:jackson-databind:2.11.0 (*)
          +--- org.springframework.boot:spring-boot-starter-tomcat:2.3.0.RELEASE
          |    +--- jakarta.annotation:jakarta.annotation-api:1.3.5
          |    +--- org.apache.tomcat.embed:tomcat-embed-core:9.0.35
          |    +--- org.glassfish:jakarta.el:3.0.3
          |    \--- org.apache.tomcat.embed:tomcat-embed-websocket:9.0.35
          |         \--- org.apache.tomcat.embed:tomcat-embed-core:9.0.35
          +--- org.springframework:spring-web:5.2.6.RELEASE (*)
          \--- org.springframework:spring-webmvc:5.2.6.RELEASE
               +--- org.springframework:spring-aop:5.2.6.RELEASE (*)
               +--- org.springframework:spring-beans:5.2.6.RELEASE (*)
               +--- org.springframework:spring-context:5.2.6.RELEASE (*)
               +--- org.springframework:spring-core:5.2.6.RELEASE (*)
               +--- org.springframework:spring-expression:5.2.6.RELEASE (*)
               \--- org.springframework:spring-web:5.2.6.RELEASE (*)

(*) - dependencies omitted (listed previously)

有人可以帮我解决我犯了错误的地方或如何解决此错误吗?这是正确的消费方式吗?

最佳答案

以下示例演示如何签署和发布 Java 库,包括源代码、Javadoc 和自定义 POM:

build.gradle

plugins {
    id 'java-library'
    id 'maven-publish'
    id 'signing'
}

group = 'com.example'
version = '1.0'

java {
    withJavadocJar()
    withSourcesJar()
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            artifactId = 'my-library'
            from components.java
            versionMapping {
                usage('java-api') {
                    fromResolutionOf('runtimeClasspath')
                }
                usage('java-runtime') {
                    fromResolutionResult()
                }
            }
            pom {
                name = 'My Library'
                description = 'A concise description of my library'
                url = 'http://www.example.com/library'
                properties = [
                    myProp: "value",
                    "prop.with.dots": "anotherValue"
                ]
                licenses {
                    license {
                        name = 'The Apache License, Version 2.0'
                        url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                    }
                }
                developers {
                    developer {
                        id = 'johnd'
                        name = 'John Doe'
                        email = 'john.doe@example.com'
                    }
                }
                scm {
                    connection = 'scm:git:git://example.com/my-library.git'
                    developerConnection = 'scm:git:ssh://example.com/my-library.git'
                    url = 'http://example.com/my-library/'
                }
            }
        }
    }
    repositories {
        maven {
            // change URLs to point to your repos, e.g. http://my.org/repo
            def releasesRepoUrl = "$buildDir/repos/releases"
            def snapshotsRepoUrl = "$buildDir/repos/snapshots"
            url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
        }
    }
}

signing {
    sign publishing.publications.mavenJava
}


javadoc {
    if(JavaVersion.current().isJava9Compatible()) {
        options.addBooleanOption('html5', true)
    }
}

结果是将发布以下工件:

  • POM:my-library-1.0.pom
  • Java 组件的主要 JAR 工件:my-library-1.0.jar
  • 已显式配置的源 JAR 工件:my-library-1.0-sources.jar
  • 已显式配置的 Javadoc JAR 工件:my-library-1.0-javadoc.jar

Signing Plugin用于为每个工件生成签名文件。此外,将为所有工件和签名文件生成校验和文件。

引用此链接:

https://docs.gradle.org/current/userguide/publishing_maven.html

关于java - 如何使用 gradle 下载/使用已发布的工件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62035560/

相关文章:

java - 如何模拟 HttpSession 并将其作为参数传递给与 session 一起使用的方法?

android - 重新排序 Gradle 默认任务

android - build.gradle 上的 JTS Topology Suite 1.15.0

java - 迭代遍历树以查找大小

java - Android 5 至 6 问题 : External Storage Path File. Exists() 已损坏/正在寻找替代方案

gradle - SCP的Gradle ant任务不会从gradle.properties中选择属性

java - Gradle 构建因 TransformException 失败

maven - 应用Gradle Dependency-Check插件

java - Spring-Boot Cloug Config Client 无法 Autowiring

java - 我可以用一个全新的项目覆盖 Git 存储库吗?