
目录一、生产环境基础架构必备三件套1. 企业 Maven 私服Nexus3/Apache Archiva2. 版本规范生产强制规约二、POM.xml 生产核心实战配置多环境 依赖 打包1. 多环境 Profiledev/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 流水线集成 MavenJenkins/GitLab CI 生产标准流程六、生产避坑要点七、插件生产扩展核心思路生产 Maven 私服 多环境 profile 依赖管控 打包瘦身 安全扫描 CI 流水线 版本规范杜绝本地直接mvn clean package上线。一、生产环境基础架构必备三件套1. 企业 Maven 私服Nexus3/Apache Archiva生产禁止直连公网 Maven 中央仓库统一私服代理仓库分类Nexus 标准划分maven-public聚合仓库开发 / 打包唯一配置源releasesnapshotproxymaven-releases正式稳定包版本不带 - SNAPSHOT不可覆盖上传maven-snapshots测试快照包带 - SNAPSHOT可重复覆盖maven-proxy代理中央仓、阿里云 Maven、第三方开源仓settings.xml 全局配置服务器统一存放/usr/local/maven/conf/settings.xml生产机器共用一套 settings不使用用户目录.m2/settings.xml核心配置servers !-- 私服上传凭证打包deploy时用 -- server idnexus-releases/id usernamedeploy/username password生产加密密码/password /server server idnexus-snapshots/id usernamedeploy/username password生产加密密码/password /server /servers mirrors mirror idnexus/id mirrorOfexternal:*,!central/mirrorOf urlhttp://私服IP:8081/repository/maven-public//url /mirror /mirrors profiles profile idprod/id repositories repository idnexus-public/id urlhttp://私服IP/repository/maven-public//url releasesenabledtrue/enabled/releases snapshotsenabledtrue/enabled/snapshots /repository /repositories /profile /profiles activeProfiles activeProfileprod/activeProfile /activeProfiles密码优化生产用{加密串}密文密码避免明文泄露。2. 版本规范生产强制规约!-- snapshot测试环境迭代 -- version2.3.0-SNAPSHOT/version !-- release生产上线版本无SNAPSHOT遵循 大版本.功能.迭代 -- version2.3.5/versionSNAPSHOT 包只能部署测试环境禁止上线生产Release 包打包后 deploy 到 nexus-releases不可重复上传版本号不重复。二、POM.xml 生产核心实战配置多环境 依赖 打包1. 多环境 Profiledev/test/prod区分配置文件、编译参数场景application-dev.yml/application-prod.yml打包时动态切换环profiles !-- 开发环境 -- profile iddev/id properties envdev/env /properties activationactiveByDefaulttrue/activeByDefault/activation /profile !-- 测试环境 -- profile idtest/id propertiesenvtest/env/properties /profile !-- 生产环境上线必启用 -- profile idprod/id propertiesenvprod/env/properties /profile /profiles !-- SpringBoot资源过滤动态读取对应环境配置 -- build resources resource directorysrc/main/resources/directory filteringtrue/filtering excludes excludeapplication-*.yml/exclude /excludes /resource resource directorysrc/main/resources/directory filteringtrue/filtering includes includeapplication-${env}.yml/include /includes /resource /resources /build打包命令# 生产打包启用prod环境 mvn clean package -Pprod -Dmaven.test.skiptrue2. 依赖管控生产重中之重版本统一、排除冲突、禁用快照依赖1父工程 dependencyManagement 统一版本多模块项目必用!-- parent pom统一所有子模块依赖版本子模块无需写version -- dependencyManagement dependencies dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-dependencies/artifactId version2.7.15/version typepom/type scopeimport/scope /dependency dependency groupIdcom.alibaba/groupId artifactIddruid-spring-boot-starter/artifactId version1.2.20/version /dependency /dependencies /dependencyManagement2依赖冲突 exclusion常见 log4j、slf4j 冲突dependency groupIdorg.apache.dubbo/groupId artifactIddubbo-spring-boot-starter/artifactId exclusions exclusion groupIdorg.slf4j/groupId artifactIdslf4j-api/artifactId /exclusion /exclusions /dependency3生产禁用 SNAPSHOT 依赖settings 或 pom 配置生产打包强制拦截快照包repositories repository idnexus/id releasesenabledtrue/enabled/releases snapshotsenabledfalse/enabled/snapshots /repository /repositories3. 打包优化瘦身、剔除无用依赖、指定 JDK 版本① 固定编译 JDK统一生产 JDK8/JDK17properties maven.compiler.source1.8/maven.compiler.source maven.compiler.target1.8/maven.compiler.target maven.compiler.encodingUTF-8/maven.compiler.encoding project.build.sourceEncodingUTF-8/project.build.sourceEncoding /properties② SpringBoot 打包插件 瘦身spring-boot-maven-pluginplugin groupIdorg.springframework.boot/groupId artifactIdspring-boot-maven-plugin/artifactId version2.7.15/version configuration !-- 生产打包排除开发依赖如devtools -- excludeDevtoolstrue/excludeDevtools /configuration executions execution goals goalrepackage/goal /goals /execution /executions /plugin③ maven-dependency-plugin 提取依赖包分离 lib 包部署扩容不用重复传大 jarplugin groupIdorg.apache.maven.plugins/groupId artifactIdmaven-dependency-plugin/artifactId executions execution phasepackage/phase goalsgoalcopy-dependencies/goal/goals configuration outputDirectory${project.build.directory}/lib/outputDirectory excludeScopeprovided/excludeScope /configuration /execution /executions /plugin打包后target/lib存放所有依赖、target/*.jar为业务 jar部署时 lib 挂载外置减小镜像体积。三、生产常用 Maven 命令CI / 脚本部署专用1. 日常打包# 跳过单元测试打包生产标准 mvn clean package -Pprod -DskipTests # 执行单元测试打包测试环境必跑生产可选 mvn clean package -Ptest2. 上传私服release/snapshot发布组件包# snapshot版本上传私服snapshots库 mvn clean deploy -Ptest -DskipTests # release正式包上传releases库上线前发布公共依赖 mvn clean deploy -Pprod -DskipTests3. 依赖排查解决 jar 冲突神器# 查看依赖树定位冲突 mvn dependency:tree dep.log # 查看指定jar依赖来源 mvn dependency:tree | grep slf4j # 分析依赖冲突 mvn dependency:analyze4. 安全漏洞扫描生产上线门禁OWASP 依赖漏洞# owasp漏洞检测高危漏洞阻断打包 mvn org.owasp:dependency-check-maven:check -Pprod四、多模块项目生产实战聚合 继承父工程pom 打包统一版本、依赖管理、公共 propertiespackagingpom/packaging modules modulecommon-core/module modulesystem-service/module modulegateway/module /modules公共模块 common-core打成 jar 上传 nexus业务模块直接引用!-- 子模块引用无需version -- dependency groupIdcom.xxx/groupId artifactIdcommon-core/artifactId /dependency聚合打包根目录一键打包所有子服务mvn clean package -Pprod -DskipTests五、CI/CD 流水线集成 MavenJenkins/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单元测试覆盖率统计准入门禁。