欢迎光临
我们一直在努力

Java 生产环境 Maven 实战指南

目录

一、生产环境基础架构(必备三件套)

1. 企业 Maven 私服(Nexus3/Apache Archiva)

2. 版本规范(生产强制规约)

二、POM.xml 生产核心实战配置(多环境 + 依赖 + 打包)

1. 多环境 Profile(dev/test/prod,区分配置文件、编译参数)

2. 依赖管控(生产重中之重:版本统一、排除冲突、禁用快照依赖)

(1)父工程 dependencyManagement 统一版本(多模块项目必用)

(2)依赖冲突 exclusion(常见 log4j、slf4j 冲突)

(3)生产禁用 SNAPSHOT 依赖

3. 打包优化(瘦身、剔除无用依赖、指定 JDK 版本)

① 固定编译 JDK(统一生产 JDK8/JDK17)

② SpringBoot 打包插件 + 瘦身(spring-boot-maven-plugin)

③ maven-dependency-plugin 提取依赖包(分离 lib 包,部署扩容不用重复传大 jar)

三、生产常用 Maven 命令(CI / 脚本部署专用)

1. 日常打包

2. 上传私服(release/snapshot,发布组件包)

3. 依赖排查(解决 jar 冲突神器)

4. 安全漏洞扫描(生产上线门禁:OWASP 依赖漏洞)

四、多模块项目生产实战(聚合 + 继承)

五、CI/CD 流水线集成 Maven(Jenkins/GitLab CI 生产标准流程)

六、生产避坑要点

七、插件生产扩展


核心思路:生产 Maven = 私服 + 多环境 profile + 依赖管控 + 打包瘦身 + 安全扫描 + CI 流水线 + 版本规范,杜绝本地直接mvn clean package上线。

一、生产环境基础架构(必备三件套)

1. 企业 Maven 私服(Nexus3/Apache Archiva)

