「Maven」- 功能 | 特性

Repository Cache

https://stackoverflow.com/questions/4856307/when-maven-says-resolution-will-not-be-reattempted-until-the-update-interval-of

-U,–update-snapshots | Forces a check for missing releases and updated snapshots on remote repositories

mvn -U clean install

<profiles>
    <profile>
      ...
      <repositories>
        <repository>
          <id>myRepo</id>
          <name>My Repository</name>
          <releases>
            <enabled>false</enabled>
            <updatePolicy>always</updatePolicy>
            <checksumPolicy>warn</checksumPolicy>
          </releases>
         </repository>
      </repositories>
      ...
    </profile>
  </profiles>

推送构建制品

DeepSeek / maven 推送构建结果

# 使用 Maven 推送构建结果到仓库

Maven 提供了将构建结果(如 JAR、WAR、POM 等)推送到远程仓库的功能。以下是完整的推送构建结果的指南:

## 1. 基本推送配置

### 1.1 在 pom.xml 中配置分发仓库

“`xml
<project>



<distributionManagement>
<!– 发布版本仓库 –>

<repository>
<id>company-releases</id>

<name>Company Release Repository</name>

<url>
https://nexus.example.com/repository/maven-releases/</url>
</repository>

<!– 快照版本仓库 –>

<snapshotRepository>
<id>company-snapshots</id>

<name>Company Snapshot Repository</name>

<url>
https://nexus.example.com/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>

</project>

“`

### 1.2 在 settings.xml 中配置认证信息

“`xml
<settings>

<servers>
<server>
<id>company-releases</id> <!– 必须与 pom 中的 id 匹配 –>

<username>deployment</username>

<password>{加密的密码}</password>
</server>

<server>
<id>company-snapshots</id>

<username>deployment</username>

<password>{加密的密码}</password>
</server>
</servers>

</settings>

“`

## 2. 执行推送命令

### 2.1 推送快照版本

“`bash
mvn clean deploy
“`

Maven 会根据项目版本自动判断:
– 版本号以 `-SNAPSHOT` 结尾 → 推送到 `<snapshotRepository>`
– 正式版本 → 推送到 `<repository>`

### 2.2 仅部署不重新构建

“`bash
mvn deploy:deploy-file
“`

## 3. 手动部署单个文件

当需要部署非标准构建产生的文件时:

“`bash
mvn deploy:deploy-file \
-DgroupId=com.example \
-DartifactId=my-library \
-Dversion=1.0.0 \
-Dpackaging=jar \
-Dfile=target/my-library-1.0.0.jar \
-DpomFile=pom.xml \
-DrepositoryId=company-releases \
-Durl=https://nexus.example.com/repository/maven-releases/
“`

## 4. 高级配置

### 4.1 多模块项目部署

父 pom 中配置:

“`xml
<distributionManagement>

<repository>
<id>company-releases</id>

<url>
https://nexus.example.com/repository/maven-releases/</url>
</repository>

<snapshotRepository>
<id>company-snapshots</id>

<url>
https://nexus.example.com/repository/maven-snapshots/</url>
</snapshotRepository>

</distributionManagement>

“`

子模块会自动继承这些配置。

### 4.2 部署源码和 Javadoc

mvn clean deploy -DperformRelease=true

或在 pom 中配置:

“`xml
<build>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-source-plugin</artifactId>

<version>3.2.1</version>

<executions>
<execution>
<id>attach-sources</id>

<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-javadoc-plugin</artifactId>

<version>3.3.2</version>

<executions>
<execution>
<id>attach-javadocs</id>

<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>

</build>

“`

## 5. 常见问题解决

### 5.1 认证失败

确保:

  1. settings.xml 中的 `<server>` id 与 pom.xml 中的仓库 id 匹配
  2. 用户名密码正确
  3. 有写入仓库的权限

### 5.2 部署冲突

错误:`409 Conflict`

解决方案:

  1. 对于发布版本(非 SNAPSHOT),版本必须唯一,不能覆盖
  2. 对于快照版本,确保有删除旧版本的权限

### 5.3 HTTPS 证书问题

如果使用自签名证书,可以:

  1. 导入证书到 JDK 的 cacerts
  2. 或临时跳过验证:

“`bash
mvn deploy -Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true
“`

## 6. 最佳实践

  1. 版本管理

– 快照版本用于开发 (`1.0.0-SNAPSHOT`)

– 发布版本用于生产 (`1.0.0`)

  1. 自动化部署

– CI/CD 流水线中自动执行 `mvn deploy`

  1. 安全建议

– 使用角色账户而非个人账户

– 加密 settings.xml 中的密码

– 最小权限原则

  1. 仓库策略

– 发布版本不可修改

– 定期清理旧快照

通过以上配置和命令,您可以有效地将 Maven 构建结果推送到远程仓库,实现构件的集中管理和分发。