@Library('infraLib') _
pipeline {
  agent {
    label 'linux-agents'
  }
  options {
    timeout(time: 10, unit: 'MINUTES')
  }
  stages {
    stage('Set Maven credentials') {
      steps {
        script {
          nexus.generateAuthorizedMavenSettingsXML("./settings.xml")
        }
      }
    }
    stage('Docker stage') {
      agent {
        docker {
          image 'maven:3-jdk-8-slim'
          reuseNode true
        }
      }
      stages {
        stage('Test') {
          steps {
            sh 'mvn test'
          }
        }
        stage('Package') {
          steps {
            // testing is done, just package
            sh 'mvn package -DskipTests'
          }
        }
        stage('Deploy') {
          when {
            // publish only for `master` and `develop`
            anyOf {
              branch 'master'
              allOf {
                // publish SNAPSHOTs only from the `develop` and `hotfixes`
                anyOf {
                  branch 'develop'
                  branch 'hotfixes'
                }
                expression { BUILD_VERSION ==~ /.*-SNAPSHOT$/ }
              }
            }
          }
          steps {
            sh """mvn deploy \
              --settings ${WORKSPACE}/settings.xml \
              -DskipTests \
              -Dmaven.install.skip=true
              """
          }
        }
      }
    }
  }
}
