javagradlegithub-actions

./gradlew build failed with Error: Could not find or load main class org.gradle.wrapper.GradleWrapperMain


I have a Java-Project as amulti-module-gradle project which look like this.

rootproject
├── subproject1
│   └── src/main/java
├── subproject2
│   └── src/main/java
├── subproject3
│   └── src/main/java
│
├── build.gradle
├── gradlew
├── gradlew.bat
├── settings.gradle
└── gradle
    └── wrapper
        ├── gradle-wrapper.jar
        └── gradle-wrapper.properties

I would like to build it in a GitHub-Action.

name: CI

on:
  push:
    branches:
      - '*'

concurrency:
  group: ci-${{ github.ref }}
  cancel-in-progress: true

jobs:
  build:
    name: CI-Build
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - uses: actions/setup-java@v4
        with:
          java-version: 17
          distribution: 'temurin'

      - name: Build and analyze
        run: |
          ./gradlew clean build

Step Build and analyze failed with the following error:

Error: Could not find or load main class org.gradle.wrapper.GradleWrapperMain
Caused by: java.lang.ClassNotFoundException: org.gradle.wrapper.GradleWrapperMain

The often mentioned solution of committing the files in gradle/wrapper does not solve the problem as they are already committed and are explicitly excluded in .gitignore.


Solution

  • After a long search, a colleague was able to find the solution to the problem.

    A .gitattributes file was committed in the project. In it, the *.jar files were configured as Git LFS files.

    *.jar filter=lfs diff=lfs merge=lfs -text
    

    Due to a merge, the gradle-wrapper.jar file was destroyed and could no longer be read.

    After deleting the configuration for *.jar files and recreating the gradle-wrapper.jar, ./gradlew can now be used without an error.