Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added support to pom.xml and build.gradle #441

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
feat: finished implementation and added e2e tests
augustomelo committed Apr 8, 2023
commit c2ea5167ffdc8297248c204184abe03ddf949ef9
186 changes: 180 additions & 6 deletions .github/workflows/e2e-versions.yml
Original file line number Diff line number Diff line change
@@ -339,22 +339,22 @@ jobs:
shell: bash

setup-java-version-from-pom-spring-boot-specification:
name: ${{ matrix.distribution }} version from file 'openjdk64-11.0.2' - ${{ matrix.os }}
name: ${{ matrix.distribution }} version from pom.xml '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' ]
distribution: ['adopt', 'zulu', 'liberica']
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Create .java-version file
- name: Create pom.xml file
shell: bash
run: |
echo "<?xml version="1.0" encoding="UTF-8"?>" > pom.xml
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" > pom.xml
echo "<project>" >> pom.xml
echo " <modelVersion>4.0.0</modelVersion>" >> 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
@@ -380,5 +380,179 @@ jobs:
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 }}"
run: bash __tests__/verify-java.sh "11" "${{ steps.setup-java.outputs.path }}"
shell: bash

setup-java-version-from-pom-maven-compiler-source-specification:
name: ${{ matrix.distribution }} version from pom.xml '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 pom.xml 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>foo.bar</groupId>" >> pom.xml
echo " <artifactId>FooBarr</artifactId>" >> pom.xml
echo " <version>0.0.1-SNAPSHOT</version>" >> pom.xml
echo " <properties>" >> pom.xml
echo " <maven.compiler.source>1.8</maven.compiler.source>" >> pom.xml
echo " <maven.compiler.target>1.8</maven.compiler.target>" >> pom.xml
echo " </properties>" >> pom.xml
echo " <build>" >> pom.xml
echo " <plugins>" >> pom.xml
echo " <plugin>" >> pom.xml
echo " <groupId>org.apache.maven.plugins</groupId>" >> pom.xml
echo " <artifactId>maven-compiler-plugin</artifactId>" >> pom.xml
echo " <version>3.10.1</version>" >> 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 "8" "${{ steps.setup-java.outputs.path }}"
shell: bash

setup-java-version-from-pom-maven-compiler-release-specification:
name: ${{ matrix.distribution }} version from pom.xml '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 pom.xml 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>foo.bar</groupId>" >> pom.xml
echo " <artifactId>FooBarr</artifactId>" >> pom.xml
echo " <version>0.0.1-SNAPSHOT</version>" >> pom.xml
echo " <properties>" >> pom.xml
echo " <maven.compiler.release>8</maven.compiler.release>" >> pom.xml
echo " </properties>" >> pom.xml
echo " <build>" >> pom.xml
echo " <plugins>" >> pom.xml
echo " <plugin>" >> pom.xml
echo " <groupId>org.apache.maven.plugins</groupId>" >> pom.xml
echo " <artifactId>maven-compiler-plugin</artifactId>" >> pom.xml
echo " <version>3.10.1</version>" >> 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 "8" "${{ steps.setup-java.outputs.path }}"
shell: bash

setup-java-version-from-pom-maven-compiler-configuration-source-specification:
name: ${{ matrix.distribution }} version from pom.xml '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 pom.xml 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>foo.bar</groupId>" >> pom.xml
echo " <artifactId>FooBarr</artifactId>" >> pom.xml
echo " <version>0.0.1-SNAPSHOT</version>" >> pom.xml
echo " <build>" >> pom.xml
echo " <plugins>" >> pom.xml
echo " <plugin>" >> pom.xml
echo " <groupId>org.apache.maven.plugins</groupId>" >> pom.xml
echo " <artifactId>maven-compiler-plugin</artifactId>" >> pom.xml
echo " <version>3.10.1</version>" >> pom.xml
echo " <configuration>" >> pom.xml
echo " <source>1.8</source>" >> pom.xml
echo " <target>1.8</target>" >> pom.xml
echo " </configuration>" >> 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 "8" "${{ steps.setup-java.outputs.path }}"
shell: bash

