Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 32 additions & 66 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,96 +1,62 @@
# 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://github.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://github.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 }}
```

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`.
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down