Skip to content

Gradle

Ok, I skipped from make to ant to scons to maven to gradle, but I have to say it is probably the way to go now. Gradle is not XML which is a delight by itself, but it also borrows the good stuff from other tools.

installation

Gradle requires java JRE version7 or higher, so I assume that is available.

sdkman

Since you rarely get the latest version on linux, this is the preferred way of installing the exactly version you like. Download the binary package from : https://gradle.org/install/#manually_

reinstall
1
2
3
apt-get remove gradle # 1.5 is of no use at-all
mkdir /opt/gradle
unzip -d /opt/gradle gradle-4.3.1-bin.zip

Then add this to your startup file :

startup file
export GRADLE_HOME=/opt/gradle/gradle-4.3.1/
export PATH=$PATH:$GRADLE_HOME/bin

zero installation

If you have projects like from android studio that come with a gradle wrapper you don't need to install anything up front. Just run the gradlew script from the project directory

wrapper
./gradlew

introduction

I needed this multiple times, so let's point to the best tutorial for this : gradle_tut It has a lot of links to the original docs, but explains between the lines.

cheatsheet

In lack of any useful :

List all tasks :

list tasks
gradle tasks

Run the default task quietly

silent
gradle -q

proxy

Add these to gradle.properties

proxy
1
2
3
4
5
systemProp.https.proxyHost=10.10.1.13
systemProp.https.proxyPort=3128
systemProp.http.proxyHost=10.10.1.13
systemProp.http.proxyPort=3128
systemProp.http.nonProxyHosts=localhost

template

I found this on some forum :

fatJar
jar {
    manifest {
        attributes(
        'Main-Class': 'my.project.main',
        )
    }
}

task fatJar(type: Jar) {
    manifest.from jar.manifest
    classifier = 'all'
    from {
        configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) }
    } {
            exclude "META-INF/*.SF"
            exclude "META-INF/*.DSA"
            exclude "META-INF/*.RSA"
    }
    with jar
}
To add this to the standard assemble or build task, add:

artifacts {
    archives fatJar
}

This does work, the current build.gradle for Stocker is now :

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

mainClassName = "org.klopt.tools.Stocker"

defaultTasks 'clean', 'fatJar', 'tags'

version = '1.0'
sourceCompatibility = 1.8

jar {
    manifest {
        attributes 'Implementation-Title' : 'Gradle Stocker',
        'Implementation-Version': version,
        'Main-Class' : 'org.klopt.tools.Stocker'
    }
}

//create a single Jar with all dependencies
task fatJar(type: Jar) {
    manifest.from jar.manifest
    baseName = project.name + '-all'
    from {
        configurations.compile.collect {
            it.isDirectory() ? it : zipTree(it)
        }
    } {
        exclude "META-INF/*.SF"
        exclude "META-INF/*.DSA"
        exclude "META-INF/*.RSA"
    }
    with jar
}

task tags {

exec {
    executable= "ctags";
    args = ["-R", "--extra=+q", "src"];
}
}

troubleshooting

Could not determine java version

when running gradlew :

./gradlew

FAILURE: Build failed with an exception.

* What went wrong:
Could not determine java version from '11.0.18'.

* Try:
Run with --info or --debug option to get more log output.

* Exception is:

This is purely because you wrapper script (gradlew) is running an older version. Look in the wrapper properties what version is used.

gradle/wrapper/gradle-wrapper.properties
#Fri Jan 24 09:36:43 CET 2020
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.4-bin.zip

So it is using gradlew 2.4, and if you check your current gradle version.

gradle -v
------------------------------------------------------------
Gradle 8.0.2
------------------------------------------------------------

Build time:   2023-03-03 16:41:37 UTC
Revision:     7d6581558e226a580d91d399f7dfb9e3095c2b1d

Kotlin:       1.8.10
Groovy:       3.0.13
Ant:          Apache Ant(TM) version 1.10.11 compiled on July 10 2021
JVM:          11.0.18 (Debian 11.0.18+10-post-Debian-1deb11u1)
OS:           Linux 5.10.0-21-amd64 amd64

So by all means just update the zip file :

distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip

Now when you run gradlew commands it will start downloading that version first.