pipeline {
    agent any

    triggers {
        cron('H 3 * * *')
    }

    stages {
        stage('Checkout') {
            steps {
                checkout scm
            }
        }

        stage('Install JDK 25') {
            steps {
                sh '''
                    if [ ! -d "/opt/jdk25/bin" ]; then
                        echo "Installing JDK 25..."
                        curl -L "https://api.adoptium.net/v3/binary/latest/25/ga/linux/x64/jdk/hotspot/normal/eclipse" \
                            -o /tmp/jdk25.tar.gz
        
                        rm -rf /opt/jdk25
                        mkdir -p /opt/jdk25
        
                        tar -xzf /tmp/jdk25.tar.gz -C /opt/jdk25 --strip-components=1
                    fi
        
                    ls -la /opt/jdk25/bin
                '''
            }
        }

        stage('Build') {
            steps {
                sh '''
                    export JAVA_HOME=/opt/jdk25
                    export PATH=$JAVA_HOME/bin:$PATH
                    mvn -B verify
                '''
            }
        }

        stage('Archive') {
            steps {
                archiveArtifacts artifacts: 'target/*.jar', fingerprint: true
            }
        }
    }

    post {
        failure {
            echo 'Build failed.'
        }
    }
}