Cheats or Frequently used

Gradle Command Description
gradle projects List Gradle projects
gradle tasks –all List all of the tasks that can be run
gradle dependencies Like mvn depeendency:tree
gradle clean build Like mvn clean install
gradle helmInstall Build docker image and deploy it to K8S (custom task)
gradle publishToMavenLocal Copy artifacts (jar) to maven .m2/repository directory

How multi-projects basics

Every gradle project (or subproject) has a build.gradle file. They can optionally have a gradle.properties file. See https://docs.gradle.org/current/userguide/multi_project_builds.html for the Gradle documentation on a Multi-Project Layout.

Gradle calls the arguments tasks. Some built in tasks are: help, projects, tasks, properties. Other tasks are defined in the build.gradle file or via gradle plugins that are included.

In this example, there are three directories the root directory and two sub-projects.

Root Project
  - settings.gradle
  - build.gradle
  - gradle.properties
  + subProject1
    - build.gradle
  + subProject2
    - build.gradle

The settings.gradle file defines the project name and sub-projects.

rootProject.name = "Pats sample Gradle multi-directory layout"
project.group = "com.steranka.play"
version = "0.0.1"

include 'subProject1'
include 'subProject2'

When gradle clean is run, it runs in the Root and in the two subProjects. Gradle executes clean on the root and every subproject, because clean is a task that exists in every project and follows Gradle’s task execution model The clean task is a lifecycle task defined in the base plugin, which is applied automatically in Java projects.

On the other hand, gradle dependencies only runs on the Root directory. This is because the dependencies task is not a global task—it belongs to each project separately.

Testing

How can I run a single test in Gradle (from command line)?

# Specific Test File (all tests in this file are run)
gradle subproject:test --tests com.example.play.MySampleTest

# Specific Test doItTest() File (only this exact test is run)
gradle subproject:test --tests com.example.play.MySampleTest#doItTest

# Run all Tests whos filename matches 'Base*Test'
gradle subproject:test --tests "Base*Test"

How can I skip over tests

By appending -x test you can skip over tests.

gradle build -x test

Where is log file output

./gradlew test --info
./gradlew test --debug

Will show you the output that you normally don’t see

ls -l subproject/build/test-results/test
less subproject/build/test-results/test/TEST-com.example.play.MySampleTest.xml
  <system.out><![CDATA[
... this is where you'll see the log output
]]></system-out>
  <system-err><![CDATA[]]><system-err>

The XML file contains the log output including a stack trace if one exists. It also has the success/failure of the test.

Tip: If you just want everything (logs, test outputs) nicely in console while developing, add this once to your build.gradle:

test {
    testLogging {
        events "passed", "skipped", "failed", "standard_out", "standard_error"
        showStandardStreams = true
    }
}

How do I setup publishToMavenLocal? How does it work?

KWS: gradle equivalent to mvn install

Add the following to your build.gradle file

plugins {
    id 'maven-publish'
}

publishing {
    publications {
        myLibrary(MavenPublication) {
            from components.java
            // This uses artifactId, groupId, version to be set 

            // Add additional artifacts if needed
            artifact sourcesJar
            artifact javadocJar
            
            // Add POM customizations if needed
            pom {
                name = 'Your Project Name'
                description = 'Description of artifact being published'
            }
        }
    }
    repositories {
        mavenLocal()
    }
}

After this is installed, you can use it

./gradlew publishToMavenLocal
./gradlew publishMyLibraryPublicationToMavenLocal

The plugin creates publish<PublicationName>PublicationTo<RepositoryName> so that is why the 2nd version of the command is shown

If you don’t override anything inside the MavenPublication, the maven-plugin uses the project-level values. Gradle will:

  • Use project.group as the groupId
  • Use project.name as the artifactId
  • Use project.version as the version

NOTE: One way to set the groupId (aka project.group) is using a gradle.properties file such as

publishRepository = release
javaVersion = 17
groupId = com.steranka.play
// group = 

Misc

How can I run a command in build.gradle?

task buildDockerImage(type:Exec, dependsOn: docker) {
    commandLine "docker"
    args = ["build"]
}

task pushDockerImage(type:Exec, dependsOn: docker) {
    commandLine "docker"
    args = ["push", "harbor.example.com/myproj/myapp:latest"]
}

How can I define a task to be used in any directory?

The file $HOME/.gradle/init.gradle is processed on every gradle command. By defining a task in this file, you can use the task anywhere in any gradle project.

allprojects {
    task.register('hello-world'){
        println "Hello from directory ${project.projectDir}"
    }
    
    task.register('list-configurations'){
        configurations
            // .findAll { it.canBeResolved || it.canBeConsumed }
             .findAll { it.canBeResolved }
             .each { println "$it.name} - ${it.description? 'No description'}" } 
    }
    
//     repositories {
//         mavenLocal()
//         maven { url "https://nexus.example.com/repository/public" }
//     }
//     
//     gradle.beforeProject {
//         System.setProperty("com.redhat.fips", "false")
//     }
}

<
Previous Post
When copy/paste has invisible characters, how do I get rid of them?
>
Next Post
How can I find files updated in last 5 minutes