94

I am a newbie to Gradle and Artifactory and I want to upload a JAR file to Artifactory.

Here is my build.gradle file:

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
}

After running the artifactoryPublish task, the build is successful as shown below:

> 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

However, there is nothing sent to Artifactory except the build info.

Any help will be much appreciated.

Edit:

As JBaruch mentioned, I've added the following:

apply plugin: 'maven-publish'

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

and defaults section to artifactory task:

defaults {
   publications ('mavenJava')
}

Now it works.

Thanks.

Mahozad
  • 18,032
  • 13
  • 118
  • 133
tuncaysenturk
  • 1,067
  • 1
  • 8
  • 7
  • 3
    Thanks for the very helpful question and update. One note that helped me: `defaults` actually goes inside `artifactory.publish`, not just in root `artifactory` task. – Ryan Walls May 28 '14 at 00:29
  • 4
    I have summarized this in a blog: http://buransky.com/scala/publish-jar-artifact-using-gradle-to-artifactory/ – Rado Buransky Aug 16 '14 at 14:54
  • 1
    when I try it I get: `Error:(x, 0) Could not find property 'java' on SoftwareComponentInternal set.` Could you please post the full script? – Nimrod Dayan Mar 10 '15 at 10:11
  • I am writing the above code for uploading a jar I have placed in my gradle folder (gradle/sample.jar) , I execute and see that only build information is getting uploaded. I have 2 doubts. Firstly, where are we specifying which jar to upload, we havent specified the path anywhere. Secondly, If I write the default section in artifaction.publish, I get error Error:(82, 0) Extension of type 'PublishingExtension' does not exist. Currently registered extension types: [DefaultExtraPropertiesExtension, DefaultArtifactPublicationSet_Decorated.. Any solution?? – sver Mar 16 '16 at 05:46
  • as long as we have `apply plugin: 'maven'` the pom file is generated and if we have `'apply plugin: 'maven-publish'` the jar file is published. And I didn't have to have `publishing.publications.mavenJava(MavenPublication) { from components.java }` . However you got to have `default {publications ('mavenJava'); publishConfigs('archives', 'published') }` – ravikanth Aug 18 '16 at 14:32

5 Answers5

60

That's because you don't have any publications. The artifactory-publish plugin works with maven-publish plugin and uploads publications.

If you prefer working with the old maven plugin, you need artifactory plugin, not artifactory-publish.

Take a look at the Overview part in "Working with Gradle" page of the official docs.

Daniel A.A. Pelsmaeker
  • 47,471
  • 20
  • 111
  • 157
JBaruch
  • 22,610
  • 5
  • 62
  • 90
  • 5
    Hi JBrauch Thanks for your response. I've added the lacking parts to the post in order to help others have same issue. – tuncaysenturk Mar 12 '14 at 14:31
  • 7
    I hope someone from artifactory comes by... because there is zero mention of `maven-publish` in the documentation. Thanks for the help @JBaruch! http://www.jfrog.com/confluence/display/RTF/Gradle+1.6+Publishing+Artifactory+Plugin – Ryan Walls May 28 '14 at 00:19
  • 1
    You can consider me "someone from artfactory" :) [Here's](http://www.jfrog.com/confluence/display/RTF/Working+with+Gradle) the explanation in the official documentation. Adding it to the answer. – JBaruch May 28 '14 at 06:29
  • 5
    @JBaruch still neither of the two linked pages mentions `maven-publish`. I agree with @Ryan that it would be helpful. But thanks for the answer here – Heinrich Filter Sep 23 '14 at 12:48
  • Current doc examples use Android Studio gradle plugin v0.9 which is completely outdated and deprecated, latest is v1.5.0. Of course, the examples do not work with this version : / – Rémy DAVID Jan 25 '16 at 13:28
  • What is the preferred way to upload artifacts to Artifactory? using gradle's uploadArchives, or Artifactory's artifactoryPublish? http://stackoverflow.com/questions/36403467/what-is-the-preferred-way-to-upload-artifacts-to-artifactory – OhadR Apr 04 '16 at 13:02
  • if you want to take advantage of Artifactory plugin you should you `artifactoryPublish`. – JBaruch Apr 04 '16 at 17:50
  • Why we can't just use only artifactory-plugin but both? I mean artifactory-plugin has also it's fields to define publishing information, right? – stdout Apr 10 '18 at 16:28
11

I got this working. I was actually using an already created jar so I am using the below code to specify my jar that is to be uploaded:

publishing {
    publications {
        mavenJava(MavenPublication) {
            // from components.java
            artifact file("path/jar-1.0.0.jar")
        }
    }
}
nyg
  • 2,380
  • 3
  • 25
  • 40
sver
  • 866
  • 1
  • 19
  • 44
9

You need plugins :

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

to build project and retrieve jars from artifactory:

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 configs:

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

        }
    }
}

and for publishing:

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

gradle.properties

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

So everything is just simple. If you want to upload your jar:

gradle artifactoryPublish
grep
  • 5,465
  • 12
  • 60
  • 112
6

This is what worked for me with the command 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
        }
    }
}
gary69
  • 3,620
  • 6
  • 36
  • 50
0

This is how I did it with Kotlin DSL (build.gradle.kts) for my Android library:

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

Mahozad
  • 18,032
  • 13
  • 118
  • 133