使用Gradle将工件上载到Artifactory

时间:2014-03-12 13:04:34

标签: gradle publish artifactory

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

现在可行。

由于

5 个答案:

答案 0 :(得分:46)

那是因为你没有publicationsartifactory-publish pluginmaven-publish plugin一起使用,并上传publications

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

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

答案 1 :(得分:8)

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

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

答案 2 :(得分:1)

您需要插件:

apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'maven'
apply plugin: 'maven-publish'
apply plugin: 'com.jfrog.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 {
    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

答案 3 :(得分:1)

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

答案 4 :(得分:0)

也可以使用publishConfigs代替明确定义发布:

artifactory {
    contextUrl = artifactoryUrl
    publish {
        repository {
            repoKey = 'libs-releases'
            username = artifactory_user
            password = artifactory_password
        }
        defaults {
            publishConfigs('archives')
        }
    }
}

它会自动获取JAR和POM文件。如果您创建其他JAR(例如,用于源代码或javadoc),我们要做的就是将它们添加到配置archives中:

task sourcesJar(type: Jar, dependsOn: classes) {
    classifier = 'sources'
    from sourceSets.main.allSource
}

task javadocJar(type: Jar, dependsOn: javadoc) {
    classifier = 'javadoc'
    from javadoc.destinationDir
}

artifacts {
    archives sourcesJar
    archives javadocJar
}