Java, Maven, & Gradle: OS Setup & Project-Level Usage
Software engineer and entrepreneur based in San Francisco.
Software engineer and entrepreneur based in San Francisco.
Before diving into project-specific setups, you'll generally need a Java Development Kit (JDK) installed on your system. Optionally, you might install Maven and/or Gradle globally, though for most project work, their respective project-level wrappers (mvnw
, gradlew
) are also used.
The JDK is essential for compiling and running Java applications. It includes the Java Runtime Environment (JRE) and development tools like the Java compiler (javac
).
While project wrappers (mvnw
, gradlew
) are preferred for project work, you might want global installations for bootstrapping new projects or for tools that expect them.
Global (OS-level) installations of Maven/Gradle are for convenience or bootstrapping. For actual project development and builds, always prefer using the project's Maven Wrapper (./mvnw
) or Gradle Wrapper (./gradlew
). This ensures everyone uses the same build tool version, leading to reproducible builds. The wrappers will download the correct version if needed.
Ensure different projects can use different Java versions without conflict.
IDE settings control development. CLI consistency benefits from scripted JAVA_HOME
or tools like SDKMAN!.
Maven is a powerful build automation tool that uses a Project Object Model (POM) file (pom.xml
) to manage a project's build, reporting, and documentation. Using the Maven Wrapper (mvnw
) is crucial for project-specific versioning.
The Maven Wrapper (mvnw
for Unix-like systems, mvnw.cmd
for Windows) ensures that a project uses a consistent version of Maven without requiring a global installation. It downloads the specified Maven version if it's not already available.
# In project root, using a globally installed mvn (if available)
mvn -N io.takari:maven:wrapper -Dmaven=3.9.6 # Specify desired Maven version
.mvn/wrapper/maven-wrapper.properties
, which specifies the Maven version to use.# .mvn/wrapper/maven-wrapper.properties
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.6/apache-maven-3.9.6-bin.zip
# ... other properties
./mvnw
(or mvnw.cmd
) instead of a global mvn
for project tasks.pom.xml
defines Java compatibility for compilation:<!-- pom.xml -->
<properties>
<java.version>17</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
JAVA_HOME
(ideally set per-project) is compatible with these settings.Gradle is another popular build automation tool, known for its flexibility and performance, especially with Groovy or Kotlin for build scripts (build.gradle
or build.gradle.kts
). The Gradle Wrapper (gradlew
) is essential.
The Gradle Wrapper (gradlew
for Unix-like systems, gradlew.bat
for Windows) allows a project to build with a specific version of Gradle without requiring a global Gradle installation. It downloads the declared Gradle version if necessary.
gradle
installed):# In project root
gradle wrapper --gradle-version 8.7 # Specify desired Gradle version
gradle/wrapper/gradle-wrapper.properties
.# gradle/wrapper/gradle-wrapper.properties
distributionUrl=https://services.gradle.org/distributions/gradle-8.7-bin.zip
# ... other properties
./gradlew
(or gradlew.bat
) for project tasks.build.gradle
(Groovy) or build.gradle.kts
(Kotlin) file defines Java compatibility:// build.gradle (Groovy DSL)
java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
JAVA_HOME
is compatible. Gradle also supports Java toolchains for more fine-grained control over the JDK/JRE used by Gradle tasks, independent of JAVA_HOME
.Dependencies are external libraries your project relies on. They are declared in pom.xml
(Maven) or build.gradle(.kts)
(Gradle).
IntelliJ IDEA and VS Code (with Java extensions) often provide UI features to detect and update dependencies.