From 35459bb912e446c55486d9a13397dd55262cde45 Mon Sep 17 00:00:00 2001 From: Wolf2323 Date: Thu, 19 Jun 2025 21:05:35 +0200 Subject: [PATCH] the maven command is now configurable to use, for example, mvnw --- README.md | 98 ++++++++++++++++++------------------------------------ action.yml | 4 +++ index.js | 3 +- 3 files changed, 38 insertions(+), 67 deletions(-) diff --git a/README.md b/README.md index 1fa0f2e..e67221e 100644 --- a/README.md +++ b/README.md @@ -1,81 +1,52 @@ -# A Github Action to Export Your Maven Project's Version for Use in Subsequent Steps +# A GitHub Action to Export Your Maven Project's Version for Use in Subsequent Steps -I saw a great tweet from [Bruno Borges](https://twitter.com/brunoborges) about using Github Actions to derive the version of Java to be installed by analyzing the `pom.xml` of the build itself. It seems like a good enough idea, -so I [put it into practive here](https://github.com/joshlong/java-version-export-github-action). Most of the magic lives [in a very tiny JavaScript file, `index.js`](https://raw.githubusercontent.com/joshlong/java-version-export-github-action/main/index.js): +This is a fork from [joshlong/java-version-export-github-action](https://github.com/joshlong/java-version-export-github-action) +that adds some new features to the base idea of the upstream: -Usage: +> I saw a great tweet from [Bruno Borges](https://twitter.com/brunoborges) about using GitHub Actions to derive +> the version of Java to be installed by analyzing the `pom.xml` of the build itself. +> It seems like a good enough idea, so I [put it into practice here](https://github.com/joshlong/java-version-export-github-action). +> Most of the magic lives [in a very tiny JavaScript file, `index.js`](https://raw.githubusercontent.com/joshlong/java-version-export-github-action/main/index.js): +> It'll analyze your Maven `pom.xml` to determine which version of Java you've configured by having Maven tell it +> the value of `maven.compiler.version`. +> The program then exports the version and the major version of Java as a step output _and_ an environment variable +> that you could use in later steps. +> +> I really should make this work for Gradle... (Pull requests welcome!) -```yaml - -const core = require("@actions/core"); -const github = require("@actions/github"); -const exec = require("@actions/exec"); - -function cmd(cmd, args, stdoutListener) { - exec - .exec(cmd, args, { - listeners: { - stdout: stdoutListener, - }, - }) - .then((ec) => { - console.debug("the exit code is " + ec); - }); -} - -try { - cmd( - "mvn", - [ - "help:evaluate", - "-q", - "-DforceStdout", - "-Dexpression=maven.compiler.target", - ], - // ["help:evaluate", "-q", "-DforceStdout", "-Dexpression=java.version"], - (outputBuffer) => { - const output = outputBuffer.toString(); - console.log(output); - const varsMap = new Map(); - varsMap.set("java_version", output + ""); - varsMap.set("java_major_version", parseInt("" + output) + ""); - varsMap.forEach(function (value, key) { - console.log(key + "=" + value); - core.setOutput(key, value); - core.exportVariable(key.toUpperCase(), value); - }); - } - ); -} catch (error) { - core.setFailed(error.message); -} -``` - -Basically, you put this at the very beginning of your Github Actions flow, right after the checkout, and it'll -analyse your Maven `pom.xml` to determine which version of Java you've configured by having Maven tell it the value of `maven.compiler.version`. The -program then exports the version and the major version of Java as a step output _and_ an environment variable that you could use in subsequent steps. +# Usage - -So, assuming the following usage: +This is a basic example of how to use this action in your workflow: ```yaml - - uses: joshlong/java-version-export-github-action@v17 + - uses: BetonQuest/java-version-export-github-action@main id: jve ``` or if you have the java version in another property like the release property: ```yaml - - uses: joshlong/java-version-export-github-action@v17 + - uses: BetonQuest/java-version-export-github-action@main id: jve with: maven-expression: 'maven.compiler.release' ``` -You can use the exported environment variable: +or if you want to use a different Maven command, like `./mvnw`, or `mvnd` or the daemon in the wrapper: +```yaml + - uses: BetonQuest/java-version-export-github-action@main + id: jve + with: + maven-command: './mvnw' + # Or for maven daemon with the --raw-streams option to work properly: + # maven-command: './mvnw --raw-streams' ``` - - uses: actions/setup-java@v3 + +Then you can use the exported environment variable: + +```yaml + - uses: actions/setup-java@v4 with: java-version: ${{ env.JAVA_MAJOR_VERSION }} ``` @@ -83,14 +54,9 @@ You can use the exported environment variable: or reference the step's output values, like this: ```yaml - - - uses: actions/setup-java@v3 + - uses: actions/setup-java@v4 with: java-version: ${{ steps.jve.outputs.java_major_version }} ``` - -Now Github Actions will download whatever version of Java you've specified in your Maven `pom.xml`. - -I really should make this work for Gradle... (Pull requests welcome!) - +Now GitHub Actions will download whatever version of Java you've specified in your Maven `pom.xml`. diff --git a/action.yml b/action.yml index 6097ba4..4a9afa9 100644 --- a/action.yml +++ b/action.yml @@ -6,6 +6,10 @@ inputs: description: "The Maven expression to evaluate for the java version" required: false default: "maven.compiler.target" + maven-command: + description: "The Maven command to use for example maven-wrapper" + required: false + default: "mvn" runs: using: "node20" diff --git a/index.js b/index.js index 6b2e2be..bd75875 100644 --- a/index.js +++ b/index.js @@ -42,8 +42,9 @@ try { if (maven) { const mavenExpression = core.getInput('maven-expression'); + const mavenCommand = core.getInput('maven-command'); cmd( - "mvn", + mavenCommand, [ "help:evaluate", "-q",