Show List

Common Configurations

Here are some common configurations/customizations in the build.gradle file.

Source file location

Conventionally the source code needs to be placed in the src/main/java subdirectory and the test classes need to be placed in src/main/test sub directory. For some reason if it is different the location can be provided in the build file. For example if the source folder is "src", test folder is "test" and are present in the root folder
sourceSets {
    main {
         java {
            srcDirs = ['src']
         }
    }

    test {
        java {
            srcDirs = ['test']
        }
    }
}
Here is an example where we have sources in multiple directories
sourceSets {
    main {
         java {
            srcDirs = ['src', 'thirdParty/src/main/java']
         }
    }

    test {
        java {
            srcDirs = ['test']
        }
    }
}

Java Version

Be default Gradle will compile the java code based on the JVM. If there multiple versions available compile version can be set as below:
compileJava {
    options.release = 7
}

Customize Output Jar 

Jar file name can be customized as below:
jar {
    archiveBaseName = 'gs-gradle'
    archiveVersion =  '0.1.0'
}
Jar manifest properties can be updated as below:
jar {
  manifest {
   attributes 'Main-Class': 'com.demo.app.Main'
  }
}

Custom Tasks and Dependent Tasks

Here is an example:
tasks.register('hello') {
    doLast {
        println 'Hello world!'
    }
}
tasks.register('intro') {
    dependsOn tasks.hello
    doLast {
        println "I'm inside intro"
    }
}
Running the "gradle intro" would run the hello command first and then intro
PS C:\Users\mail2\Downloads\gradle-project> gradle intro
Starting a Gradle Daemon, 1 incompatible and 1 stopped Daemons could not be reused, use --status for details

> Task :hello
Hello world!

> Task :intro
I'm inside intro

BUILD SUCCESSFUL in 6s
2 actionable tasks: 2 executed

Define Multiple Tasks

5.times { counter ->
    tasks.register("task$counter") {
        doLast {
            println "Inside task number $counter"
        }
    }
}
Running command "gradle task4"
PS C:\Users\mail2\Downloads\gradle-project> gradle task4

> Task :task4
Inside task number 4

BUILD SUCCESSFUL in 1s
1 actionable task: 1 executed

Extending Tasks

tasks.register('number') {
    doLast {
        println 'Hello Two'
    }
}
tasks.named('number') {
    doFirst {
        println 'Hello One'
    }
}
tasks.named('number') {
    doLast {
        println 'Hello Three'
    }
}
tasks.named('number') {
    doLast {
        println 'Hello Four'
    }
}
Running "gradle number" would produce following result:
PS C:\Users\mail2\Downloads\gradle-project> gradle number

> Task :number
Hello One
Hello Two
Hello Three
Hello Four

BUILD SUCCESSFUL in 1s

Defaults Tasks

Default tasks are executed when task is not specified in the gradle command.
defaultTasks 'task2', 'task3'

5.times { counter ->
    tasks.register("task$counter") {
        doLast {
            println "Inside task number $counter"
        }
    }
}
Running the gradle command without providing the task name
PS C:\Users\mail2\Downloads\gradle-project> gradle       

> Task :task2
Inside task number 2

> Task :task3
Inside task number 3

Using Multiple Repos for Dependencies

repositories {
    maven ("https://repository-achartengine.forge.myorg.com/repo/")
    jcenter {
        url "http://jcenter.bintray.com/"
    }
    ivy {
        url "http://repo.myorg.com/repo"
    }
}

    Leave a Comment


  • captcha text