私有Maven仓库在企业级开发中扮演着重要角色:
| 产品 | 开源版本 | 企业版本 | 特点 | 推荐场景 |
|---|---|---|---|---|
| Nexus Repository | ✅ | ✅ | 功能强大,社区活跃 | 中大型企业 |
| JFrog Artifactory | ✅ | ✅ | 支持多种格式,云原生 | 大型企业 |
| Apache Archiva | ✅ | ❌ | 轻量级,易部署 | 小型团队 |
| 阿里云Maven私服 | ❌ | ✅ | 托管服务,免运维 | 快速上线 |
本指南以 Nexus Repository 3 为例,原因:
|
1 2 3 4 5 6 7 8 9 10 |
# 创建数据目录 mkdir -p /data/nexus-data chmod -R 777 /data/nexus-data # 运行Nexus容器 docker run -d \ --name nexus3 \ --restart=always \ -p 8081:8081 \ -v /data/nexus-data:/nexus-data \ sonatype/nexus3:latest |
创建 docker-compose.yml:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
version: '3.8' services: nexus: image: sonatype/nexus3:3.63.0 container_name: nexus3 restart: always ports: - "8081:8081" # Web UI端口 - "8082:8082" # Docker私有仓库端口(可选) volumes: - nexus-data:/nexus-data environment: - INSTALL4J_ADD_VM_PARAMS=-Xms2g -Xmx2g -XX:MaxDirectMemorySize=3g ulimits: nofile: soft: 65536 hard: 65536 volumes: nexus-data: driver: local |
启动服务:
|
1 2 3 4 5 6 |
# 启动Nexus docker-compose up -d # 查看日志 docker-compose logs -f nexus # 等待启动完成(首次启动需要几分钟) docker-compose logs nexus | grep "Started Sonatype Nexus" |
|
1 2 3 4 |
# 查看初始admin密码 docker exec nexus3 cat /nexus-data/admin.password # 或者在宿主机查看 cat /data/nexus-data/admin.password |
http://your-server-ip:8081admin|
1 |
kubectl create namespace nexus |
创建 nexus-pvc.yaml:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: nexus-pvc namespace: nexus spec: accessModes: - ReadWriteOnce resources: requests: storage: 100Gi storageClassName: alicloud-disk-ssd # 根据实际情况修改 |
应用配置:
|
1 |
kubectl apply -f nexus-pvc.yaml |
创建 nexus-deployment.yaml:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
apiVersion: apps/v1 kind: Deployment metadata: name: nexus3 namespace: nexus labels: app: nexus3 spec: replicas: 1 selector: matchLabels: app: nexus3 template: metadata: labels: app: nexus3 spec: containers: - name: nexus3 image: sonatype/nexus3:3.63.0 imagePullPolicy: IfNotPresent ports: - containerPort: 8081 name: web protocol: TCP env: - name: INSTALL4J_ADD_VM_PARAMS value: "-Xms2g -Xmx2g -XX:MaxDirectMemorySize=3g" resources: requests: memory: "4Gi" cpu: "1000m" limits: memory: "6Gi" cpu: "2000m" volumeMounts: - name: nexus-data mountPath: /nexus-data livenessProbe: httpGet: path: / port: 8081 initialDelaySeconds: 180 periodSeconds: 30 timeoutSeconds: 10 readinessProbe: httpGet: path: / port: 8081 initialDelaySeconds: 60 periodSeconds: 30 timeoutSeconds: 10 volumes: - name: nexus-data persistentVolumeClaim: claimName: nexus-pvc |
应用配置:
|
1 |
kubectl apply -f nexus-deployment.yaml |
创建 nexus-service.yaml:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
apiVersion: v1 kind: Service metadata: name: nexus3 namespace: nexus labels: app: nexus3 spec: type: ClusterIP ports: - port: 8081 targetPort: 8081 protocol: TCP name: web selector: app: nexus3 |
应用配置:
|
1 |
kubectl apply -f nexus-service.yaml |
创建 nexus-ingress.yaml:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: nexus3 namespace: nexus annotations: nginx.ingress.kubernetes.io/proxy-body-size: "0" nginx.ingress.kubernetes.io/proxy-read-timeout: "600" nginx.ingress.kubernetes.io/proxy-send-timeout: "600" spec: ingressClassName: nginx rules: - host: nexus.yourdomain.com http: paths: - path: / pathType: Prefix backend: service: name: nexus3 port: number: 8081 tls: - hosts: - nexus.yourdomain.com secretName: nexus-tls-secret # 需要提前创建SSL证书 |
应用配置:
|
1 |
kubectl apply -f nexus-ingress.yaml |
|
1 2 3 4 5 6 |
# 查看Pod状态 kubectl get pods -n nexus # 查看日志 kubectl logs -f deployment/nexus3 -n nexus # 获取初始密码 kubectl exec -n nexus deployment/nexus3 -- cat /nexus-data/admin.password |
登录Nexus后,进行仓库配置。
maven-releases-blob重复以上步骤创建:
maven-snapshots-blobmaven-central-blob|
1 2 3 4 5 |
Name: maven-releases Version policy: Release Layout policy: Strict Blob store: maven-releases-blob Deployment policy: Allow redeploy (生产环境建议Disable redeploy) |
|
1 2 3 4 5 |
Name: maven-snapshots Version policy: Snapshot Layout policy: Strict Blob store: maven-snapshots-blob Deployment policy: Allow redeploy |
|
1 2 3 4 5 6 |
Name: maven-central Version policy: Release Remote storage: https://repo1.maven.org/maven2/ Blob store: maven-central-blob Maximum component age: 1440 (24小时) Maximum metadata age: 1440 |
推荐添加阿里云镜像以加速国内访问:
|
1 2 3 4 |
Name: maven-aliyun Version policy: Release Remote storage: https://maven.aliyun.com/repository/public Blob store: maven-central-blob |
|
1 2 |
Name: maven-public Blob store: default |
|
1 2 3 4 |
1. maven-releases 2. maven-snapshots 3. maven-aliyun 4. maven-central |
|
1 2 3 4 5 |
maven-public (group) ├── maven-releases (hosted) # 内部releases版本 ├── maven-snapshots (hosted) # 内部snapshots版本 ├── maven-aliyun (proxy) # 阿里云代理 └── maven-central (proxy) # Maven Central代理 |
丁乔:创建角色 nx-deploy(我装的3.91.0,没有nx-deploy)

