90

我是 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')
}

现在它起作用了。

谢谢。

4

5 回答 5

58

那是因为你没有任何publications. 该artifactory-publish插件适用于maven-publish插件和上传publications

如果您更喜欢使用旧的 maven 插件,则需要artifactory插件,而不是artifactory-publish.

查看官方文档的“使用 Gradle”页面中的概述部分。

于 2014-03-12T14:06:00.973 回答
11

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

publishing {
    publications {
        mavenJava(MavenPublication) {
            // from components.java
            artifact file("path/jar-1.0.0.jar")
        }
    }
}
于 2016-03-17T11:47:04.713 回答
8

你需要插件:

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
于 2018-07-14T13:49:47.647 回答
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
        }
    }
}
于 2018-11-16T19:34:14.867 回答
0

这就是我在我的 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)
            }
        }
    }
}

于 2022-02-18T08:47:29.103 回答