Gradle - nimrody/knowledgebase GitHub Wiki

  • Gradle for devops

  • Running with a proxy

    $ ./gradlew -Dhttps.proxyHost=XXXXX -Dhttp.proxyHost=XXXXX -Dhttp.proxyPort=XXXXX
    -Dhttps.proxyPort=XXXXX -Dhttp.proxyUser=XXXXX -Dhttp.proxyPassword=XXXXX
    -Dhttps.proxyUser=XXXXX -Dhttps.proxyPassword=XXXXX
    build

  • Running a single test or group of tests

    sh ./gradlew :policy:test --tests "ReportsServletTestConcur*"

  • Run with the application plugin in debug mode: gradle run --debug-jvm

  • Setup default JVM args applicationDefaultJvmArgs = ["-Dgreeting.language=en"]

  • Setup run arguments with run.args "aa", "bb", ...

  • gradle --daemon to run in daemon mode (faster)

  • gradle --help for other command line options

  • Gradle - create new java project gradle init --type java-library

  • Use settings.gradle to make gradle search other directories for modules. E.g.,

    include '../rep/server/Tenserver/policy' findProject(':../rep/server/Tenserver/policy')?.name = 'policy'

  • More than one application with the application plugin

    task groupEventsByUsers(type: CreateStartScripts) { mainClassName = 'periodic.GroupEventsByUsers' applicationName = 'group' outputDir = new File(project.buildDir, 'scripts') classpath = jar.outputs.files + project.configurations.runtime }

    applicationDistribution.into('bin') { from(groupEventsByUsers) fileMode = 0755 }

  • Application plugin - set jvm defaults and command line arguments

    apply plugin: 'application'

    mainClassName='analysis.MainClass' applicationDefaultJvmArgs=['-Xmx2G']

    run.args "../../databaseSchema/server.local.config"

    // add the public directory (js/html/css) to the distribution applicationDistribution.from('public') { into 'public' }

  • Run a java program (without the application plugin)

    task run(type: JavaExec) { main = "analysis.MainVideoAnalysis" classpath = project.sourceSets.main.runtimeClasspath //args "../../databaseSchema/server.local.config", "com.rge.sport5", "full_20151228" args "../../databaseSchema/server.local.config", "com.rge.sport5", "full_20160114" jvmArgs "-Xms5G", "-Xmx5G" }

  • Add a local jar file as a dependency: compile files('xxx.jar')

  • Execute external command: (the following sets the version code according to the git version count):

    def cmd = 'git rev-list HEAD --first-parent --count' def gitVersion = cmd.execute().text.trim().toInteger()

    android { defaultConfig { versionCode gitVersion } }

Shadow plugin for fat jars

  • Shadow plugin documentation

    // Use "gradle shadow" to generate a fat-jar

    buildscript { repositories { mavenCentral() } }

    plugins { id 'com.github.johnrengelman.shadow' version '1.2.4' }

    group 'tensea' version '1.0-SNAPSHOT'

    apply plugin: 'java' apply plugin: 'application'

    sourceCompatibility = 1.8 mainClassName = 'Obfuscate'

    shadowJar { baseName = 'obfuscate' classifier = null version = null }

Speedup

  • Enable parallel builds

    echo 'org.gradle.parallel=true' >> ~/.gradle/gradle.properties

  • Enable the gradle daemon

    echo 'org.gradle.daemon=true' >> ~/.gradle/gradle.properties

  • Enable configuration on demand

    echo 'org.gradle.configureondemand=true' >> ~/.gradle/gradle.properties