15 October 2020

Gradle cheat sheet

 Despite my preference for Maven, here it is a quick list of gradle (6.x) commands:

In gradle 7 has removed several tasks, and replaced by implementation, runtimeOnly, testImplementation, and testRuntimeOnly.

├── gradle
│ └── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── lib
 ├── build.gradle
 └── src
 ├── main
 │ └── java
 │ └── demo
 │ └── Library.java
 └── test
 └── java
 └── demo
 └── LibraryTest.java

1. basic build

gradle clean build

gradle assemble

gradle test

gradle init

2. eclipse java14 plugin

It has issues with wrapped gradle for older gradle (6.2--) gradle-wrapper.properties

Caused by: java.lang.NoClassDefFoundError: Could not initialize class org.codehaus.groovy.vmplugin.v7.Java7

plugins {
 id 'eclipse'
}

gradle cleanEclipse eclipse

3. tasks (list of possible task to execute)

./gradlew tasks

4. springboot 

gradle bootRun

5. version

./gradlew -version

6. sonarqube

plugins {
 id "org.sonarqube" version "3.0"
}

sonarqube {
 properties {
 property "sonar.sourceEncoding", "UTF-8"
 }
}

gradle sonarqube

7. debug/stacktrace

--stacktrace
--debug
--warning-mode=(all,fail,none,summary)

8. publish local maven

gradle publishToMavenLocal

available with:

apply plugin: 'maven-publish'

Another approach:

apply plugin: "maven"
group = "mygroupid"
version = "1.0.0"

repositories {
   mavenLocal()
}


$ gradle clean build install

 

9. skip test 

gradle build -x test

 

10. jar building

gradle jar

jar {
 manifest {
 attributes "Main-Class": "com.baeldung.fatjar.Application"
 }
 from {
 configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
 }
}


11. lombok

dependencies {
    compileOnly 'org.projectlombok:lombok:1.18.20'
    annotationProcessor 'org.projectlombok:lombok:1.18.20'
    
    testCompileOnly 'org.projectlombok:lombok:1.18.20'
    testAnnotationProcessor 'org.projectlombok:lombok:1.18.20'
}

12. dependencies

gradle dependencies
 

My Blog List

Blog Archive

Disclaimer

The views expressed on this blog are my own and do not necessarily reflect the views of Oracle.