Skip to content

Instantly share code, notes, and snippets.

@vegaasen
Last active December 1, 2021 12:10
Show Gist options
  • Save vegaasen/bf818edaeeef5f862120634bc47cd984 to your computer and use it in GitHub Desktop.
Save vegaasen/bf818edaeeef5f862120634bc47cd984 to your computer and use it in GitHub Desktop.
Gradle Cheat Sheet

Gradle Cheat Sheet

Include details on which test that failed?

By default, gradle doesn't provide much information on which test that failed. This is something you can easily configure. Add the following configuration:

  test {
    useJUnitPlatform()
    afterTest(
      KotlinClosure2({ desc: TestDescriptor, result: TestResult ->
        if(result.resultType == TestResult.ResultType.FAILURE) {
          println("❌ Test failed 👉 ${desc.className} > ${desc.displayName}")
          result.exception?.printStackTrace()
        }
      })
    )
  }

Skip all withType<>{} during build/tests in Idea?

If you have a fancy configuration with Gradle with sections with things being triggering on types or stages, there might be a possibility that whenever you trigger (something) in Intellij, things may be über-slow as everything needs to happen before your (whatever) starts up. To "fix" this, do the following:

Just pop in to

settings > Build (..) > Build Tools > Gradle

and do the following:

  • Build/Test using => IntelliJ
  • Run tests using => IntelliJ

Done ✅👌

Versions? Use this!

object Version {
  const val javaVersion = "16"
  const val kotlinVersion = "1.4.32"
  const val testContainers = "1.15.3"
}

Used by e.g:

..
  implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${Version.kotlinVersion}")
  implementation("org.jetbrains.kotlin:kotlin-reflect:${Version.kotlinVersion}")
..

Assemble git hash

Add this to the top of your class.

import java.io.ByteArrayOutputStream
...
class Git {
  val gitAbbriv = "git rev-parse --verify --short HEAD".runCommand()
  val gitBranch = "git rev-parse --abbrev-ref HEAD".runCommand()
  val gitUsername = "git config user.name".runCommand()
  val gitLastCommitDateTime = "git log -1 --format=%cd".runCommand()
  private fun String.runCommand(workingDir: File = project.rootDir): String =
    ByteArrayOutputStream().use {
      try {
        project.exec {
          this.workingDir = workingDir
          commandLine = this@runCommand.split(" ")
          standardOutput = it
        }
        println("Executed command: $this")
        String(it.toByteArray()).trim()
      } catch (e: Exception) {
        println("Command failed: $this. Using fallback (-)")
        "-"
      }
    }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment