Monday, April 8, 2013

How to Publish Artifacts with Gradle

In this blog, I'm going to show you how to publish and consume artifacts with Gradle using both Ivy and Maven plugins.

  • Publishing via Ivy (old way)
  • apply plugin: "java"
    
    group = "test"
    version = "0.0.1"
    
    repositories {
        mavenCentral()
    }
    
    uploadArchives {
        repositories {
            ivy {
                url "file://home/foo/tmp/ivy"
            }
        }
    }
    
    dependencies {
        // just an example to test transitive dependencies
        compile "ch.qos.logback:logback-classic:1.0.11"
        compile "ch.qos.logback:logback-core:1.0.11"
    }
    
  • Publishing via Ivy (new way)
  • apply plugin: "java"
    apply plugin: "ivy-publish"
    
    group = "test"
    version = "0.0.1"
    
    repositories {
        mavenCentral()
    }
    
    publishing {
        publications {
            ivyJava(IvyPublication) {
                from components.java
            }
        }
        repositories {
            ivy {
                url "file://home/foo/tmp/ivy"
            }
        }
    }
    
    dependencies {
        // just an example to test transitive dependencies
        compile "ch.qos.logback:logback-classic:1.0.11"
        compile "ch.qos.logback:logback-core:1.0.11"
    }
    
  • Publishing via Maven (old way)
  • apply plugin: "java"
    apply plugin: "maven"
    
    group = "test"
    version = "0.0.1"
    
    repositories {
        mavenCentral()
    }
    
    uploadArchives {
        repositories {
            mavenDeployer {
                repository(url: "file:///home/foo/tmp/maven")
            }
        }
    }
    
    dependencies {
        // just an example to test transitive dependencies
        compile "ch.qos.logback:logback-classic:1.0.11"
        compile "ch.qos.logback:logback-core:1.0.11"
    }
    
  • Publishing via Maven (new way)
  • apply plugin: "java"
    apply plugin: "maven-publish"
    
    group = "test"
    version = "0.0.1"
    
    repositories {
        mavenCentral()
    }
    
    publishing {
        publications {
            mavenJava(MavenPublication) {
                from components.java
            }
       }
        repositories {
            maven {
                url "file://home/foo/tmp/maven"
            }
        }
    }
    
    dependencies {
        // just an example to test transitive dependencies
        compile "ch.qos.logback:logback-classic:1.0.11"
        compile "ch.qos.logback:logback-core:1.0.11"
    }
    
  • Consuming via Ivy
  • apply plugin: "java"
    
    version = "0.0.1"
    
    repositories {
        mavenCentral()
        ivy { url "file://home/foo/tmp/ivy" }
    }
    
    dependencies {
        compile "test:upload-artifact:0.0.1"
    }
    
  • Consuming via Maven
  • apply plugin: "java"
    
    version = "0.0.1"
    
    repositories {
        mavenCentral()
        maven { url "file://home/foo/tmp/maven" }
    }
    
    dependencies {
        compile "test:upload-artifact:0.0.1"
    }
    

    No comments:

    Post a Comment