使用Gradle将构件上传到Artifactory

95

我是Gradle和Artifactory的新手,希望能将一个JAR文件上传到Artifactory。

这是我的build.gradle文件:

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'maven'
apply plugin: 'artifactory-publish'

groupId = 'myGroup'
version = '1.0'
def artifactId = projectDir.name
def versionNumber = version

artifactory {
    contextUrl = 'http://path.to.artifactory' // base artifactory url
    publish {
        repository {
            repoKey = 'libs-releases'   // Artifactory repository key to publish to
            username = 'publisher'      // publisher user name
            password = '********'       // publisher password
            maven = true
        }
    }
}
    
artifactoryPublish { 
    dependsOn jar
}
在运行artifactoryPublish任务后,构建成功如下所示:
> gradle artifactoryPublish  --stacktrace
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar
:artifactoryPublish
Deploying build info to: http://path.to.artifactory/api/build
    
BUILD SUCCESSFUL
    
Total time: 7.387 secs

然而,除了构建信息之外,没有发送任何内容到Artifactory。

非常感谢任何帮助。

编辑:

正如JBaruch所提到的,我已经添加了以下内容:

apply plugin: 'maven-publish'

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
        }
    }
}

并将defaults部分设置为artifactory任务:

defaults {
   publications ('mavenJava')
}

现在它可以工作了。

谢谢。


3
感谢您提供非常有帮助的问题和更新。有一个提示对我很有用:defaults 实际上应该放在 artifactory.publish 内部,而不仅仅是根 artifactory 任务中。 - Ryan Walls
4
我已经在博客中总结了这个内容:http://buransky.com/scala/publish-jar-artifact-using-gradle-to-artifactory/ - Rado Buransky
1
当我尝试运行它时,出现以下错误:Error:(x, 0) Could not find property 'java' on SoftwareComponentInternal set. 请问您能否提供完整的脚本? - Nimrod Dayan
我正在编写上面的代码,用于上传我放置在gradle文件夹(gradle/sample.jar)中的jar文件。我执行后发现只有构建信息被上传了。我有两个疑问。首先,我们在哪里指定要上传哪个jar文件,我们没有在任何地方指定路径。其次,如果我在artifaction.publish中编写默认部分,我会收到错误消息Error:(82, 0) Extension of type 'PublishingExtension' does not exist. Currently registered extension types: [DefaultExtraPropertiesExtension, DefaultArtifactPublicationSet_Decorated.. 有什么解决办法吗? - sver
只要我们有apply plugin: 'maven',pom文件就会生成;如果有apply plugin: 'maven-publish',jar文件就会发布。我不需要publishing.publications.mavenJava(MavenPublication) { from components.java }。但是你需要使用default { publications ('mavenJava'); publishConfigs('archives', 'published') } - ravikanth
5个回答

60

5
嗨,JBrauch,谢谢您的回复。我已经添加了缺失的部分到帖子中,以帮助其他遇到相同问题的人。 - tuncaysenturk
7
我希望有来自Artifactory的人过来...因为文档中完全没有提到'maven-publish'。感谢@JBaruch的帮助!http://www.jfrog.com/confluence/display/RTF/Gradle+1.6+Publishing+Artifactory+Plugin - Ryan Walls
1
你可以把我看作是“艺术工厂”的一员 :) 这是官方文档中的解释。将其添加到答案中。 - JBaruch
5
@JBaruch仍然没有提到maven-publish的两个链接页面中的任何一个。我同意@Ryan的看法,它会很有帮助。但感谢您在这里的回答。 - Heinrich Filter
上传构件到Artifactory,使用gradle的uploadArchives还是Artifactory的artifactoryPublish更好?http://stackoverflow.com/questions/36403467/what-is-the-preferred-way-to-upload-artifacts-to-artifactory - OhadR
显示剩余3条评论

11

我搞定了。实际上我正在使用一个已经创建好的jar包,所以我正在使用下面的代码来指定要上传的jar包:

