Search code examples
javamavengithub-actions

github workflows: maven tests fail when because of missing some test resources, but runs local


I spent hours to find out, why one of my junit tests runs local but not on the github workflow. The test which fails checks the existence of some files to do some configuration stuff. I'm using the maven-resources-plugin to copy these files to a folder inside the target directory. Here is my plugin setting of my pom.xml:

<plugin>
    <!-- move some test resources to target -->
    <artifactId>maven-resources-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-resource-one</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>${basedir}/target/test-dir</outputDirectory>
                <resources>
                    <resource>
                        <directory>${basedir}/test-files</directory>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

The files are copied as expected and mvn clean test runs as expected, but not within a github workflow.

Here is my workflow definition:

name: Sonar Scan
on:
  push:
    branches:
      - develop
      - feature*
      - master
  pull_request:
    types: [opened, synchronize, reopened]
jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
      - name: Checkout source
        uses: actions/checkout@v2
      - name: Set up JDK 11
        uses: actions/[email protected]
        with:
          distribution: 'adopt'
          java-version: '11'
      - name: Cache SonarCloud packages
        uses: actions/cache@v1
        with:
          path: ~/.sonar/cache
          key: ${{ runner.os }}-sonar
          restore-keys: ${{ runner.os }}-sonar
      - name: Build and analyze
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}  # Needed to get PR information, if any
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
        run: mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=abckey

I guess this is a typically RTFM problem, but I didn't find anything.

Any hints?

UPDATE:

Just testet, mvn clean test fails on github workflow too!


Solution

  • You don't need to copy test resource files to target[/sub/dir] explicitely if you place them in src/test/resources[/sub/dir]. The Maven Resources Plugin is bound to the process-resources and process-test-resources phases by default and hence does this for you without any declaration in the POM:

    The Resources Plugin handles the copying of project resources to the output directory. There are two different kinds of resources: main resources and test resources. The difference is that the main resources are the resources associated to the main source code while the test resources are associated to the test source code.

    Thus, this allows the separation of resources for the main source code and its unit tests.

    Not that this is the reason for your issue but I'd remove the declaration from the POM anyway.