生产禁止直连公网 Maven 中央仓库,统一私服代理:

  • 仓库分类(Nexus 标准划分)
    • maven-public:聚合仓库,开发 / 打包唯一配置源(release+snapshot+proxy)
    • maven-releases:正式稳定包(版本不带 – SNAPSHOT,不可覆盖上传)
    • maven-snapshots:测试快照包(带 – SNAPSHOT,可重复覆盖)
    • maven-proxy:代理中央仓、阿里云 Maven、第三方开源仓
  • settings.xml 全局配置(服务器统一存放:/usr/local/maven/conf/settings.xml) 生产机器共用一套 settings,不使用用户目录.m2/settings.xml,核心配置:
  • <servers>
    <!– 私服上传凭证,打包deploy时用 –>
    <server>
    <id>nexus-releases</id>
    <username>deploy</username>
    <password>生产加密密码</password>
    </server>
    <server>
    <id>nexus-snapshots</id>
    <username>deploy</username>
    <password>生产加密密码</password>
    </server>
    </servers>
    <mirrors>
    <mirror>
    <id>nexus</id>
    <mirrorOf>external:*,!central</mirrorOf>
    <url>http://私服IP:8081/repository/maven-public/</url>
    </mirror>
    </mirrors>
    <profiles>
    <profile>
    <id>prod</id>
    <repositories>
    <repository>
    <id>nexus-public</id>
    <url>http://私服IP/repository/maven-public/</url>
    <releases><enabled>true</enabled></releases>
    <snapshots><enabled>true</enabled></snapshots>
    </repository>
    </repositories>
    </profile>
    </profiles>
    <activeProfiles>
    <activeProfile>prod</activeProfile>
    </activeProfiles>

    密码优化:生产用{加密串}密文密码,避免明文泄露。

    2. 版本规范(生产强制规约)

    <!– snapshot:测试环境迭代 –>
    <version>2.3.0-SNAPSHOT</version>
    <!– release:生产上线版本,无SNAPSHOT,遵循 大版本.功能.迭代 –>
    <version>2.3.5</version>

    • SNAPSHOT 包:只能部署测试环境,禁止上线生产;
    • Release 包:打包后 deploy 到 nexus-releases,不可重复上传,版本号不重复。

    二、POM.xml 生产核心实战配置(多环境 + 依赖 + 打包)

    1. 多环境 Profile(dev/test/prod,区分配置文件、编译参数)

    场景:application-dev.yml/application-prod.yml,打包时动态切换环

    <profiles>
    <!– 开发环境 –>
    <profile>
    <id>dev</id>
    <properties>
    <env>dev</env>
    </properties>
    <activation><activeByDefault>true</activeByDefault></activation>
    </profile>
    <!– 测试环境 –>
    <profile>
    <id>test</id>
    <properties><env>test</env></properties>
    </profile>
    <!– 生产环境(上线必启用) –>
    <profile>
    <id>prod</id>
    <properties><env>prod</env></properties>
    </profile>
    </profiles>

    <!– SpringBoot资源过滤,动态读取对应环境配置 –>
    <build>
    <resources>
    <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
    <excludes>
    <exclude>application-*.yml</exclude>
    </excludes>
    </resource>
    <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
    <includes>
    <include>application-${env}.yml</include>
    </includes>
    </resource>
    </resources>
    </build>

    打包命令

    # 生产打包,启用prod环境
    mvn clean package -Pprod -Dmaven.test.skip=true

    2. 依赖管控(生产重中之重:版本统一、排除冲突、禁用快照依赖)

    (1)父工程 dependencyManagement 统一版本(多模块项目必用)

    <!– parent pom统一所有子模块依赖版本,子模块无需写version –>
    <dependencyManagement>
    <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.7.15</version>
    <type>pom</type>
    <scope>import</scope>
    </dependency>
    <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.2.20</version>
    </dependency>
    </dependencies>
    </dependencyManagement>

    (2)依赖冲突 exclusion(常见 log4j、slf4j 冲突)

    <dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <exclusions>
    <exclusion>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    </exclusion>
    </exclusions>
    </dependency>

    (3)生产禁用 SNAPSHOT 依赖

    settings 或 pom 配置,生产打包强制拦截快照包:

    <repositories>
    <repository>
    <id>nexus</id>
    <releases><enabled>true</enabled></releases>
    <snapshots><enabled>false</enabled></snapshots>
    </repository>
    </repositories>

    3. 打包优化(瘦身、剔除无用依赖、指定 JDK 版本)

    ① 固定编译 JDK(统一生产 JDK8/JDK17)

    <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    ② SpringBoot 打包插件 + 瘦身(spring-boot-maven-plugin)

    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>2.7.15</version>
    <configuration>
    <!– 生产打包排除开发依赖,如devtools –>
    <excludeDevtools>true</excludeDevtools>
    </configuration>
    <executions>
    <execution>
    <goals>
    <goal>repackage</goal>
    </goals>
    </execution>
    </executions>
    </plugin>

    ③ maven-dependency-plugin 提取依赖包(分离 lib 包,部署扩容不用重复传大 jar)

    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
    <execution>
    <phase>package</phase>
    <goals><goal>copy-dependencies</goal></goals>
    <configuration>
    <outputDirectory>${project.build.directory}/lib</outputDirectory>
    <excludeScope>provided</excludeScope>
    </configuration>
    </execution>
    </executions>
    </plugin>

    打包后:target/lib存放所有依赖、target/*.jar为业务 jar,部署时 lib 挂载外置,减小镜像体积。

    三、生产常用 Maven 命令(CI / 脚本部署专用)

    1. 日常打包

    # 跳过单元测试打包(生产标准)
    mvn clean package -Pprod -DskipTests
    # 执行单元测试打包(测试环境必跑,生产可选)
    mvn clean package -Ptest

    2. 上传私服(release/snapshot,发布组件包)

    # snapshot版本上传私服snapshots库
    mvn clean deploy -Ptest -DskipTests
    # release正式包上传releases库(上线前发布公共依赖)
    mvn clean deploy -Pprod -DskipTests

    3. 依赖排查(解决 jar 冲突神器)

    # 查看依赖树,定位冲突
    mvn dependency:tree > dep.log
    # 查看指定jar依赖来源
    mvn dependency:tree | grep slf4j
    # 分析依赖冲突
    mvn dependency:analyze

    4. 安全漏洞扫描(生产上线门禁:OWASP 依赖漏洞)

    # owasp漏洞检测,高危漏洞阻断打包
    mvn org.owasp:dependency-check-maven:check -Pprod

    四、多模块项目生产实战(聚合 + 继承)

  • 父工程(pom 打包):统一版本、依赖管理、公共 properties
  • <packaging>pom</packaging>
    <modules>
    <module>common-core</module>
    <module>system-service</module>
    <module>gateway</module>
    </modules>

  • 公共模块 common-core:打成 jar 上传 nexus,业务模块直接引用
  • <!– 子模块引用,无需version –>
    <dependency>
    <groupId>com.xxx</groupId>
    <artifactId>common-core</artifactId>
    </dependency>

  • 聚合打包:根目录一键打包所有子服务
  • mvn clean package -Pprod -DskipTests

    五、CI/CD 流水线集成 Maven(Jenkins/GitLab CI 生产标准流程)

    # Jenkins流水线脚本步骤
    1. 拉取git代码
    2. mvn clean verify -Ptest (单元测试+漏洞扫描,失败阻断构建)
    3. mvn clean package -Pprod -DskipTests (生产打包)
    4. mvn deploy -Pprod (公共组件上传nexus)
    5. Docker build 基于打包后的jar构建镜像
    6. 推送镜像到镜像仓库,k8s部署

    六、生产避坑要点

  • 严禁本地 jar 手动导入:mvn install:install-file只临时调试,正式依赖上传 nexus;
  • 生产永远不使用 latest 版本:版本固定,避免依赖静默升级引发线上 bug;
  • 统一 maven 版本:所有打包机器、CI 使用同一个 Maven 版本(3.8.6 稳定版);
  • 私服定时同步:Nexus 定期拉取中央仓缓存,避免外网故障打包失败;
  • provided 依赖:容器环境 tomcat/jetty 依赖设为 provided,避免 jar 包冲突。
  • 七、插件生产扩展

  • maven-enforcer-plugin:强制 JDK 版本、禁止 SNAPSHOT 依赖、依赖版本规范校验;
  • maven-source-plugin:打包源码上传私服,便于线上排错看源码;
  • maven-jacoco-plugin:单元测试覆盖率统计,准入门禁。
  • 赞(0)
    未经允许不得转载:171主机测评 » Java 生产环境 Maven 实战指南
    分享到: 更多 (0)

    评论 抢沙发

    • 昵称 (必填)
    • 邮箱 (必填)
    • 网址