【问题标题】:Upload artifact to Artifactory using Gradle使用 Gradle 将工件上传到 Artifactory
【发布时间】:2014-04-16 15:27:10
【问题描述】:

我是 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 {
   publications ('mavenJava')
}

现在可以了。

谢谢。

【问题讨论】:

  • 感谢您提出非常有用的问题和更新。一条对我有帮助的注释:defaults 实际上在 artifactory.publish 内部,而不仅仅是在根 artifactory 任务中。
  • 我在博客中总结了这一点:buransky.com/scala/…
  • 当我尝试它时,我得到:Error:(x, 0) Could not find property 'java' on SoftwareComponentInternal set. 你能发布完整的脚本吗?
  • 我正在编写上面的代码来上传我放在我的 gradle 文件夹 (gradle/sample.jar) 中的 jar,我执行并看到只有构建信息被上传。我有2个疑问。首先,我们在哪里指定要上传哪个 jar,我们还没有在任何地方指定路径。其次,如果我在 artifaction.publish 中编写默认部分,我会收到错误 Error:(82, 0) Extension of type 'PublishingExtension' does not exist。当前注册的扩展类型:[DefaultExtraPropertiesExtension, DefaultArtifactPublicationSet_Decorated..任何解决方案??
  • 只要我们有apply plugin: 'maven',就会生成pom文件,如果我们有'apply plugin: 'maven-publish',就会发布jar文件。而且我不必拥有 publishing.publications.mavenJava(MavenPublication) { from components.java } 。但是你必须有default {publications ('mavenJava'); publishConfigs('archives', 'published') }

标签: gradle build.gradle artifactory publish maven-publish


【解决方案1】:

那是因为您没有任何publicationsartifactory-publish pluginmaven-publish plugin 一起使用并上传 publications

如果您更喜欢使用the old maven plugin,则需要artifactory plugin,而不是artifactory-publish

看看官方文档的Overview part in "Working with Gradle" page

【讨论】:

  • 您好 JBrauch 感谢您的回复。我已将缺少的部分添加到帖子中,以帮助其他人遇到同样的问题。
  • 我希望来自 artifactory 的人过来......因为在文档中没有提到 maven-publish。感谢@JBaruch 的帮助! jfrog.com/confluence/display/RTF/…
  • 你可以认为我是“艺术工厂的人” :) Here's 官方文档中的解释。将其添加到答案中。
  • @JBaruch 仍然没有两个链接页面提到maven-publish。我同意@Ryan 的观点,这会有所帮助。但是在这里感谢您的回答
  • 当前文档示例使用完全过时和弃用的 Android Studio gradle 插件 v0.9,最新的是 v1.5.0。当然,示例不适用于此版本:/
【解决方案2】:

我得到了这个工作。我实际上使用的是已经创建的 jar,所以我使用下面的代码来指定要上传的 jar:

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

【讨论】:

    【解决方案3】:

    你需要插件:

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

    构建项目并从工件中检索 jar:

    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 {
        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
    

    【讨论】:

      【解决方案4】:

      这就是使用命令 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
              }
          }
      }
      

      【讨论】:

        【解决方案5】:

        这就是我在我的 Android 库中使用 Kotlin DSL (build.gradle.kts) 的方式:

        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)
                    }
                }
            }
        }
        
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-06-11
          • 1970-01-01
          • 2016-11-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-01-24
          相关资源
          最近更新 更多