setup-java-version-from-pom-maven-compiler-configuration-release-specification:
name: ${{ matrix.distribution }} version from pom.xml '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 pom.xml 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>foo.bar</groupId>" >> pom.xml
echo " <artifactId>FooBarr</artifactId>" >> pom.xml
echo " <version>0.0.1-SNAPSHOT</version>" >> pom.xml
echo " <build>" >> pom.xml
echo " <plugins>" >> pom.xml
echo " <plugin>" >> pom.xml
echo " <groupId>org.apache.maven.plugins</groupId>" >> pom.xml
echo " <artifactId>maven-compiler-plugin</artifactId>" >> pom.xml
echo " <version>3.10.1</version>" >> pom.xml
echo " <configuration>" >> pom.xml
echo " <release>11</release>" >> pom.xml
echo " </configuration>" >> 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" "${{ steps.setup-java.outputs.path }}"
shell: bash
68,680 changes: 54,331 additions & 14,349 deletions dist/cleanup/index.js

Large diffs are not rendered by default.

4,927 changes: 4,919 additions & 8 deletions dist/setup/index.js

Large diffs are not rendered by default.

4,789 changes: 8 additions & 4,781 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -33,7 +33,8 @@
"@actions/io": "^1.0.2",
"@actions/tool-cache": "^1.6.1",
"semver": "^7.3.4",
"xmlbuilder2": "^2.4.0"
"xmlbuilder2": "^2.4.0",
"xpath": "0.0.32"
},
"devDependencies": {
"@types/jest": "^27.0.2",
97 changes: 44 additions & 53 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import os from 'os';
import os, { version } from 'os';
import path from 'path';
import * as fs from 'fs';
import * as semver from 'semver';
import * as cache from '@actions/cache';
import * as core from '@actions/core';

import { create as xmlCreate } from 'xmlbuilder2';
import { select } from 'xpath';
import * as tc from '@actions/tool-cache';
import { INPUT_JOB_STATUS, DISTRIBUTIONS_ONLY_MAJOR_VERSION } from './constants';
import { XMLBuilder } from 'xmlbuilder2/lib/interfaces';

export function getTempDir() {
let tempDirectory = process.env['RUNNER_TEMP'] || os.tmpdir();
@@ -106,12 +109,16 @@ export function getVersionFromFileContent(
distributionName: string
): string | null {
let fileContent = null;

core.debug(`Getting version from: '${fileName}'`);
if (fileName.includes('.java-version')) {
fileContent = parseJavaVersionFile(content)
} else if (fileName.includes('pom.xml')){
fileContent = parsePomXmlFile(content)
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'`)
throw new Error(
`File ${fileName} not supported, files supported: '.java-version' and 'pom.xml'`
);
}

if (!fileContent) {
@@ -148,75 +155,59 @@ function parseJavaVersionFile(content: string): string | null {
return null;
}

return fileContent
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)
function parsePomXmlFile(xmlString: string): string | null {
const xmlDoc = xmlCreate(xmlString);
const versionDefinitionTypes = [getByMavenCompilerSpecification, getBySpringBootSpecification];

return version !== null
})
for (var definitionType of versionDefinitionTypes) {
var version = definitionType(xmlDoc);

return tagFound?.at(0)?.toString() ?? null
if (version !== null) {
return version;
}
}

return null;
}

function getByMavenCompilerRelease(xmlDoc: Document): string | null {
function getByMavenCompilerSpecification(xmlDoc: XMLBuilder): string | null {
const possibleTags = [
"maven.compiler.release",
"release"
]
'//properties/maven.compiler.source',
'//configuration/source',
'//properties/maven.compiler.release',
'//configuration/release'
];

const tagFound = possibleTags.filter(function(tag) {
const version = getVersionByTagName(xmlDoc, tag)
for (var tag of possibleTags) {
const version = getVersionByTagName(xmlDoc, tag);

return version !== null
})
if (version !== null) {
return version;
}
}

return tagFound?.at(0)?.toString() ?? null
return null;
}

function getBySpringBootSpecification(xmlDoc: Document): string | null {
return getVersionByTagName(xmlDoc, "java.version")
function getBySpringBootSpecification(xmlDoc: XMLBuilder): string | null {
return getVersionByTagName(xmlDoc, '//properties/java.version');
}

function getVersionByTagName(xmlDoc: Document, tagName: string): string | null {
const element = xmlDoc.getElementsByTagName(tagName)
function getVersionByTagName(xmlDoc: XMLBuilder, xpathQuery: string): string | null {
const element = select(`string(${xpathQuery})`, xmlDoc.node as any);

if (element.length < 1) {
return null
core.debug(`Found: '${element}' using xpath query: '${xpathQuery}'`);
if (element == undefined || element.length == 0) {
return null;
}

return element[0].textContent

return element.toString();
}

// 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;
}