Skip to content

Commit 354bd3f

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 0312b7d commit 354bd3f

2 files changed

Lines changed: 95 additions & 4 deletions

File tree

.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: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@
7272
<maven.compiler.source>1.8</maven.compiler.source>
7373
<maven.compiler.target>1.8</maven.compiler.target>
7474
<maven.compiler.showDeprecation>true</maven.compiler.showDeprecation>
75+
<!-- Maven itself runs on Java 17+, but compilation and tests can still use an older toolchain JDK. -->
76+
<hc.build.toolchain.version>${maven.compiler.source}</hc.build.toolchain.version>
7577
<conscrypt.version>2.5.2</conscrypt.version>
7678
<junit.version>5.14.3</junit.version>
7779
<junit.migrationsupport.version>5.0.0</junit.migrationsupport.version>
@@ -229,6 +231,27 @@
229231
</execution>
230232
</executions>
231233
</plugin>
234+
<plugin>
235+
<groupId>org.apache.maven.plugins</groupId>
236+
<artifactId>maven-enforcer-plugin</artifactId>
237+
<version>3.5.0</version>
238+
<executions>
239+
<execution>
240+
<id>enforce-java-runtime</id>
241+
<goals>
242+
<goal>enforce</goal>
243+
</goals>
244+
<configuration>
245+
<rules>
246+
<requireJavaVersion>
247+
<version>[17,)</version>
248+
<message>Apache HttpComponents Core requires Java 17 or newer to run Maven. Use toolchains to compile and test with older JDKs.</message>
249+
</requireJavaVersion>
250+
</rules>
251+
</configuration>
252+
</execution>
253+
</executions>
254+
</plugin>
232255
<plugin>
233256
<groupId>com.github.siom79.japicmp</groupId>
234257
<artifactId>japicmp-maven-plugin</artifactId>
@@ -295,6 +318,40 @@
295318
</plugins>
296319
</build>
297320

321+
<profiles>
322+
<profile>
323+
<id>use-toolchains</id>
324+
<activation>
325+
<file>
326+
<exists>${user.home}/.m2/toolchains.xml</exists>
327+
</file>
328+
</activation>
329+
<build>
330+
<plugins>
331+
<plugin>
332+
<groupId>org.apache.maven.plugins</groupId>
333+
<artifactId>maven-toolchains-plugin</artifactId>
334+
<version>3.2.0</version>
335+
<configuration combine.self="override">
336+
<toolchains>
337+
<jdk>
338+
<version>${hc.build.toolchain.version}</version>
339+
</jdk>
340+
</toolchains>
341+
</configuration>
342+
<executions combine.self="override">
343+
<execution>
344+
<goals>
345+
<goal>toolchain</goal>
346+
</goals>
347+
</execution>
348+
</executions>
349+
</plugin>
350+
</plugins>
351+
</build>
352+
</profile>
353+
</profiles>
354+
298355
<reporting>
299356
<plugins>
300357

@@ -391,4 +448,4 @@
391448
</plugins>
392449
</reporting>
393450

394-
</project>
451+
</project>

0 commit comments

Comments
 (0)