mirror of
				https://gitea.com/actions/setup-java.git
				synced 2025-10-30 19:28:59 +08:00 
			
		
		
		
	feat: added support to build.gradle
This commit is contained in:
		
							
								
								
									
										84
									
								
								.github/workflows/e2e-versions.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										84
									
								
								.github/workflows/e2e-versions.yml
									
									
									
									
										vendored
									
									
								
							| @@ -469,7 +469,6 @@ jobs: | |||||||
|       - name: Verify Java |       - name: Verify Java | ||||||
|         run: bash __tests__/verify-java.sh "8" "${{ steps.setup-java.outputs.path }}" |         run: bash __tests__/verify-java.sh "8" "${{ steps.setup-java.outputs.path }}" | ||||||
|         shell: bash |         shell: bash | ||||||
|  |  | ||||||
|   setup-java-version-from-pom-maven-compiler-plugin-configuration: |   setup-java-version-from-pom-maven-compiler-plugin-configuration: | ||||||
|     name: ${{ matrix.distribution }} version from pom.xml - ${{ matrix.os }} |     name: ${{ matrix.distribution }} version from pom.xml - ${{ matrix.os }} | ||||||
|     runs-on: ${{ matrix.os }} |     runs-on: ${{ matrix.os }} | ||||||
| @@ -513,3 +512,86 @@ jobs: | |||||||
|       - name: Verify Java |       - name: Verify Java | ||||||
|         run: bash __tests__/verify-java.sh "8" "${{ steps.setup-java.outputs.path }}" |         run: bash __tests__/verify-java.sh "8" "${{ steps.setup-java.outputs.path }}" | ||||||
|         shell: bash |         shell: bash | ||||||
|  |   setup-java-version-from-build-gradle-java-library-plugin-specification: | ||||||
|  |     name: ${{ matrix.distribution }} version from build.gradle - ${{ 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 build.gradle file | ||||||
|  |         shell: bash | ||||||
|  |         run: | | ||||||
|  |           echo "java {" > build.gradle | ||||||
|  |           echo "    toolchain {" >> build.gradle | ||||||
|  |           echo "        languageVersion.set(JavaLanguageVersion.of(11))" >> build.gradle | ||||||
|  |           echo "    }" >> build.gradle | ||||||
|  |           echo "}" >> build.gradle | ||||||
|  |       - name: setup-java | ||||||
|  |         uses: ./ | ||||||
|  |         id: setup-java | ||||||
|  |         with: | ||||||
|  |           distribution: ${{ matrix.distribution }} | ||||||
|  |           java-version-file: 'build.gradle' | ||||||
|  |       - name: Verify Java | ||||||
|  |         run: bash __tests__/verify-java.sh "11" "${{ steps.setup-java.outputs.path }}" | ||||||
|  |         shell: bash | ||||||
|  |  | ||||||
|  |   setup-java-version-from-build-gradle-java-plugin-source-compatibility-specification: | ||||||
|  |     name: ${{ matrix.distribution }} version from build.gradle - ${{ 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 "java {" > build.gradle | ||||||
|  |           echo "    sourceCompatibility = JavaVersion.VERSION_1_8" >> build.gradle | ||||||
|  |           echo "    targetCompatibility = JavaVersion.VERSION_1_8" >> build.gradle | ||||||
|  |           echo "}" >> build.gradle | ||||||
|  |       - name: setup-java | ||||||
|  |         uses: ./ | ||||||
|  |         id: setup-java | ||||||
|  |         with: | ||||||
|  |           distribution: ${{ matrix.distribution }} | ||||||
|  |           java-version-file: 'build.gradle' | ||||||
|  |       - name: Verify Java | ||||||
|  |         run: bash __tests__/verify-java.sh "8" "${{ steps.setup-java.outputs.path }}" | ||||||
|  |         shell: bash | ||||||
|  |  | ||||||
|  |   setup-java-version-from-build-gradle-java-plugin-target-compatibility-specification: | ||||||
|  |     name: ${{ matrix.distribution }} version from build.gradle - ${{ 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 "java {" > build.gradle | ||||||
|  |           echo "    targetCompatibility = JavaVersion.VERSION_21" >> build.gradle | ||||||
|  |           echo "}" >> build.gradle | ||||||
|  |       - name: setup-java | ||||||
|  |         uses: ./ | ||||||
|  |         id: setup-java | ||||||
|  |         with: | ||||||
|  |           distribution: ${{ matrix.distribution }} | ||||||
|  |           java-version-file: 'build.gradle' | ||||||
|  |       - name: Verify Java | ||||||
|  |         run: bash __tests__/verify-java.sh "21" "${{ steps.setup-java.outputs.path }}" | ||||||
|  |         shell: bash | ||||||
|   | |||||||
							
								
								
									
										39
									
								
								dist/cleanup/index.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										39
									
								
								dist/cleanup/index.js
									
									
									
									
										vendored
									
									
								
							| @@ -103801,6 +103801,9 @@ function getVersionFromFile(fileName, content, distributionName) { | |||||||
|     else if (fileName.includes('pom.xml')) { |     else if (fileName.includes('pom.xml')) { | ||||||
|         parsedVersion = parsePomXmlFile(content); |         parsedVersion = parsePomXmlFile(content); | ||||||
|     } |     } | ||||||
|  |     else if (fileName.includes('build.gradle')) { | ||||||
|  |         parsedVersion = parseBuildGradleFile(content); | ||||||
|  |     } | ||||||
|     else { |     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'`); | ||||||
|     } |     } | ||||||
| @@ -103909,6 +103912,42 @@ function getByMavenCompilerPluginConfig(xmlDoc) { | |||||||
|     }); |     }); | ||||||
|     return (_a = source === null || source === void 0 ? void 0 : source.first().toString()) !== null && _a !== void 0 ? _a : null; |     return (_a = source === null || source === void 0 ? void 0 : source.first().toString()) !== null && _a !== void 0 ? _a : null; | ||||||
| } | } | ||||||
|  | function parseBuildGradleFile(buildGradle) { | ||||||
|  |     const versionDefinitionTypes = [getByJavaLibraryPlugin, getByJavaPlugin]; | ||||||
|  |     for (const definitionType of versionDefinitionTypes) { | ||||||
|  |         const version = definitionType(buildGradle); | ||||||
|  |         if (version !== null) { | ||||||
|  |             return version; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     return null; | ||||||
|  | } | ||||||
|  | function getByJavaLibraryPlugin(buildGradle) { | ||||||
|  |     return getVersionByRegex(buildGradle, 'JavaLanguageVersion.of((d+))'); | ||||||
|  | } | ||||||
|  | function getByJavaPlugin(buildGradle) { | ||||||
|  |     const possibleRegex = [ | ||||||
|  |         'sourceCompatibilitys?=s?JavaVersion.VERSION_(?:1_)?(d+)', | ||||||
|  |         'targetCompatibilitys?=s?JavaVersion.VERSION_(?:1_)?(d+)' | ||||||
|  |     ]; | ||||||
|  |     for (var regex of possibleRegex) { | ||||||
|  |         const version = getVersionByRegex(buildGradle, regex); | ||||||
|  |         if (version !== null) { | ||||||
|  |             return version; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     return null; | ||||||
|  | } | ||||||
|  | function getVersionByRegex(content, regex) { | ||||||
|  |     const match = content.match(new RegExp(regex)); | ||||||
|  |     if (match) { | ||||||
|  |         core.debug(`Found java version: '${match[1]}' using regex: '${regex}'`); | ||||||
|  |         return match[1]; | ||||||
|  |     } | ||||||
|  |     else { | ||||||
|  |         return null; | ||||||
|  |     } | ||||||
|  | } | ||||||
| // By convention, action expects version 8 in the format `8.*` instead of `1.8`
 | // By convention, action expects version 8 in the format `8.*` instead of `1.8`
 | ||||||
| function avoidOldNotation(content) { | function avoidOldNotation(content) { | ||||||
|     return content.startsWith('1.') ? content.substring(2) : content; |     return content.startsWith('1.') ? content.substring(2) : content; | ||||||
|   | |||||||
							
								
								
									
										39
									
								
								dist/setup/index.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										39
									
								
								dist/setup/index.js
									
									
									
									
										vendored
									
									
								
							| @@ -105458,6 +105458,9 @@ function getVersionFromFile(fileName, content, distributionName) { | |||||||
|     else if (fileName.includes('pom.xml')) { |     else if (fileName.includes('pom.xml')) { | ||||||
|         parsedVersion = parsePomXmlFile(content); |         parsedVersion = parsePomXmlFile(content); | ||||||
|     } |     } | ||||||
|  |     else if (fileName.includes('build.gradle')) { | ||||||
|  |         parsedVersion = parseBuildGradleFile(content); | ||||||
|  |     } | ||||||
|     else { |     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'`); | ||||||
|     } |     } | ||||||
| @@ -105566,6 +105569,42 @@ function getByMavenCompilerPluginConfig(xmlDoc) { | |||||||
|     }); |     }); | ||||||
|     return (_a = source === null || source === void 0 ? void 0 : source.first().toString()) !== null && _a !== void 0 ? _a : null; |     return (_a = source === null || source === void 0 ? void 0 : source.first().toString()) !== null && _a !== void 0 ? _a : null; | ||||||
| } | } | ||||||
|  | function parseBuildGradleFile(buildGradle) { | ||||||
|  |     const versionDefinitionTypes = [getByJavaLibraryPlugin, getByJavaPlugin]; | ||||||
|  |     for (const definitionType of versionDefinitionTypes) { | ||||||
|  |         const version = definitionType(buildGradle); | ||||||
|  |         if (version !== null) { | ||||||
|  |             return version; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     return null; | ||||||
|  | } | ||||||
|  | function getByJavaLibraryPlugin(buildGradle) { | ||||||
|  |     return getVersionByRegex(buildGradle, 'JavaLanguageVersion.of((d+))'); | ||||||
|  | } | ||||||
|  | function getByJavaPlugin(buildGradle) { | ||||||
|  |     const possibleRegex = [ | ||||||
|  |         'sourceCompatibilitys?=s?JavaVersion.VERSION_(?:1_)?(d+)', | ||||||
|  |         'targetCompatibilitys?=s?JavaVersion.VERSION_(?:1_)?(d+)' | ||||||
|  |     ]; | ||||||
|  |     for (var regex of possibleRegex) { | ||||||
|  |         const version = getVersionByRegex(buildGradle, regex); | ||||||
|  |         if (version !== null) { | ||||||
|  |             return version; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     return null; | ||||||
|  | } | ||||||
|  | function getVersionByRegex(content, regex) { | ||||||
|  |     const match = content.match(new RegExp(regex)); | ||||||
|  |     if (match) { | ||||||
|  |         core.debug(`Found java version: '${match[1]}' using regex: '${regex}'`); | ||||||
|  |         return match[1]; | ||||||
|  |     } | ||||||
|  |     else { | ||||||
|  |         return null; | ||||||
|  |     } | ||||||
|  | } | ||||||
| // By convention, action expects version 8 in the format `8.*` instead of `1.8`
 | // By convention, action expects version 8 in the format `8.*` instead of `1.8`
 | ||||||
| function avoidOldNotation(content) { | function avoidOldNotation(content) { | ||||||
|     return content.startsWith('1.') ? content.substring(2) : content; |     return content.startsWith('1.') ? content.substring(2) : content; | ||||||
|   | |||||||
| @@ -485,4 +485,8 @@ It is able to parse the following files as `java-version-file`: | |||||||
|   - Maven compiler plugin |   - Maven compiler plugin | ||||||
|     - Setting the [source](https://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html). |     - Setting the [source](https://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html). | ||||||
|     - Setting the [release](https://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-release.html). |     - Setting the [release](https://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-release.html). | ||||||
|     - **Note:** Since we are using a RegExp to find the java version, it doesn't grab the values from the tags `source` or `release` on `maven-compiler-plugin`. | - `build.gradle`  | ||||||
|  |   - Java library plugin: uses what is defined by `JavaLanguageVersion` example: `JavaLanguageVersion.of(11)` | ||||||
|  |   - Java plugin [docs](https://docs.gradle.org/current/userguide/java_plugin.html#toolchain_and_compatibility): | ||||||
|  |     - sourceCompatibility | ||||||
|  |     - targetCompatibility | ||||||
|   | |||||||
							
								
								
									
										49
									
								
								src/util.ts
									
									
									
									
									
								
							
							
						
						
									
										49
									
								
								src/util.ts
									
									
									
									
									
								
							| @@ -9,6 +9,7 @@ import * as tc from '@actions/tool-cache'; | |||||||
| import { INPUT_JOB_STATUS, DISTRIBUTIONS_ONLY_MAJOR_VERSION } from './constants'; | import { INPUT_JOB_STATUS, DISTRIBUTIONS_ONLY_MAJOR_VERSION } from './constants'; | ||||||
| import { create } from 'xmlbuilder2'; | import { create } from 'xmlbuilder2'; | ||||||
| import { XMLBuilder } from 'xmlbuilder2/lib/interfaces'; | import { XMLBuilder } from 'xmlbuilder2/lib/interfaces'; | ||||||
|  | import { on } from 'events'; | ||||||
|  |  | ||||||
| export function getTempDir() { | export function getTempDir() { | ||||||
|   let tempDirectory = process.env['RUNNER_TEMP'] || os.tmpdir(); |   let tempDirectory = process.env['RUNNER_TEMP'] || os.tmpdir(); | ||||||
| @@ -114,6 +115,8 @@ export function getVersionFromFile( | |||||||
|     parsedVersion = parseJavaVersionFile(content); |     parsedVersion = parseJavaVersionFile(content); | ||||||
|   } else if (fileName.includes('pom.xml')) { |   } else if (fileName.includes('pom.xml')) { | ||||||
|     parsedVersion = parsePomXmlFile(content); |     parsedVersion = parsePomXmlFile(content); | ||||||
|  |   } else if (fileName.includes('build.gradle')) { | ||||||
|  |     parsedVersion = parseBuildGradleFile(content); | ||||||
|   } else { |   } else { | ||||||
|     throw new Error( |     throw new Error( | ||||||
|       `File ${fileName} not supported, files supported: '.java-version' and 'pom.xml'` |       `File ${fileName} not supported, files supported: '.java-version' and 'pom.xml'` | ||||||
| @@ -249,6 +252,52 @@ function getByMavenCompilerPluginConfig(xmlDoc: XMLBuilder): string | null { | |||||||
|   return source?.first().toString() ?? null; |   return source?.first().toString() ?? null; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | function parseBuildGradleFile(buildGradle: string): any { | ||||||
|  |   const versionDefinitionTypes = [getByJavaLibraryPlugin, getByJavaPlugin]; | ||||||
|  |  | ||||||
|  |   for (const definitionType of versionDefinitionTypes) { | ||||||
|  |     const version = definitionType(buildGradle); | ||||||
|  |  | ||||||
|  |     if (version !== null) { | ||||||
|  |       return version; | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   return null; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | function getByJavaLibraryPlugin(buildGradle: string) { | ||||||
|  |   return getVersionByRegex(buildGradle, 'JavaLanguageVersion.of((d+))'); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | function getByJavaPlugin(buildGradle: string) { | ||||||
|  |   const possibleRegex = [ | ||||||
|  |     'sourceCompatibilitys?=s?JavaVersion.VERSION_(?:1_)?(d+)', | ||||||
|  |     'targetCompatibilitys?=s?JavaVersion.VERSION_(?:1_)?(d+)' | ||||||
|  |   ]; | ||||||
|  |  | ||||||
|  |   for (var regex of possibleRegex) { | ||||||
|  |     const version = getVersionByRegex(buildGradle, regex); | ||||||
|  |  | ||||||
|  |     if (version !== null) { | ||||||
|  |       return version; | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   return null; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | function getVersionByRegex(content: string, regex: string): string | null { | ||||||
|  |   const match = content.match(new RegExp(regex)); | ||||||
|  |  | ||||||
|  |   if (match) { | ||||||
|  |     core.debug(`Found java version: '${match[1]}' using regex: '${regex}'`); | ||||||
|  |     return match[1]; | ||||||
|  |   } else { | ||||||
|  |     return null; | ||||||
|  |   } | ||||||
|  | } | ||||||
|  |  | ||||||
| // By convention, action expects version 8 in the format `8.*` instead of `1.8` | // By convention, action expects version 8 in the format `8.*` instead of `1.8` | ||||||
| function avoidOldNotation(content: string): string { | function avoidOldNotation(content: string): string { | ||||||
|   return content.startsWith('1.') ? content.substring(2) : content; |   return content.startsWith('1.') ? content.substring(2) : content; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 augustomelo
					augustomelo