From f5e27775fa693fb30602e87692cbf6b3542e0703 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tim=20Wei=C3=9Fenfels?=
<10491533+tim-we@users.noreply.github.com>
Date: Tue, 23 Apr 2024 15:51:22 +0200
Subject: [PATCH] Add support for maven-compiler-plugin configuration (#1)
---
.github/workflows/e2e-versions.yml | 44 ++++++++++++++++++++++++++
dist/cleanup/index.js | 48 +++++++++++++++++++++++++---
dist/setup/index.js | 48 +++++++++++++++++++++++++---
src/util.ts | 50 +++++++++++++++++++++++++++---
4 files changed, 178 insertions(+), 12 deletions(-)
diff --git a/.github/workflows/e2e-versions.yml b/.github/workflows/e2e-versions.yml
index 2e179e4..bf9b812 100644
--- a/.github/workflows/e2e-versions.yml
+++ b/.github/workflows/e2e-versions.yml
@@ -469,3 +469,47 @@ jobs:
- name: Verify Java
run: bash __tests__/verify-java.sh "8" "${{ steps.setup-java.outputs.path }}"
shell: bash
+
+ setup-java-version-from-pom-maven-compiler-plugin-configuration:
+ name: ${{ matrix.distribution }} version from pom.xml - ${{ matrix.os }}
+ runs-on: ${{ matrix.os }}
+ strategy:
+ fail-fast: false
+ matrix:
+ os: [macos-latest, windows-latest, ubuntu-latest]
+ distribution: ['adopt', 'zulu', 'liberica']
+ steps:
+ - name: Checkout
+ uses: actions/checkout@v3
+ - name: Create pom.xml file
+ shell: bash
+ run: |
+ echo "" > pom.xml
+ echo "" >> pom.xml
+ echo " 4.0.0" >> pom.xml
+ echo " foo.bar" >> pom.xml
+ echo " FooBar" >> pom.xml
+ echo " 0.0.1-SNAPSHOT" >> pom.xml
+ echo " " >> pom.xml
+ echo " " >> pom.xml
+ echo " " >> pom.xml
+ echo " org.apache.maven.plugins" >> pom.xml
+ echo " maven-compiler-plugin" >> pom.xml
+ echo " 3.10.1" >> pom.xml
+ echo " " >> pom.xml
+ echo " 1.8" >> pom.xml
+ echo " 1.8" >> pom.xml
+ echo " " >> pom.xml
+ echo " " >> pom.xml
+ echo " " >> pom.xml
+ echo " " >> pom.xml
+ echo "" >> pom.xml
+ - name: setup-java
+ uses: ./
+ id: setup-java
+ with:
+ distribution: ${{ matrix.distribution }}
+ java-version-file: 'pom.xml'
+ - name: Verify Java
+ run: bash __tests__/verify-java.sh "8" "${{ steps.setup-java.outputs.path }}"
+ shell: bash
diff --git a/dist/cleanup/index.js b/dist/cleanup/index.js
index c3ba9e3..799fbb7 100644
--- a/dist/cleanup/index.js
+++ b/dist/cleanup/index.js
@@ -103834,16 +103834,21 @@ function parseJavaVersionFile(content) {
return fileContent;
}
function parsePomXmlFile(xmlFileAsString) {
- const versionDefinitionTypes = [getByMavenCompilerSpecification, getBySpringBootSpecification];
- for (var definitionType of versionDefinitionTypes) {
- var version = definitionType(xmlbuilder2_1.create(xmlFileAsString));
+ const xmlDoc = xmlbuilder2_1.create(xmlFileAsString);
+ const versionDefinitionTypes = [
+ getByMavenProperties,
+ getBySpringBootSpecification,
+ getByMavenCompilerPluginConfig
+ ];
+ for (const definitionType of versionDefinitionTypes) {
+ const version = definitionType(xmlDoc);
if (version !== null) {
return version;
}
}
return null;
}
-function getByMavenCompilerSpecification(xmlDoc) {
+function getByMavenProperties(xmlDoc) {
const possibleTagsRegex = [
'maven.compiler.source',
'maven.compiler.release',
@@ -103869,6 +103874,41 @@ function getVersionByTagName(xmlDoc, tag) {
return null;
}
}
+function getByMavenCompilerPluginConfig(xmlDoc) {
+ var _a;
+ const source = xmlDoc.find(n => {
+ // Find node
+ if (n.node.nodeName !== "source") {
+ return false;
+ }
+ if (n.node.childNodes.length !== 1) {
+ return false;
+ }
+ // Must be within
+ if (n.up().node.nodeName !== "configuration") {
+ return false;
+ }
+ // Which must be inside
+ if (n.up().up().node.nodeName !== "plugin") {
+ return false;
+ }
+ // Make sure the plugin is maven-compiler-plugin
+ const isCompilerPlugin = n.up().up().some(c => {
+ if (c.node.nodeName !== "artifactId") {
+ return false;
+ }
+ if (c.node.childNodes.length !== 1) {
+ return false;
+ }
+ return c.first().toString() === "maven-compiler-plugin";
+ }, false, true);
+ if (!isCompilerPlugin) {
+ return false;
+ }
+ return true;
+ });
+ return (_a = source === null || source === void 0 ? void 0 : source.first().toString()) !== null && _a !== void 0 ? _a : null;
+}
// By convention, action expects version 8 in the format `8.*` instead of `1.8`
function avoidOldNotation(content) {
return content.startsWith('1.') ? content.substring(2) : content;
diff --git a/dist/setup/index.js b/dist/setup/index.js
index a2faa25..fbed6b0 100644
--- a/dist/setup/index.js
+++ b/dist/setup/index.js
@@ -105491,16 +105491,21 @@ function parseJavaVersionFile(content) {
return fileContent;
}
function parsePomXmlFile(xmlFileAsString) {
- const versionDefinitionTypes = [getByMavenCompilerSpecification, getBySpringBootSpecification];
- for (var definitionType of versionDefinitionTypes) {
- var version = definitionType(xmlbuilder2_1.create(xmlFileAsString));
+ const xmlDoc = xmlbuilder2_1.create(xmlFileAsString);
+ const versionDefinitionTypes = [
+ getByMavenProperties,
+ getBySpringBootSpecification,
+ getByMavenCompilerPluginConfig
+ ];
+ for (const definitionType of versionDefinitionTypes) {
+ const version = definitionType(xmlDoc);
if (version !== null) {
return version;
}
}
return null;
}
-function getByMavenCompilerSpecification(xmlDoc) {
+function getByMavenProperties(xmlDoc) {
const possibleTagsRegex = [
'maven.compiler.source',
'maven.compiler.release',
@@ -105526,6 +105531,41 @@ function getVersionByTagName(xmlDoc, tag) {
return null;
}
}
+function getByMavenCompilerPluginConfig(xmlDoc) {
+ var _a;
+ const source = xmlDoc.find(n => {
+ // Find node
+ if (n.node.nodeName !== "source") {
+ return false;
+ }
+ if (n.node.childNodes.length !== 1) {
+ return false;
+ }
+ // Must be within
+ if (n.up().node.nodeName !== "configuration") {
+ return false;
+ }
+ // Which must be inside
+ if (n.up().up().node.nodeName !== "plugin") {
+ return false;
+ }
+ // Make sure the plugin is maven-compiler-plugin
+ const isCompilerPlugin = n.up().up().some(c => {
+ if (c.node.nodeName !== "artifactId") {
+ return false;
+ }
+ if (c.node.childNodes.length !== 1) {
+ return false;
+ }
+ return c.first().toString() === "maven-compiler-plugin";
+ }, false, true);
+ if (!isCompilerPlugin) {
+ return false;
+ }
+ return true;
+ });
+ return (_a = source === null || source === void 0 ? void 0 : source.first().toString()) !== null && _a !== void 0 ? _a : null;
+}
// By convention, action expects version 8 in the format `8.*` instead of `1.8`
function avoidOldNotation(content) {
return content.startsWith('1.') ? content.substring(2) : content;
diff --git a/src/util.ts b/src/util.ts
index 3b6c0e9..08d2410 100644
--- a/src/util.ts
+++ b/src/util.ts
@@ -158,10 +158,15 @@ function parseJavaVersionFile(content: string): string | null {
}
function parsePomXmlFile(xmlFileAsString: string): string | null {
- const versionDefinitionTypes = [getByMavenCompilerSpecification, getBySpringBootSpecification];
+ const xmlDoc = create(xmlFileAsString);
+ const versionDefinitionTypes = [
+ getByMavenProperties,
+ getBySpringBootSpecification,
+ getByMavenCompilerPluginConfig
+ ];
- for (var definitionType of versionDefinitionTypes) {
- var version = definitionType(create(xmlFileAsString));
+ for (const definitionType of versionDefinitionTypes) {
+ const version = definitionType(xmlDoc);
if (version !== null) {
return version;
@@ -171,7 +176,7 @@ function parsePomXmlFile(xmlFileAsString: string): string | null {
return null;
}
-function getByMavenCompilerSpecification(xmlDoc: XMLBuilder): string | null {
+function getByMavenProperties(xmlDoc: XMLBuilder): string | null {
const possibleTagsRegex = [
'maven.compiler.source',
'maven.compiler.release',
@@ -204,6 +209,43 @@ function getVersionByTagName(xmlDoc: XMLBuilder, tag: string): string | null {
}
+function getByMavenCompilerPluginConfig(xmlDoc: XMLBuilder): string | null {
+ const source = xmlDoc.find(n => {
+ // Find node
+ if (n.node.nodeName !== "source") {
+ return false;
+ }
+ if (n.node.childNodes.length !== 1) {
+ return false;
+ }
+ // Must be within
+ if (n.up().node.nodeName !== "configuration") {
+ return false;
+ }
+ // Which must be inside
+ if (n.up().up().node.nodeName !== "plugin") {
+ return false;
+ }
+ // Make sure the plugin is maven-compiler-plugin
+ const isCompilerPlugin = n.up().up().some(c => {
+ if (c.node.nodeName !== "artifactId") {
+ return false;
+ }
+ if (c.node.childNodes.length !== 1) {
+ return false;
+ }
+ return c.first().toString() === "maven-compiler-plugin";
+ }, false, true);
+ if (!isCompilerPlugin) {
+ return false;
+ }
+
+ return true;
+ });
+
+ return source?.first().toString() ?? null;
+}
+
// By convention, action expects version 8 in the format `8.*` instead of `1.8`
function avoidOldNotation(content: string): string {
return content.startsWith('1.') ? content.substring(2) : content;