|
1 2 3 4 5 6 7 |
ID: deployment First name: Deployment Last name: User Email: deployment@yourdomain.com Status: Active Roles: nx-deploy (添加部署权限) Password: 设置强密码 |
|
1 2 3 4 5 6 7 |
ID: developer First name: Developer Last name: User Email: developer@yourdomain.com Status: Active Roles: nx-anonymous (只读权限) Password: 设置密码 |
如果希望内网用户无需认证即可拉取依赖:
注意:生产环境建议禁用匿名访问,使用账号密码认证。
Maven的全局配置文件通常位于:
C:\Users\{用户名}\.m2\settings.xml~/.m2/settings.xml|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
<?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <!-- 本地仓库路径 --> <localRepository>D:/maven-repository</localRepository> <!-- 服务器认证信息 --> <servers> <!-- Nexus部署用户(用于发布构件) --> <server> <id>nexus-releases</id> <username>deployment</username> <password>your-deployment-password</password> </server> <server> <id>nexus-snapshots</id> <username>deployment</username> <password>your-deployment-password</password> </server> <!-- Nexus下载用户(用于拉取依赖) --> <server> <id>nexus-public</id> <username>developer</username> <password>your-developer-password</password> </server> </servers> <!-- 镜像配置 --> <mirrors> <!-- 配置Nexus为所有仓库的镜像 --> <mirror> <id>nexus-public</id> <mirrorOf>*</mirrorOf> <name>Nexus Public Repository</name> <url>http://your-nexus-server:8081/repository/maven-public/</url> </mirror> </mirrors> <!-- Profile配置 --> <profiles> <profile> <id>nexus</id> <repositories> <!-- 从Nexus下载releases版本 --> <repository> <id>nexus-public</id> <name>Nexus Public Repository</name> <url>http://your-nexus-server:8081/repository/maven-public/</url> <releases> <enabled>true</enabled> <updatePolicy>daily</updatePolicy> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots> </repository> </repositories> <!-- 从Nexus下载插件 --> <pluginRepositories> <pluginRepository> <id>nexus-public</id> <name>Nexus Public Repository</name> <url>http://your-nexus-server:8081/repository/maven-public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository> </pluginRepositories> </profile> </profiles> <!-- 激活profile --> <activeProfiles> <activeProfile>nexus</activeProfile> </activeProfiles> </settings> |
1. localRepository
2. servers
<id> 必须与 pom.xml 中的 <id> 匹配3. mirrors
<mirrorOf>*</mirrorOf> 表示代理所有仓库<mirrorOf>central</mirrorOf> 只代理中央仓库4. profiles
updatePolicy 配置更新策略:
always: 每次构建都检查更新daily: 每天检查一次(默认)never: 从不检查更新在项目的 pom.xml 中添加发布配置:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <!-- 项目坐标 --> <groupId>com.tigeriot</groupId> <artifactId>product-manager-service</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>jar</packaging> <name>Product Manager Service</name> <description>产品管理服务</description> <!-- 发布配置 --> <distributionManagement> <!-- releases版本发布地址 --> <repository> <id>nexus-releases</id> <name>Nexus Release Repository</name> <url>http://your-nexus-server:8081/repository/maven-releases/</url> </repository> <!-- snapshots版本发布地址 --> <snapshotRepository> <id>nexus-snapshots</id> <name>Nexus Snapshot Repository</name> <url>http://your-nexus-server:8081/repository/maven-snapshots/</url> </snapshotRepository> </distributionManagement> <!-- 仓库配置(用于下载依赖) --> <repositories> <repository> <id>nexus-public</id> <name>Nexus Public Repository</name> <url>http://your-nexus-server:8081/repository/maven-public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <!-- 插件仓库配置 --> <pluginRepositories> <pluginRepository> <id>nexus-public</id> <name>Nexus Public Repository</name> <url>http://your-nexus-server:8081/repository/maven-public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository> </pluginRepositories> <!-- 构建配置 --> <build> <plugins> <!-- Maven Deploy插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-deploy-plugin</artifactId> <version>3.1.1</version> </plugin> <!-- Maven Source插件(发布源码) --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>3.3.0</version> <executions> <execution> <id>attach-sources</id> <phase>verify</phase> <goals> <goal>jar-no-fork</goal> </goals> </execution> </executions> </plugin> <!-- Maven Javadoc插件(发布文档) --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <version>3.6.3</version> <configuration> <encoding>UTF-8</encoding> <charset>UTF-8</charset> <docencoding>UTF-8</docencoding> <!-- 忽略Javadoc错误 --> <failOnError>false</failOnError> <doclint>none</doclint> </configuration> <executions> <execution> <id>attach-javadocs</id> <phase>verify</phase> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project> |
对于多模块项目,在父 pom.xml 中配置:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.tigeriot</groupId> <artifactId>microservices-parent</artifactId> <version>1.0.0</version> <packaging>pom</packaging> <name>Microservices Parent</name> <!-- 子模块 --> <modules> <module>product-manager-service</module> <module>user-service</module> <module>order-service</module> </modules> <!-- 发布配置(子模块会继承) --> <distributionManagement> <repository> <id>nexus-releases</id> <name>Nexus Release Repository</name> <url>http://your-nexus-server:8081/repository/maven-releases/</url> </repository> <snapshotRepository> <id>nexus-snapshots</id> <name>Nexus Snapshot Repository</name> <url>http://your-nexus-server:8081/repository/maven-snapshots/</url> </snapshotRepository> </distributionManagement> <!-- 统一依赖管理 --> <dependencyManagement> <dependencies> <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.7.18</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-deploy-plugin</artifactId> <version>3.1.1</version> </plugin> </plugins> </pluginManagement> </build> </project> |
确保 pom.xml 中版本号包含 -SNAPSHOT 后缀:
|
1 |
<version>1.0.0-SNAPSHOT</version> |
执行发布命令:
|
1 2 3 4 5 6 |
# 清理并发布 mvn clean deploy # 跳过测试发布 mvn clean deploy -DskipTests # 指定profile发布 mvn clean deploy -P nexus -DskipTests |
pom.xml 版本号,去掉 -SNAPSHOT:|
1 |
<version>1.0.0</version> |
|
1 |
mvn clean deploy -DskipTests |
-SNAPSHOT:|
1 |
<version>1.0.1-SNAPSHOT</version> |
如果已经构建过,只想发布:
|
1 2 3 4 5 6 7 8 |
mvn deploy:deploy-file \ -DgroupId=com.tigeriot \ -DartifactId=product-manager-service \ -Dversion=1.0.0 \ -Dpackaging=jar \ -Dfile=target/product-manager-service-1.0.0.jar \ -DrepositoryId=nexus-releases \ -Durl=http://your-nexus-server:8081/repository/maven-releases/ |
在父模块根目录执行:
|
1 2 3 4 5 |
# 发布所有子模块 mvn clean deploy -DskipTests # 只发布某个子模块 cd product-manager-service mvn clean deploy -DskipTests |
有时需要发布第三方提供的jar包(没有源码)。
|
1 2 3 4 5 6 7 8 |
mvn deploy:deploy-file \ -DgroupId=com.thirdparty \ -DartifactId=third-party-lib \ -Dversion=1.0.0 \ -Dpackaging=jar \ -Dfile=/path/to/third-party-lib-1.0.0.jar \ -DrepositoryId=nexus-releases \ -Durl=http://your-nexus-server:8081/repository/maven-releases/ |
参数说明:
groupId: 自定义group IDartifactId: 自定义artifact IDversion: 版本号packaging: 打包类型(jar/war/pom等)file: jar文件路径repositoryId: settings.xml中配置的server IDurl: Nexus仓库地址|
1 2 3 4 5 6 7 8 9 |
mvn deploy:deploy-file \ -DgroupId=com.thirdparty \ -DartifactId=third-party-lib \ -Dversion=1.0.0 \ -Dpackaging=jar \ -Dfile=/path/to/third-party-lib-1.0.0.jar \ -DpomFile=/path/to/third-party-lib-1.0.0.pom \ -DrepositoryId=nexus-releases \ -Durl=http://your-nexus-server:8081/repository/maven-releases/ |
maven-releasescom.thirdpartythird-party-lib1.0.0jar创建 upload-third-party.sh:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#!/bin/bash NEXUS_URL="http://your-nexus-server:8081/repository/maven-releases/" REPO_ID="nexus-releases" # 上传单个jar upload_jar() { local group=$1 local artifact=$2 local version=$3 local file=$4 echo "Uploading $artifact-$version..." mvn deploy:deploy-file \ -DgroupId=$group \ -DartifactId=$artifact \ -Dversion=$version \ -Dpackaging=jar \ -Dfile=$file \ -DrepositoryId=$REPO_ID \ -Durl=$NEXUS_URL } # 示例:上传多个jar包 upload_jar "com.oracle" "ojdbc8" "19.3.0.0" "libs/ojdbc8-19.3.0.0.jar" upload_jar "com.alibaba" "druid" "1.2.20" "libs/druid-1.2.20.jar" upload_jar "com.github" "pagehelper" "5.3.2" "libs/pagehelper-5.3.2.jar" echo "All uploads completed!" |
执行脚本:
|
1 2 |
chmod +x upload-third-party.sh ./upload-third-party.sh |
确保 settings.xml 和 pom.xml 已正确配置后,直接在 pom.xml 中添加依赖:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<dependencies> <!-- 从私有仓库拉取内部组件 --> <dependency> <groupId>com.tigeriot</groupId> <artifactId>common-utils</artifactId> <version>1.0.0</version> </dependency> <!-- 拉取SNAPSHOT版本 --> <dependency> <groupId>com.tigeriot</groupId> <artifactId>api-gateway</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> <!-- 从私有仓库拉取第三方jar --> <dependency> <groupId>com.thirdparty</groupId> <artifactId>third-party-lib</artifactId> <version>1.0.0</version> </dependency> </dependencies> |
|
1 2 3 4 5 6 7 8 9 |
# 更新所有依赖(包括SNAPSHOT) mvn clean install -U # 或者 mvn clean install --update-snapshots # 查看依赖树 mvn dependency:tree # 下载源码和文档 mvn dependency:sources mvn dependency:resolve -Dclassifier=javadoc |
如果遇到依赖问题,可以清理本地缓存:
|
1 2 3 4 5 |
# 清理特定依赖 mvn dependency:purge-local-repository \ -DmanualInclude=com.tigeriot:common-utils # 清理所有本地依赖并重新下载 mvn dependency:purge-local-repository -DreResolve=false |
问题:发布时提示401 Unauthorized
解决方案:
settings.xml 中的 <server> 配置<id> 与 pom.xml 中的 <repository><id> 匹配|
1 2 3 4 5 6 |
<!-- settings.xml --> <server> <id>nexus-releases</id> <!-- 必须与pom.xml中的id一致 --> <username>deployment</username> <password>correct-password</password> </server> |
问题:Maven无法从Nexus下载依赖
解决方案:
settings.xml 中的镜像配置docker logs nexus3rm -rf ~/.m2/repository/com/tigeriot问题:发布SNAPSHOT版本时提示不允许重复部署
解决方案:
在Nexus中配置 maven-snapshots 仓库:
maven-snapshots问题:HTTPS连接提示证书验证失败
解决方案1:信任证书
|
1 2 3 4 5 6 |
# 导出Nexus证书 openssl s_client -showcerts -connect your-nexus-server:8081 \ </dev/null 2>/dev/null | openssl x509 -outform PEM > nexus.crt # 导入到Java信任库 keytool -import -alias nexus -keystore $JAVA_HOME/jre/lib/security/cacerts \ -file nexus.crt -storepass changeit |
解决方案2:使用HTTP(不推荐生产环境)
在 settings.xml 和 pom.xml 中使用 http:// 而非 https://
问题:上传大文件时超时
解决方案:
|
1 2 3 4 5 6 |
location / { proxy_pass http://nexus:8081; proxy_read_timeout 600s; proxy_send_timeout 600s; client_max_body_size 1024M; } |
pom.xml):|
1 2 3 4 5 6 7 8 9 10 11 |
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-deploy-plugin</artifactId> <configuration> <retryFailedDeploymentCount>3</retryFailedDeploymentCount> </configuration> </plugin> </plugins> </build> |
问题:依赖版本冲突
解决方案:
|
1 2 3 4 5 |
# 查看依赖树 mvn dependency:tree # 查看冲突 mvn dependency:tree -Dverbose # 在pom.xml中排除冲突依赖 |
|
1 2 3 4 5 6 7 8 9 10 11 |
<dependency> <groupId>com.tigeriot</groupId> <artifactId>common-utils</artifactId> <version>1.0.0</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </exclusion> </exclusions> </dependency> |
问题:Nexus磁盘空间占用过大
解决方案:配置自动清理策略
|
1 2 3 |
Name: cleanup-snapshots Format: maven2 Last downloaded: 30 days |
maven-snapshots 仓库maven-snapshots遵循语义化版本规范(Semantic Versioning):
|
1 2 3 4 |
1.0.0 -> 正式发布 1.0.1 -> Bug修复 1.1.0 -> 新增功能 2.0.0 -> 重大变更 |
1.0.0-SNAPSHOT1.0.0在父POM中统一管理版本:
|
1 2 3 4 5 6 7 8 9 |
<dependencyManagement> <dependencies> <dependency> <groupId>com.tigeriot</groupId> <artifactId>common-utils</artifactId> <version>1.0.0</version> </dependency> </dependencies> </dependencyManagement> |
子模块中无需指定版本:
|
1 2 3 4 5 6 7 |
<dependencies> <dependency> <groupId>com.tigeriot</groupId> <artifactId>common-utils</artifactId> <!-- 版本由父POM管理 --> </dependency> </dependencies> |
|
1 2 3 4 |
# 检查依赖更新 mvn versions:display-dependency-updates # 检查插件更新 mvn versions:display-plugin-updates |
Maven支持密码加密:
|
1 2 3 |
# 生成主密码 mvn --encrypt-master-password your-master-password # 输出类似:{jSMOWnoPFgsHVpMvz5VrIt5kRbzGpI8u+9EF1iFQyJQ=} |
创建 ~/.m2/settings-security.xml:
|
1 2 3 |
<settingsSecurity> <master>{jSMOWnoPFgsHVpMvz5VrIt5kRbzGpI8u+9EF1iFQyJQ=}</master> </settingsSecurity> |
加密服务器密码:
|
1 2 |
mvn --encrypt-password your-server-password # 输出类似:{COQLCE6DU6GtcS5P=} |
在 settings.xml 中使用加密密码:
|
1 2 3 4 5 |
<server> <id>nexus-releases</id> <username>deployment</username> <password>{COQLCE6DU6GtcS5P=}</password> </server> |
生产环境建议使用HTTPS:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# 使用Nginx反向代理,配置SSL证书 server { listen 443 ssl; server_name nexus.yourdomain.com; ssl_certificate /path/to/cert.pem; ssl_certificate_key /path/to/key.pem; location / { proxy_pass http://nexus:8081; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } } |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#!/bin/bash # backup-nexus.sh BACKUP_DIR="/data/nexus-backup" DATE=$(date +%Y%m%d_%H%M%S) NEXUS_DATA="/data/nexus-data" # 创建备份目录 mkdir -p $BACKUP_DIR # 停止Nexus(可选,推荐在线备份) # docker stop nexus3 # 备份数据 tar -czf $BACKUP_DIR/nexus-backup-$DATE.tar.gz $NEXUS_DATA # 启动Nexus # docker start nexus3 # 保留最近7天的备份 find $BACKUP_DIR -name "nexus-backup-*.tar.gz" -mtime +7 -delete echo "Backup completed: nexus-backup-$DATE.tar.gz" |
配置定时任务:
|
1 2 3 4 |
# 编辑crontab crontab -e # 每天凌晨2点执行备份 0 2 * * * /path/to/backup-nexus.sh |
|
1 2 3 |
# docker-compose.yml environment: - INSTALL4J_ADD_VM_PARAMS=-Xms4g -Xmx4g -XX:MaxDirectMemorySize=6g -XX:+UseG1GC |
在Maven settings.xml 中配置合理的更新策略:
|
1 2 3 4 5 6 7 8 |
<repository> <releases> <updatePolicy>daily</updatePolicy> </releases> <snapshots> <updatePolicy>interval:60</updatePolicy> <!-- 每60分钟 --> </snapshots> </repository> |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
pipeline { agent any tools { maven 'Maven-3.8' jdk 'JDK-11' } stages { stage('Checkout') { steps { git branch: 'develop', url: 'https://github.com/yourorg/yourproject.git' } } stage('Build') { steps { sh 'mvn clean package -DskipTests' } } stage('Test') { steps { sh 'mvn test' } } stage('Deploy to Nexus') { when { branch 'develop' } steps { sh 'mvn deploy -DskipTests' } } stage('Release') { when { branch 'master' } steps { script { // 去掉SNAPSHOT后缀 sh 'mvn versions:set -DremoveSnapshot' // 发布 sh 'mvn clean deploy -DskipTests' // 升级版本并添加SNAPSHOT sh 'mvn versions:set -DnextSnapshot' // 提交版本变更 sh 'git add pom.xml' sh 'git commit -m "[CI] Update version"' sh 'git push origin master' } } } } post { success { echo 'Build and deploy successful!' } failure { echo 'Build or deploy failed!' } } } |
创建 .gitlab-ci.yml:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
variables: MAVEN_OPTS: "-Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository" cache: paths: - .m2/repository/ stages: - build - test - deploy build: stage: build image: maven:3.8-openjdk-11 script: - mvn clean compile only: - branches test: stage: test image: maven:3.8-openjdk-11 script: - mvn test only: - branches deploy-snapshot: stage: deploy image: maven:3.8-openjdk-11 script: - mvn deploy -DskipTests only: - develop deploy-release: stage: deploy image: maven:3.8-openjdk-11 script: - mvn versions:set -DremoveSnapshot - mvn clean deploy -DskipTests only: - master when: manual |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# 发布SNAPSHOT mvn clean deploy -DskipTests # 发布RELEASE mvn versions:set -DremoveSnapshot mvn clean deploy -DskipTests # 上传第三方jar mvn deploy:deploy-file \ -DgroupId=com.example \ -DartifactId=example-lib \ -Dversion=1.0.0 \ -Dpackaging=jar \ -Dfile=example-lib.jar \ -DrepositoryId=nexus-releases \ -Durl=http://nexus:8081/repository/maven-releases/ # 更新依赖 mvn clean install -U # 查看依赖树 mvn dependency:tree # 清理本地缓存 mvn dependency:purge-local-repository |
文档版本: v1.0.0
最后更新: 2025-12-09
作者: Tiger IoT团队
适用范围: 企业内部微服务项目
| 仓库类型 | 仓库名称 | URL |
|---|---|---|
| Group | maven-public | http://nexus:8081/repository/maven-public/ |
| Hosted | maven-releases | http://nexus:8081/repository/maven-releases/ |
| Hosted | maven-snapshots | http://nexus:8081/repository/maven-snapshots/ |
| Proxy | maven-central | http://nexus:8081/repository/maven-central/ |
| Proxy | maven-aliyun | http://nexus:8081/repository/maven-aliyun/ |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# 清理构建 mvn clean # 编译 mvn compile # 测试 mvn test # 打包 mvn package # 安装到本地仓库 mvn install # 发布到远程仓库 mvn deploy # 跳过测试 mvn clean package -DskipTests # 更新依赖 mvn clean install -U # 查看有效POM mvn help:effective-pom # 查看有效settings mvn help:effective-settings # 查看依赖树 mvn dependency:tree # 分析依赖 mvn dependency:analyze # 下载源码 mvn dependency:sources # 版本管理 mvn versions:set -DnewVersion=2.0.0 mvn versions:set -DremoveSnapshot mvn versions:set -DnextSnapshot mvn versions:display-dependency-updates |
提供一个完整的生产环境 settings.xml 模板,保存为独立文件方便使用:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
<?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <localRepository>D:/maven-repository</localRepository> <servers> <server> <id>nexus-releases</id> <username>deployment</username> <password>{COQLCE6DU6GtcS5P=}</password> </server> <server> <id>nexus-snapshots</id> <username>deployment</username> <password>{COQLCE6DU6GtcS5P=}</password> </server> <server> <id>nexus-public</id> <username>developer</username> <password>{DFxCGMN8LQ8=}</password> </server> </servers> <mirrors> <mirror> <id>nexus-public</id> <mirrorOf>*</mirrorOf> <name>Nexus Public Repository</name> <url>http://your-nexus-server:8081/repository/maven-public/</url> </mirror> </mirrors> <profiles> <profile> <id>nexus</id> <repositories> <repository> <id>nexus-public</id> <name>Nexus Public Repository</name> <url>http://your-nexus-server:8081/repository/maven-public/</url> <releases> <enabled>true</enabled> <updatePolicy>daily</updatePolicy> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>nexus-public</id> <name>Nexus Public Repository</name> <url>http://your-nexus-server:8081/repository/maven-public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository> </pluginRepositories> </profile> </profiles> <activeProfiles> <activeProfile>nexus</activeProfile> </activeProfiles> </settings> |
祝您使用愉快!
from:https://www.cnblogs.com/clnchanpin/p/19468854