FROM eclipse-temurin:17-jdk-alpine AS builder

WORKDIR /app

# 复制Maven相关文件
COPY pom.xml .
COPY common ./common
COPY services/dashboard-service ./services/dashboard-service

# 下载依赖（利用缓存）
RUN apt-get update && apt-get install -y maven && \
    mvn dependency:go-offline -B && \
    apt-get clean

# 构建项目
RUN mvn clean package -DskipTests -B

# 运行镜像
FROM eclipse-temurin:17-jre-alpine

WORKDIR /app

# 添加非root用户
RUN addgroup -g 1001 -S erp && \
    adduser -u 1001 -S erp -G erp

# 复制JAR文件
COPY --from=builder /app/services/dashboard-service/target/*.jar app.jar

# 修改权限
RUN chown -R erp:erp /app

USER erp

EXPOSE 8086

ENV JAVA_OPTS="-Xms512m -Xmx1g -XX:+UseG1GC -XX:MaxGCPauseMillis=200"

ENTRYPOINT ["sh", "-c", "java $JAVA_OPTS -jar app.jar"]
