# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

name: Can Maven build itself

on: [push, pull_request]

jobs:
  build:
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macOS-latest]
        java: [8, 11, 17]
      fail-fast: false

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

    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-java@v2
        with:
          java-version: ${{ matrix.java }}
          distribution: 'temurin'
          cache: 'maven'

      - name: Build with Maven
        run: mvn verify -e -B -V -DdistributionFileName=apache-maven

      - name: Extract tarball
        shell: bash
        run: |
          set +e
          if [ -f ${{ env.TAR_BALL }} ]; then
            temp_dir=$(mktemp -d)
            tar -xzf ${{ env.TAR_BALL }} -C "$temp_dir" --strip 1
            maven_bin_dir=$temp_dir/bin
            if [ -d $maven_bin_dir ]; then
              echo "tar.gz file \"${{ env.TAR_BALL }}\" succesfully extracted in temporarily directory \"$temp_dir.\""
              echo "TEMP_MAVEN_BIN_DIR=$maven_bin_dir" >> $GITHUB_ENV
            else
              echo "$maven_bin_dir does not exist."
              exit 1;
            fi
          else
            echo "${{ env.TAR_BALL }} does not exist."
            exit 1;
          fi
        env:
          TAR_BALL: apache-maven/target/apache-maven-bin.tar.gz

      - name: Clean with Maven
        run: mvn clean

      - name: Build again with Maven SNAPSHOT
        shell: bash
        run: |
          set +e
          export PATH=${{ env.TEMP_MAVEN_BIN_DIR }}:$PATH
          mvn verify -e -B -V -DdistributionFileName=apache-maven

Verify Maven can build itself
[MNG-7339] Verify Maven can build itself

Closes #613.
