Showing posts with label Gradle. Show all posts
Showing posts with label Gradle. Show all posts

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

    Sunday, December 30, 2012

    How to Setup JavaFX Project with Gradle

    apply plugin: "java"
    apply plugin: "eclipse"
    
    version = "0.1.0"
    
    sourceCompatibility = 1.7
    targetCompatibility = 1.7
    
    defaultTasks = ["clean", "jar"]
    
    def javafxLib = "jfxrt.jar"
    
    def getJavaFXPath(def javafxLib) {
        def javaHome = System.env["JAVA_HOME"]
        if (javaHome == null) {
            throw new RuntimeException("JAVA_HOME environment variable must be set")
        }
        def javafxrt = "jre" + File.separator + "lib" + File.separator + javafxLib
        return new File(javaHome, javafxrt).absolutePath
    }
    
    dependencies {
        compile files(getJavaFXPath(javafxLib))
    }
    
    jar {
        // When creating a fat JAR, make sure we exclude javafx runtime
        // from the fat JAR
        dependsOn configurations.runtime
        from {
            configurations.runtime.findAll { !it.name.contains(javafxLib) }.collect { 
                it.isDirectory() ? it : zipTree(it)
            }
        }
        from sourceSets.main.allJava
    
        // There's no need to explicitly specify the -classpath or -cp
        // since the classpath information is already stored in the MANIFEST.MF
        manifest {
            attributes "Main-Class": "javafxapp.JavaFXApp"
            attributes "Class-Path": getJavaFXPath(javafxLib)
        }
    }
    
    Now we can easily execute the generated JAR by calling
    java -jar <jar_file.jar>
    

    Thursday, December 27, 2012

    Getting Started with Gradle Multiproject

    Project structure:
    .
    ├── api
    │   └── src
    │       └── main
    │           └── java
    │               └── api
    │                   └── API.java
    ├── build.gradle
    ├── cli
    │   └── src
    │       └── main
    │           └── java
    │               └── cli
    │                   └── CLI.java
    ├── core
    │   └── src
    │       └── main
    │           └── java
    │               └── core
    │                   └── Core.java
    ├── gui
    │   └── src
    │       └── main
    │           └── java
    │               └── gui
    │                   └── GUI.java
    └── settings.gradle
    
    In this project, core depends on api, cli depends on core, and gui depends on core.

    settings.gradle

    include ":api", ":core", ":gui", ":cli"
    
    build.gradle
    def distDir = "dist"
    
    defaultTasks = ["clean", "jar", "dist"]
    
    subprojects {
        apply plugin: "java"
        apply plugin: "eclipse"
        sourceCompatibility = 1.7
        targetCompatibility = 1.7
        version = "0.1.0"
    }
    
    project(":core") {
        dependencies {
            compile project(":api")
        }
    }
    
    project(":gui") {
        dependencies {
            compile project(":core")
        }
    }
    
    project(":cli") {
        dependencies {
            compile project(":core")
        }
    }
    
    task clean << {
        delete distDir
    }
    
    task dist << {
        def d = mkdir(distDir)
        subprojects.each { subproject ->
            subproject.tasks.jar.each { archiveTask ->
                copy {
                    from archiveTask.archivePath
                    into d
                }
            }
        }
    }
    
    The build.gradle shown above has a dist task that will pull all the generated jars into dist directory.

    How to Include Source Files into JAR in Gradle

    Project structure:
    ├── build.gradle
    └── src
        └── main
            └── java
                └── test
                    └── Test.java
    
    build.gradle
    apply plugin: "java"
    
    defaultTasks = ["clean", "jar"]
    
    jar {
        from sourceSets.main.allJava
    }
    

    Tuesday, November 1, 2011

    How to Create a JAR with Dependencies in Gradle

    Creating a single executable JAR/fat JAR can be pretty useful. We can do something like this.
    java -jar foobar.jar
    
    In Maven, we need to write a pretty verbose XML to do that as shown in my previous blog. In Gradle, this task can be easily achieved in just few lines as shown in the example below.
    apply plugin: 'java'
    
    sourceCompatibility = 1.6
    targetCompatibility = 1.6
    
    defaultTasks = ['clean', 'jar']
    
    dependencies {
       compile 'org.slf4j:slf4j-api:1.6.2'
       compile 'ch.qos.logback:logback-core:0.9.30'
       compile 'ch.qos.logback:logback-classic:0.9.30'
       compile 'javax.servlet:servlet-api:2.5'
       compile 'org.eclipse.jetty.aggregate:jetty-all:7.5.4.v20111024' 
       
       testCompile 'junit:junit:4.+'
       testCompile 'org.mockito:mockito-core:1.8.5'
    }
    
    jar {
       from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
       manifest { attributes 'Main-Class': 'test.Main' }
    }
    

    Tuesday, March 8, 2011

    Getting Started with Gradle

    As a build tool, Gradle has a pretty steep learning curve. The official documentation is a good place to start.

    build.gradle
    apply plugin: 'java'
    apply plugin: 'groovy'
    apply plugin: 'eclipse'
    
    version = '1.0'
    
    repositories {
        mavenCentral()   
        flatDir name: 'localRepository', dirs: 'lib'
    }
    
    dependencies {
        runtime group: 'log4j', name: 'log4j', version: '1.2.+'  
        compile ':junit:4.0'
    }
    
    task hello << {
        println System.properties['msg']
        ant.echo('Hello')
    }
    
    compileJava.dependsOn(hello)
    

    gradle.properties
    systemProp.http.proxyHost=myhost
    systemProp.http.proxyPort=8080
    systemProp.http.proxyUser=userid
    systemProp.http.proxyPassword=password
    systemProp.msg
    

    Project structure
    gradle-project/src/main/java/MyJava.java 
                   src/main/resources/
                   src/main/groovy/MyGroovy.groovy
                   src/test/java/MyJavaTest.java
                   src/test/groovy/MyGroovy.grovy
                   src/test/resources/
                   build.gradle
                   gradle.properties   
                  /lib/junit-4.0.jar               
    

    Here, we use Java, Groovy, and Eclipse plugins. With Java plugin, the conventional project structure is defined as below.
    src/main/java
    src/main/resources
    src/test/java
    src/test/resources
    

    With Groovy plugin, the conventional project structure is defined as below.
    src/main/groovy
    src/main/resources
    src/test/groovy
    src/test/resources
    
    As you can see here, it's very easy to mix between Java and Groovy projects. Gradle also supports another language such as Scala.

    To generate Eclipse project:
    gradle eclipse
    To clean the generated Eclipse project
    gradle cleanEclipse

    To build the project:
    gradle build
    This will compile Java and Groovy files.

    To clean the project:
    gradle clean

    As we can see here, it's pretty easy create a custom hello task and make it as a dependency of an existing compileJavaTask as shown in the example above. Because Gradle is based on Groovy, we can leverage on Groovy programming language and Groovy out of the box supports Ant.

    To execute the individual task:
    gradle hello

    To view the list of available tasks:
    gradle tasks

    With Gradle, it's easy to mix dependency management between Maven and local repository as shown in the example above.

    There are still so many things that Gradle can provide, but hopefully this simple tutorial is sufficient to get started with Gradle.