name: Build and test (using maven)

#on: [push, pull_request]
#on: [pull_request]
on:
  # Trigger the workflow on push to master (ignoring .md only changes)
  push:
    branches:
      - master
    paths-ignore:
      - '**.md'

  # Trigger the workflow on any pull_request (ignoring .md only changes)
  pull_request:
    paths-ignore:
      - '**.md'

  # Enable manual triggering (important for contributors to enable a check on their fork)
  workflow_dispatch:

# If a build is running in the current branch, and the branch is updated, we cancel the previous build and start
# a new one with the updated changes.
concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

jobs:
  maven_test:
    strategy:
      ## Optionally cancel all other combinations if one fails
      fail-fast: false
      matrix:
        ## Different OSs have different default line-endings -- test on all combinations.
        os: [ ubuntu-latest, macos-latest, windows-latest ]
        ## Different JDK versions have different implementations etc. -- test on all combinations (ideally 8 to latest).
        ### exclude pre-8 (min development version jdk8)
        jdk: [ 8,9,10,11,12,13,14,15,16,17,18 ]
        # The below configurations are no longer available on github runners and is not supported by the 
        # setup-java action, nor are they available from the supported distributions.
        # See https://github.com/actions/setup-java for details
        exclude:
          - os: macos-latest
            jdk: 9
          - os: macos-latest
            jdk: 10
          - os: macos-latest
            jdk: 12
          - os: macos-latest
            jdk: 14

    env:
      OS: ${{ matrix.os }}
      JDK: ${{ matrix.jdk }}

    runs-on: ${{ matrix.os }}

    steps:
      ## Checkout the current version of the code from the repo.
      - name: Checkout latest code
        uses: actions/checkout@v4.2.2
        with:
          fetch-depth: "0"

      ## Setup the specified version of Java (includes maven/gradle).
      - name: Set up JDK ${{ matrix.jdk }}
        uses: actions/setup-java@v4
        with:
          distribution: 'zulu'              # v2 requires explicitly stating the distribution - `zulu` and `adopt` supported at time of writing
          java-version: ${{ matrix.jdk }}   # Use matrix to select which JDK level to use
          java-package: jdk                 # optional (jdk or jre) - defaults to jdk

      ## Given that the build matrix only specifies the major version (configurable), output the precise version used.
      - name: Echo exact java version being used
        run: java -version

      ## Use a cache to reduce the build/test times (avoids having to download dependencies on EVERY run).
      ### https://help.github.com/en/actions/language-and-framework-guides/building-and-testing-java-with-maven#caching-dependencies
      - name: Cache Maven packages
        uses: actions/cache@v4
        with:
          path: ~/.m2
          key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
          restore-keys: ${{ runner.os }}-m2


      ## Actually perform the tests. Unsuccessful tests will indicate a failed build.
      ### -e, --errors : show traces where any errors occur
      ### -B,--batch-mode : Run in non-interactive (batch) mode (disables output color)
      ### clean : run the maven lifecycle stage `clean`
      ### test : run the maven lifecycle stage `test`
      ### -P,--activate-profiles : Comma-delimited list of profiles to activate
      ### AlsoSlowTests : by default, only quick tests are run - the profile `AlsoSlowTests` runs the full test suite
      - name: Test with Maven (incl. slow tests)
        shell: bash
        run: ./mvnw -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn -B --errors clean test --activate-profiles AlsoSlowTests


      - name: CodeCov - JavaParser Core
        uses: codecov/codecov-action@v5.4.3
        timeout-minutes: 10
        with:
          files: javaparser-core-testing/target/site/jacoco/jacoco.xml,javaparser-core-testing-bdd/target/site/jacoco/jacoco.xml
          fail_ci_if_error: false # optional (default = false) -- fail the build if upload to codecov.io fails
          verbose: false # optional (default = false):
          flags: javaparser-core,AlsoSlowTests,${{ matrix.os }},jdk-${{ matrix.jdk }}
          env_vars: OS,JDK

      - name: CodeCov - JavaParser Symbol Solver
        uses: codecov/codecov-action@v5.4.3
        timeout-minutes: 10
        with:
          file: javaparser-symbol-solver-testing/target/site/jacoco/jacoco.xml
          fail_ci_if_error: false # optional (default = false) -- fail the build if upload to codecov.io fails
          verbose: false # optional (default = false):
          flags: javaparser-symbol-solver,AlsoSlowTests,${{ matrix.os }},jdk-${{ matrix.jdk }}
          env_vars: OS,JDK
