pipeline {
    agent { label 'linux' }
    stages {
        stage('Checkout') {
            steps {
                script {
                    infra.checkoutSCM()
                }
            }
        }
        stage('Build') {
            steps {
                script {
                    m2repo = "${pwd tmp: true}/m2repo"
                    String jdk = "21"
                    List<String> mavenOptions = [
                            '--update-snapshots',
                            "-Dmaven.repo.local=$m2repo",
                            '-Dmaven.test.failure.ignore',
                            "-Dset.changelist",
                            "-Djava.security.egd=file:/dev/./urandom",
                            "clean install"
                    ]

                    infra.runMaven(mavenOptions, jdk)
                }
             }
             post {
                 always {
                    /*
                     archiveArtifacts(allowEmptyArchive: true,
                         artifacts: "** /target/trilead*.jar",
                         onlyIfSuccessful: false)
                     */
                     junit(allowEmptyResults: true,
                         keepLongStdio: true,
                         testResults: "**/target/surefire-reports/**/*.xml")
                     script {
                        def m2repo = "${pwd tmp: true}/m2repo"
                        // No easy way to load both of these in one command: https://stackoverflow.com/q/23521889/12916
                        String version = sh script: 'mvn -Dset.changelist -Dexpression=project.version -q -DforceStdout help:evaluate', returnStdout: true
                        echo "Collecting $version from $m2repo for possible Incrementals publishing"
                        dir(m2repo) {
                            archiveArtifacts "org/jenkins-ci/trilead-ssh2/$version/*$version*"
                        }
                        infra.maybePublishIncrementals()
                     }
                 }
             }
        }
    }
}
