Skip to content

Commit 63c5ebd

Browse files
committed
Require Java 17+ for Maven runtime
* Decouple the Maven runtime JDK from the compile/test toolchain by introducing `hc.build.toolchain.version` and overriding the inherited `use-toolchains` profile to select the toolchain from that property instead of `maven.compiler.source`. * Require Java 17 or newer to run Maven, but continue compiling and testing against Java 8, 11, 17, and 21 toolchains. * Update CI to install JDK 17 for the Maven process and the matrix JDK as the toolchain, generate `toolchains.xml`, and pass `hc.build.toolchain.version` explicitly instead of disabling toolchains and running everything on the host JDK. This does not drop support for Java 8 consumers: published artifacts still target Java 8 and the build can still compile and run tests on Java 8 through toolchains.
1 parent 323251c commit 63c5ebd

File tree

2 files changed

+91
-3
lines changed

2 files changed

+91
-3
lines changed

.github/workflows/maven.yml

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ permissions:
2222

2323
jobs:
2424
build:
25-
2625
runs-on: ${{ matrix.os }}
2726
strategy:
2827
matrix:
@@ -43,10 +42,45 @@ jobs:
4342
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
4443
restore-keys: |
4544
${{ runner.os }}-maven-
46-
- name: Set up JDK ${{ matrix.java }}
45+
- name: Select JDK toolchain version
46+
run: |
47+
case '${{ matrix.java }}' in
48+
8)
49+
echo 'HC_BUILD_TOOLCHAIN_VERSION=1.8' >> "$GITHUB_ENV"
50+
;;
51+
*)
52+
echo 'HC_BUILD_TOOLCHAIN_VERSION=${{ matrix.java }}' >> "$GITHUB_ENV"
53+
;;
54+
esac
55+
- name: Set up toolchain JDK ${{ matrix.java }}
56+
id: setup-java-toolchain
4757
uses: actions/setup-java@v5
4858
with:
4959
distribution: 'temurin'
5060
java-version: ${{ matrix.java }}
61+
- name: Set up JDK 17 for Maven
62+
uses: actions/setup-java@v5
63+
with:
64+
distribution: 'temurin'
65+
java-version: '17'
66+
- name: Configure Maven toolchains
67+
env:
68+
TOOLCHAIN_JAVA_HOME: ${{ steps.setup-java-toolchain.outputs.path }}
69+
run: |
70+
mkdir -p "${HOME}/.m2"
71+
cat > "${HOME}/.m2/toolchains.xml" <<EOF
72+
<?xml version="1.0" encoding="UTF-8"?>
73+
<toolchains>
74+
<toolchain>
75+
<type>jdk</type>
76+
<provides>
77+
<version>${HC_BUILD_TOOLCHAIN_VERSION}</version>
78+
</provides>
79+
<configuration>
80+
<jdkHome>${TOOLCHAIN_JAVA_HOME}</jdkHome>
81+
</configuration>
82+
</toolchain>
83+
</toolchains>
84+
EOF
5185
- name: Build with Maven
52-
run: ./mvnw -V --file pom.xml --no-transfer-progress -DtrimStackTrace=false -Djunit.jupiter.execution.parallel.enabled=false -P-use-toolchains,docker
86+
run: ./mvnw -V --file pom.xml --no-transfer-progress -DtrimStackTrace=false -Djunit.jupiter.execution.parallel.enabled=false -Dhc.build.toolchain.version="${HC_BUILD_TOOLCHAIN_VERSION}" -Pdocker

pom.xml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
<properties>
6363
<maven.compiler.source>1.8</maven.compiler.source>
6464
<maven.compiler.target>1.8</maven.compiler.target>
65+
<!-- Maven itself runs on Java 17+, but compilation and tests can still use an older toolchain JDK. -->
66+
<hc.build.toolchain.version>${maven.compiler.source}</hc.build.toolchain.version>
6567
<httpcore.version>5.5-alpha1</httpcore.version>
6668
<log4j.version>2.25.3</log4j.version>
6769
<brotli4j.version>1.20.0</brotli4j.version>
@@ -340,6 +342,27 @@
340342
</execution>
341343
</executions>
342344
</plugin>
345+
<plugin>
346+
<groupId>org.apache.maven.plugins</groupId>
347+
<artifactId>maven-enforcer-plugin</artifactId>
348+
<version>3.5.0</version>
349+
<executions>
350+
<execution>
351+
<id>enforce-java-runtime</id>
352+
<goals>
353+
<goal>enforce</goal>
354+
</goals>
355+
<configuration>
356+
<rules>
357+
<requireJavaVersion>
358+
<version>[17,)</version>
359+
<message>Apache HttpComponents Client requires Java 17 or newer to run Maven. Use toolchains to compile and test with older JDKs.</message>
360+
</requireJavaVersion>
361+
</rules>
362+
</configuration>
363+
</execution>
364+
</executions>
365+
</plugin>
343366
<plugin>
344367
<groupId>com.github.siom79.japicmp</groupId>
345368
<artifactId>japicmp-maven-plugin</artifactId>
@@ -432,6 +455,37 @@
432455
</plugins>
433456
</build>
434457
</profile>
458+
<profile>
459+
<id>use-toolchains</id>
460+
<activation>
461+
<file>
462+
<exists>${user.home}/.m2/toolchains.xml</exists>
463+
</file>
464+
</activation>
465+
<build>
466+
<plugins>
467+
<plugin>
468+
<groupId>org.apache.maven.plugins</groupId>
469+
<artifactId>maven-toolchains-plugin</artifactId>
470+
<version>3.2.0</version>
471+
<configuration combine.self="override">
472+
<toolchains>
473+
<jdk>
474+
<version>${hc.build.toolchain.version}</version>
475+
</jdk>
476+
</toolchains>
477+
</configuration>
478+
<executions combine.self="override">
479+
<execution>
480+
<goals>
481+
<goal>toolchain</goal>
482+
</goals>
483+
</execution>
484+
</executions>
485+
</plugin>
486+
</plugins>
487+
</build>
488+
</profile>
435489
</profiles>
436490

437491

0 commit comments

Comments
 (0)