Create a new open-source Java project using the Gradle build tool

How to set up a brand new Java project using Gradle

Photo: Building
“Photo by Danist Soh on Unsplash

When building a house, you should start with a strong foundation. Similarly, creating a new open-source project requires a certain level of organization!

rootProject.name = "props"

pluginManagement {
repositories {
mavenCentral()
gradlePluginPortal()
}
}
group = "sh.props"
version = project.version
version=0.2.0-SNAPSHOT
include("java-props-core")
plugins {
`java-library`
}

java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(JavaVersion.VERSION_11.toString()))
}
}

// additionally also allow incremental builds, speeding up build speed
tasks.compileJava {
options.isIncremental = true
}
java {
withJavadocJar()
withSourcesJar()
}

tasks.create<Zip>("docZip") {
archiveFileName.set("doc.zip")
from("doc")
}
tasks.test {
// Use JUnit Platform for unit tests.
useJUnitPlatform()

// limit heap usage to 1G (can we tweaked later)
// https://docs.gradle.org/7.2/userguide/java_testing.html#sec:test_execution
maxHeapSize = "1G"
}
dependencies {
testImplementation(libs.junit.jupiter.api)
testRuntimeOnly(libs.junit.jupiter.engine)
}
// enable the incubating feature
enableFeaturePreview("VERSION_CATALOGS")

dependencyResolutionManagement {
versionCatalogs {
create("libs") {
// define the version once
version("junit", "5.7.2")

// then create aliases to each component, referencing the version above
alias("junit-jupiter-api").to("org.junit.jupiter", "junit-jupiter-api").versionRef("junit")
alias("junit-jupiter-engine").to("org.junit.jupiter", "junit-jupiter-engine").versionRef("junit")
}
}
}
pluginManagement {
plugins {
id("com.diffplug.spotless").version("5.14.3")
}
}
spotless {
// generic formatting for miscellaneous files
format("misc") {
target("*.gradle", "*.md", ".gitignore")

trimTrailingWhitespace()
indentWithSpaces()
endWithNewline()
}

// chose the Google java formatter, version 1.9
java {
googleJavaFormat("1.9").aosp()

// and apply a license header
licenseHeaderFile(rootProject.file("props.license.kt"))
}
}
/*
MIT License

Copyright (c) $YEAR Mihai Bojin

Permission is hereby granted...
*/
dependencyResolutionManagement {
versionCatalogs {
create("libs") {
// make a global version available
version("checkstyle", "9.0")
}
}
}
plugins {
checkstyle
}

checkstyle {
// will use the version declared in the catalog
toolVersion = libs.versions.checkstyle.get()
}
pluginManagement {
plugins {
id("net.ltgt.errorprone").version("2.0.2")
}
}
import net.ltgt.gradle.errorprone.errorprone
plugins {
id("net.ltgt.errorprone")
}

dependencies {
errorprone(libs.errorprone.core)
errorprone(libs.nullaway)
}

// hook up the checker to the compilation target
tasks.withType<JavaCompile>().configureEach {
options.errorprone.disableWarningsInGeneratedCode.set(true)
}

--

--

Software Engineer at heart, Manager by day, Indie Hacker at night. Writing about DevOps, Software engineering, and Cloud computing. Opinions my own.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mihai Bojin

Software Engineer at heart, Manager by day, Indie Hacker at night. Writing about DevOps, Software engineering, and Cloud computing. Opinions my own.