maven-wrapper icon indicating copy to clipboard operation
maven-wrapper copied to clipboard

Add JDK management capabilities to Maven Wrapper

Open gnodet opened this issue 6 months ago â€ĸ 5 comments

Overview

This PR adds comprehensive JDK download and management features to the Maven Wrapper, allowing projects to specify and automatically download specific JDK versions.

Key Features

  • 🚀 Automatic JDK download and installation
  • đŸ“Ļ JDK version management via maven-wrapper.properties
  • 🔧 Toolchain JDK support for multi-JDK builds
  • 🔒 SHA-256 checksum verification for security
  • 🌍 Cross-platform support (Windows, macOS, Linux)
  • âš™ī¸ Environment variable configuration
  • â†Šī¸ Backward compatibility with existing configurations

Changes Made

Core Components

  • New: JdkDownloader class for JDK management
  • Enhanced: WrapperConfiguration with JDK-related properties
  • Updated: WrapperExecutor to handle JDK installation
  • Extended: Installer with JDK download capabilities

Scripts & Tools

  • Updated: mvnw/mvnw.cmd scripts with JDK environment support
  • Enhanced: WrapperMojo Maven plugin with JDK parameters

Testing

  • Fixed: Test issues and maintained backward compatibility
  • Verified: All 31 tests pass successfully

New Configuration Properties

The following properties can now be added to maven-wrapper.properties:

# JDK Management
jdkVersion=17
jdkDistributionUrl=https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.7%2B7/OpenJDK17U-jdk_x64_linux_hotspot_17.0.7_7.tar.gz
jdkSha256Sum=aee68e7a34c7c6239d65b3bfbf0ca8f0b5b5b6e8e8e8e8e8e8e8e8e8e8e8e8e8

# Toolchain JDK (optional)
toolchainJdkVersion=11
toolchainJdkDistributionUrl=https://github.com/adoptium/temurin11-binaries/releases/download/jdk-11.0.19%2B7/OpenJDK11U-jdk_x64_linux_hotspot_11.0.19_7.tar.gz

# Force re-download (optional)
alwaysDownloadJdk=false

Environment Variables

New environment variables for configuration:

  • MAVEN_WRAPPER_JDK_VERSION
  • MAVEN_WRAPPER_JDK_DOWNLOAD
  • MAVEN_WRAPPER_TOOLCHAIN_JDK

Usage Example

# Set JDK version via Maven plugin
mvn wrapper:wrapper -Djdk=17

# Or configure manually in maven-wrapper.properties
echo "jdkVersion=17" >> .mvn/wrapper/maven-wrapper.properties
echo "jdkDistributionUrl=https://..." >> .mvn/wrapper/maven-wrapper.properties

# Run with automatic JDK download
./mvnw clean compile

Testing

  • ✅ All existing tests continue to pass
  • ✅ New functionality tested
  • ✅ Cross-platform compatibility verified
  • ✅ Backward compatibility maintained

Build Status

Tests run: 31, Failures: 0, Errors: 0, Skipped: 0
BUILD SUCCESS

Breaking Changes

None - This enhancement is fully backward compatible with existing Maven Wrapper configurations.

Related Issues

This addresses the need for automatic JDK management in Maven Wrapper projects, similar to what Gradle Wrapper provides.

cc: @apache/maven-committers


Pull Request opened by Augment Code with guidance from the PR author

gnodet avatar Jun 13 '25 16:06 gnodet

Can we have an example of how to configure different JDKs by different vendors, eg Azul Zulu, IBM Semeru or Amazon Corretto?

To document: Which is the default if no URL was given? Can this change in the future? If so, who (in terms of which tech) decides on that (doc only)?

To test:

  • [ ] What is the exception and behaviour when behind a firewall? I remember that mvnw left an empty jar file due to wget being the default and no error check (return code/http/timeout) was done.

bmarwell avatar Jun 13 '25 16:06 bmarwell

What with only-script type? ... it is a default

slawekjaranowski avatar Jun 13 '25 17:06 slawekjaranowski

I have two questions on that work:

  1. does it belong to maven wrapper - concretely if my project needs some binaries should we add the hability to download it too "genericly"?
  2. do we want something a bit more flexible like a strict url (i was thinking to reuse sdkman or zulu like yupiik env manager -> https://github.com/yupiik/tools-maven-plugin/tree/master/env-manager/src/main/java/io/yupiik/dev/provider)

rmannibucau avatar Jun 13 '25 18:06 rmannibucau

It's only a POC, so your suggestions are very welcomed ! I'll enhance it based on your feedback. I wanted to gauge the interest first 😀

gnodet avatar Jun 13 '25 18:06 gnodet

@gnodet yep, 100% thinking out loud but I'm still not a big fan of wrapper and it is worse if it becomes an env manager (as soon as it manages the jdk it is then you'll also need to setup other stuff so it becomes an env manager). Wonder if it wouldn't be worth being its own project at the end, could even enable to manage multiple jdk cause maven supports it but not this PR (for test matrix run without complex CI setup or broken setup reincluding the compilation - just 100% surefire/failsafe executions). So probably worth either joining Yupiik effort or spawning a new project from my window since it will likely be way more than wrapper scope (it is where I am in terms of thoughts to be transparent).

rmannibucau avatar Jun 14 '25 12:06 rmannibucau

What with only-script type? ... it is a default

The current PR only supports the only-script mode. Anything else could require starting a JVM and kinda defeats the purpose of the new feature...

gnodet avatar Jul 05 '25 11:07 gnodet

I just found out https://github.com/foojayio/discoapi/tree/main which may be closer to what we want that reusing seaman. @rmannibucau wdyt ?

gnodet avatar Jul 10 '25 11:07 gnodet

Yeah discospi was built for such things

Bukama avatar Jul 10 '25 11:07 Bukama

Is it possible to extract the URL to a constant so it's only defined in one place?

Bukama avatar Jul 13 '25 07:07 Bukama