pipeline { agent any environment { APP_PORT = 6767 PID_FILE = "${WORKSPACE}/app.pid" } stages { stage('Echo Version') { steps { sh 'echo Print Maven Version' sh 'mvn -version ' } } stage('Build') { steps { sh 'mvn clean package -DskipTests=true' archiveArtifacts 'target/hello-demo-*.jar' } } stage('Unit Test') { steps { script { sh "mvn test" } junit(testResults: 'target/surefire-reports/TEST-*.xml', keepProperties: true, keepTestNames: true) } } stage('Start App') { steps { sh ''' set -e if [ -f "$PID_FILE" ]; then kill "$(cat "$PID_FILE")" || true rm -f "$PID_FILE" fi echo "Starting JAR..." nohup java -jar target/hello-demo-*.jar \ > app.log 2>&1 & echo $! > "$PID_FILE" echo "App started (PID: $(cat "$PID_FILE"))" ''' } } stage('Integration Test') { steps { sh ''' set -e echo "Waiting for app..." for i in {1..15}; do if curl -fs http://localhost:$APP_PORT/hello >/dev/null; then echo "Integration test passed" curl -s http://localhost:$APP_PORT/hello exit 0 fi sleep 1 done echo "Integration test failed" cat app.log || true exit 1 ''' } } } post { always { sh ''' if [ -f "$PID_FILE" ]; then echo "Stopping application" kill "$(cat "$PID_FILE")" || true rm -f "$PID_FILE" fi ''' } } tools { maven 'M398' } }