<?xml version='1.1' encoding='UTF-8'?>
<project>
  <description>Fzx Platform Build Job - Compile and package all microservices and frontend</description>
  <keepDependencies>false</keepDependencies>
  <properties>
    <jenkins.model.BuildDiscarderProperty>
      <strategy class="hudson.tasks.LogRotator">
        <daysToKeep>7</daysToKeep>
        <numToKeep>5</numToKeep>
        <artifactDaysToKeep>7</artifactDaysToKeep>
        <artifactNumToKeep>3</artifactNumToKeep>
        <removeLastBuild>false</removeLastBuild>
      </strategy>
    </jenkins.model.BuildDiscarderProperty>
  </properties>
  <scm class="hudson.plugins.git.GitSCM" plugin="git@5.10.1">
    <configVersion>2</configVersion>
    <userRemoteConfigs>
      <hudson.plugins.git.UserRemoteConfig>
        <url>https://codeup.aliyun.com/69c68bacfa2a62bc85952a67/FzxBdsh.git</url>
        <credentialsId>6a269f08-e243-4f6a-843e-76244317e4a0</credentialsId>
      </hudson.plugins.git.UserRemoteConfig>
    </userRemoteConfigs>
    <branches>
      <hudson.plugins.git.BranchSpec>
        <name>*/develop</name>
      </hudson.plugins.git.BranchSpec>
    </branches>
    <doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>
    <submoduleCfg class="empty-list"/>
    <extensions/>
  </scm>
  <canRoam>true</canRoam>
  <disabled>false</disabled>
  <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
  <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
  <triggers/>
  <concurrentBuild>false</concurrentBuild>
  <builders>
    <hudson.tasks.Shell>
      <command><![CDATA[#!/bin/bash
set -euo pipefail

echo "========================================"
echo "Fzx Platform Build - Aliyun Dev Environment"
echo "========================================"

MVN_BIN="/opt/maven/bin/mvn"
if [ ! -f "$MVN_BIN" ]; then
    echo "ERROR: Maven not found at $MVN_BIN"
    exit 1
fi
echo "Using Maven: $MVN_BIN"
$MVN_BIN -version

if [ -z "${WORKSPACE:-}" ]; then
    echo "ERROR: WORKSPACE environment variable is not set"
    exit 1
fi

echo "Workspace: ${WORKSPACE}"
echo "Current directory: $(pwd)"

if [ ! -d "${WORKSPACE}" ]; then
    echo "WARN: Workspace directory does not exist, creating..."
    mkdir -p "${WORKSPACE}" || { echo "ERROR: Failed to create workspace"; exit 1; }
fi

cd "${WORKSPACE}" || { echo "ERROR: Failed to cd to workspace"; exit 1; }

echo "Checking project structure..."
if [ ! -d "services" ]; then
    echo "ERROR: services directory not found in workspace"
    ls -la
    exit 1
fi

echo "========================================"
echo "Generating version info (v1.1.0 baseline)"
echo "========================================"
BASE_VERSION="1.1.0"

if [ -n "${GIT_BRANCH:-}" ]; then
    BRANCH="${GIT_BRANCH#origin/}"
else
    BRANCH=$(git for-each-ref --format='%(refname:short)' refs/heads/ --points-at HEAD 2>/dev/null | head -n 1 || git symbolic-ref --short HEAD 2>/dev/null || echo "unknown")
fi
BRANCH_SAFE=$(echo "${BRANCH}" | tr '/' '-')

COMMIT_HASH="${GIT_COMMIT:-$(git rev-parse --short HEAD 2>/dev/null || echo "unknown")}"
BUILD_NUMBER="${BUILD_ID:-$(date +%Y%m%d%H%M)}"

VERSION="${BASE_VERSION}-${BRANCH_SAFE}-${COMMIT_HASH}-${BUILD_NUMBER}"
echo "Full version: ${VERSION}"

BUILD_TIME=$(date +%Y-%m-%dT%H:%M:%S)
echo "Generating version.json..."
cat > version.json <<EOF
{
  "version": "${VERSION}",
  "baseVersion": "${BASE_VERSION}",
  "commit": "${COMMIT_HASH}",
  "branch": "${BRANCH}",
  "buildNumber": "${BUILD_NUMBER}",
  "buildTime": "${BUILD_TIME}",
  "timezone": "Asia/Shanghai",
  "environment": "development"
}
EOF
cat version.json

echo "========================================"
echo "Installing parent POM to local repository"
echo "========================================"
$MVN_BIN install -N -DskipTests -q || { echo "ERROR: Failed to install parent POM"; exit 1; }

echo "========================================"
echo "Building common-service (dependency)"
echo "========================================"
if [ -d "services/common-service" ]; then
    cd services/common-service || { echo "ERROR: Failed to cd to common-service"; exit 1; }
    $MVN_BIN clean install -DskipTests -q || { echo "ERROR: Failed to build common-service"; exit 1; }
    cd "${WORKSPACE}" || true
else
    echo "WARN: common-service directory not found"
fi

echo "========================================"
echo "Building microservices"
echo "========================================"
SERVICES="api-gateway auth-service user-service map-service order-service merchant-service platform-service product-service rider-service upload-service marketing-service"
FAILED_SERVICES=""

for service in ${SERVICES}; do
    SERVICE_DIR="services/${service}"
    if [ -d "${SERVICE_DIR}" ]; then
        echo "Building ${service}..."
        cd "${SERVICE_DIR}" || { echo "ERROR: Failed to cd to ${service}"; FAILED_SERVICES="${FAILED_SERVICES} ${service}"; continue; }

        if $MVN_BIN clean install -DskipTests -q; then
            echo "SUCCESS: ${service} built successfully"
        else
            echo "FAILED: ${service} build failed"
            FAILED_SERVICES="${FAILED_SERVICES} ${service}"
        fi

        cd "${WORKSPACE}" || true
    else
        echo "WARN: ${service} directory not found, skipping"
    fi
done

if [ -n "${FAILED_SERVICES}" ]; then
    echo "========================================"
    echo "ERROR: Build failed for services: ${FAILED_SERVICES}"
    echo "========================================"
    exit 1
fi

echo "========================================"
echo "Build completed successfully!"
echo "Version: ${VERSION}"
echo "========================================"
]]></command>
      <configuredLocalRules/>
    </hudson.tasks.Shell>
  </builders>
  <publishers/>
  <buildWrappers/>
</project>