actions / setup-java

Set up your GitHub Actions workflow with a specific version of Java

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Proper way of using a Maven toolchain?

LorenzoBettini opened this issue · comments

This action generates a toolchains.xml file in .m2/toolchains.xml according to the arguments passed to the setup action; however, Maven (its toolchain plugin) does not seem to detect the needed information.

In my workflow I have (runs-on: ubuntu-latest):

      - name: 'Set up Java'
        uses: actions/setup-java@v4
        with:
          java-version: 21
          distribution: 'temurin'

      - name: Show Toolchain
        run: cat ~/.m2/toolchains.xml

which seems to work as expected:

Run actions/setup-java@v4
  with:
    java-version: 21
    distribution: temurin
    java-package: jdk
    check-latest: false
    server-id: github
    server-username: GITHUB_ACTOR
    server-password: GITHUB_TOKEN
    overwrite-settings: true
    job-status: success
    token: ***
Installed distributions
  Resolved Java 21.0.2+13 from tool-cache
  Setting Java 21.0.2+13 as the default
  Creating toolchains.xml for JDK version 21 from temurin
  Writing to /home/runner/.m2/toolchains.xml
  
  Java configuration:
    Distribution: temurin
    Version: 21.0.2+13
    Path: /opt/hostedtoolcache/Java_Temurin-Hotspot_jdk/21.0.2-13/x64
...
Run cat ~/.m2/toolchains.xml
<?xml version="1.0"?>
<toolchains xmlns="http://maven.apache.org/TOOLCHAINS/1.1.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/TOOLCHAINS/1.1.0 https://maven.apache.org/xsd/toolchains-1.1.0.xsd">
  <toolchain>
    <type>jdk</type>
    <provides>
      <version>21</version>
      <vendor>temurin</vendor>
      <id>temurin_21</id>
    </provides>
    <configuration>
      <jdkHome>/opt/hostedtoolcache/Java_Temurin-Hotspot_jdk/21.0.2-13/x64</jdkHome>
    </configuration>
  </toolchain>
</toolchains>

Then, in my Maven POM I have this profile, which is correctly activated:

<profile>
	<id>strict-jdk-21</id>
	<activation>
		<jdk>[21,)</jdk>
	</activation>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-toolchains-plugin</artifactId>
				<version>3.1.0</version>
				<executions>
					<execution>
						<goals>
							<goal>toolchain</goal>
						</goals>
					</execution>
				</executions>
				<configuration>
					<toolchains>
						<jdk>
							<version>21</version>
							<vendor>temurin</vendor>
							<id>temurin_21</id>
						</jdk>
					</toolchains>
				</configuration>
			</plugin>
		</plugins>
	</build>
</profile>

However, during the build the plugin doesn't seem to find the toolchain:

[INFO] Required toolchain: jdk [ vendor='temurin' id='temurin_21' version='21' ]
[DEBUG] Toolchain JDK[/opt/hostedtoolcache/Java_Temurin-Hotspot_jdk/17.0.10-7/x64] doesn't match required property: id
Error:  No toolchain matched from 1 found for type jdk
Error:  Cannot find matching toolchain definitions for the following toolchain types:
jdk [ vendor='temurin' id='temurin_21' version='21' ]

It looks like it ignores the generated toolchains since it refers to JDK 17, which is never mentioned in my workflow.

Am I doing anything wrong?

Hello @LorenzoBettini, Thank you for creating the issue and we will look into it :)

Hello @LorenzoBettini, I have tried reproducing the issue but I am unable to. I have attached screenshots of the workflow where I am able to use toolchains.xml as expected.

Screenshot 2024-04-15 at 12 11 07 Screenshot 2024-04-15 at 12 30 08

Please consider sharing a public repository for us to examine the workflow and pom.xml file, or provide the steps to reproduce the issue. Thank you.

@mahabaleshwars thank you for the verification.
Maybe my problem was due to an interaction between Maven and Tycho (the Maven plugin for building Eclipse plugins).
In that case, I could only make it work by creating a toolchains.xml and passing it with the Maven command line option.

@mahabaleshwars could you please share the project you have used?

Hello @LorenzoBettini,I'm pleased to share with you a link to a public repository. Here, you'll find a successful implementation of the workflow and pom profile that aligns with your project. I believe it could be useful for your reference.

@mahabaleshwars thank you for sharing this. I'll examine that further.
For the moment, in my case, where as I said we use Maven/Tycho, the only way I could find to make it work is this: eclipse/xtext@8f8b905

That is, copy the toolchains.xml generated by the action into another directory (i.e., the root of the git repository) and refer to that with the --toolchains command line argument. I know it makes no sense, but that's the only way to make it work. Just to be clear --toolchains ~/.m2/toolchains.xml does NOT work either.

Hello @LorenzoBettini, could you please verify if the provided repository assisted you in resolving the issue?

Hello @LorenzoBettini, could you confirm if your issue has been resolved?

@mahabaleshwars your example is precious and I think it should be referred in the documentation.

In my case, probably due to the additional Tycho Maven plugin, it just doesn't work: I had to do the hack described above, which doesn't make sense (because the contents of the copied file is the same).

Hello @LorenzoBettini, we appreciate your response. This issue is now being closed. Don't hesitate to contact us if you have any other issues.