publishing {
    publications {
        mavenJava(MavenPublication) {
            // from components.java
            artifact file("path/jar-1.0.0.jar")
        }
    }
}

10

您需要插件:

apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'maven'
apply plugin: 'maven-publish'
apply plugin: 'com.jfrog.artifactory'

构建项目并从artifactory检索jars:

buildscript {
    repositories {
        maven {
            url 'http://[IP]:[PORT]/artifactory/gradle-dev'
            credentials {
                username = "${artifactory_user}"
                password = "${artifactory_password}"
            }
        }
        mavenCentral()
    }
    dependencies { classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.5.4" }
}

repositories {
    mavenCentral()
    mavenLocal()
}

Artifactory配置:

artifactory {
    contextUrl = "${artifactory_contextUrl}"
    publish {
        repository {
            repoKey = 'gradle-dev-local'
            username = "${artifactory_user}"
            password = "${artifactory_password}"
            maven = true
        }
        defaults {
            publications('mavenJava')
        }
        publishBuildInfo = true
        publishArtifacts = true
        publishPom = true
    }
    resolve {
        repository {
            repoKey = 'gradle-dev'
            username = "${artifactory_user}"
            password = "${artifactory_password}"
            maven = true

        }
    }
}

并且用于发布:

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
        }
    }
}

gradle.properties

artifactory_user=publisher
artifactory_password=*****
artifactory_contextUrl=http://IP:PORT/artifactory

所以一切都很简单。如果您想上传您的jar文件:
gradle artifactoryPublish

6
这是我使用命令gradle clean build publish成功的方法。
apply plugin: 'maven-publish'
apply plugin: 'groovy'
apply plugin: 'java'
apply plugin: 'maven'

group = 'com.mine'
version = '1.0.1-SNAPSHOT'

repositories{
    mavenCentral()
}

dependencies {
    compile gradleApi()
    compile localGroovy()
    compile 'com.google.guava:guava:27.0-jre'
    testCompile 'junit:junit:4.12'
    //compile 'org.apache.commons:commons-lang3:3.8.1'
}

publishing {
    repositories {
        maven {
            url = 'https://artifactory.mine.net/artifactory/my-snapshots-maven'
            credentials {
                username 'user'
                password 'password'
            }
        }
    }
    publications{
        mavenJava(MavenPublication) {
            from components.java
        }
    }
}

0

这是我使用Kotlin DSL(build.gradle.kts)为我的Android库完成的方法:

plugins {
    id("maven-publish")
    // ...
}

lateinit var sourcesArtifact: PublishArtifact
lateinit var javadocArtifact: PublishArtifact
tasks {
    val sourcesJar by creating(Jar::class) {
        archiveClassifier.set("sources")
        from(android.sourceSets["main"].java.srcDirs)
    }

    val dokkaHtml by getting(org.jetbrains.dokka.gradle.DokkaTask::class)

    val javadocJar by creating(Jar::class) {
        dependsOn(dokkaHtml)
        archiveClassifier.set("javadoc")
        from(dokkaHtml.outputDirectory)
    }

    artifacts {
        sourcesArtifact = archives(sourcesJar)
        javadocArtifact = archives(javadocJar)
    }
}

afterEvaluate {
    publishing {
        repositories {
            maven {
                name = "GitHubPackages"
                url = uri("https://maven.pkg.github.com/mahozad/android-pie-chart")
                credentials {
                    username = project.properties["github.username"] as String? ?: System.getenv("GITHUB_ACTOR") ?: ""
                    password = project.properties["github.token"] as String? ?: System.getenv("GITHUB_TOKEN") ?: ""
                }
            }
        }
        publications {
            create<MavenPublication>("Release") {
                // Applies the component for the release build variant (two artifacts: the aar and the sources)
                from(components["release"])
                artifact(sourcesArtifact)
                artifact(javadocArtifact)
            }
        }
    }
}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接