feat: added support to pom.xml

This commit is contained in:
AUGMELO 2023-01-08 19:25:29 +00:00
parent e11351903a
commit e1a751b5d3
3 changed files with 133 additions and 5 deletions

View File

@ -337,3 +337,48 @@ jobs:
- name: Verify Java
run: bash __tests__/verify-java.sh "11.0.2" "${{ steps.setup-java.outputs.path }}"
shell: bash
setup-java-version-from-pom-spring-boot-specification:
name: ${{ matrix.distribution }} version from file 'openjdk64-11.0.2' - ${{ 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 .java-version file
shell: bash
run: |
echo "<?xml version="1.0" encoding="UTF-8"?>" > pom.xml
echo "<project>" >> pom.xml
echo " <modelVersion>4.0.0</modelVersion>" >> pom.xml
echo " <groupId>com.test</groupId>" >> pom.xml
echo " <artifactId>Test</artifactId>" >> pom.xml
echo " <version>0.0.1-SNAPSHOT</version>" >> pom.xml
echo " <name>Test</name>" >> pom.xml
echo " <properties>" >> pom.xml
echo " <java.version>11</java.version>" >> pom.xml
echo " </properties>" >> pom.xml
echo " <dependencies>" >> pom.xml
echo " </dependencies>" >> pom.xml
echo " <build>" >> pom.xml
echo " <plugins>" >> pom.xml
echo " <plugin>" >> pom.xml
echo " <groupId>org.springframework.boot</groupId>" >> pom.xml
echo " <artifactId>spring-boot-maven-plugin</artifactId>" >> pom.xml
echo " </plugin>" >> pom.xml
echo " </plugins>" >> pom.xml
echo " </build>" >> pom.xml
echo "</project>" >> 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 "11.0.2" "${{ steps.setup-java.outputs.path }}"
shell: bash

View File

@ -47,7 +47,7 @@ async function run() {
.toString()
.trim();
const version = getVersionFromFileContent(content, distributionName);
const version = getVersionFromFileContent(versionFile, content, distributionName);
core.debug(`Parsed version from file '${version}'`);
if (!version) {

View File

@ -101,13 +101,19 @@ export function isCacheFeatureAvailable(): boolean {
}
export function getVersionFromFileContent(
fileName: string,
content: string,
distributionName: string
): string | null {
const javaVersionRegExp = /(?<version>(?<=(^|\s|\-))(\d+\S*))(\s|$)/;
const fileContent = content.match(javaVersionRegExp)?.groups?.version
? (content.match(javaVersionRegExp)?.groups?.version as string)
: '';
let fileContent = null;
if (fileName.includes('.java-version')) {
fileContent = parseJavaVersionFile(content)
} else if (fileName.includes('pom.xml')){
fileContent = parsePomXmlFile(content)
} else {
throw new Error(`File ${fileName} not supported, files supported: '.java-version' and 'pom.xml'`)
}
if (!fileContent) {
return null;
}
@ -133,7 +139,84 @@ export function getVersionFromFileContent(
return version.toString();
}
function parseJavaVersionFile(content: string): string | null {
const javaVersionRegExp = /(?<version>(?<=(^|\s|\-))(\d+\S*))(\s|$)/;
const fileContent = content.match(javaVersionRegExp)?.groups?.version
? (content.match(javaVersionRegExp)?.groups?.version as string)
: '';
if (!fileContent) {
return null;
}
return fileContent
}
function parsePomXmlFile(content: string): string | null {
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(content, "text/xml");
const versionDefinitionTypes = [
getByMavenCompilerSource,
getByMavenCompilerRelease,
getBySpringBootSpecification,
]
const versionFound = versionDefinitionTypes.filter(function(definitionType){
const version = definitionType(xmlDoc)
return version !== null
})
return versionFound?.at(0)?.toString() ?? null;
}
function getByMavenCompilerSource(xmlDoc: Document): string | null {
const possibleTags = [
"maven.compiler.source",
"source"
]
const tagFound = possibleTags.filter(function(tag) {
const version = getVersionByTagName(xmlDoc, tag)
return version !== null
})
return tagFound?.at(0)?.toString() ?? null
}
function getByMavenCompilerRelease(xmlDoc: Document): string | null {
const possibleTags = [
"maven.compiler.release",
"release"
]
const tagFound = possibleTags.filter(function(tag) {
const version = getVersionByTagName(xmlDoc, tag)
return version !== null
})
return tagFound?.at(0)?.toString() ?? null
}
function getBySpringBootSpecification(xmlDoc: Document): string | null {
return getVersionByTagName(xmlDoc, "java.version")
}
function getVersionByTagName(xmlDoc: Document, tagName: string): string | null {
const element = xmlDoc.getElementsByTagName(tagName)
if (element.length < 1) {
return null
}
return element[0].textContent
}
// 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;
}