maven - 为什么 Maven(错误地?)将我的 SNAPSHOT 部署到发布存储库和快照存储库?

标签 maven maven-deploy-plugin

我当前在尝试设置项目以部署到内部 Nexus 存储库时遇到问题。由于我对 Maven 总体来说还很陌生,所以我希望在如何设置分发管理方面有一些我不太理解的地方。

基本问题是,当我执行“mvn deploy”时, Artifact 已成功部署到快照存储库,但 Maven 也尝试将其部署到发布存储库,但失败了……正如它应该的那样。我对当前配置的理解是,它也不应该将其部署到发布存储库。

我已经在下面包含了各种配置元素,但我想知道我是否真的应该使用配置文件来管理该部分,以便仅定义快照构建,并且仅定义发布构建。

对此的任何帮助/澄清将非常感激。

我的 POM 中有以下内容用于分发管理:

<distributionManagement>
<repository>
  <id>internal-releases</id>
  <name>Internal Releases</name>
  <url>http://localhost:8081/nexus/content/repositories/releases</url>
</repository>
<snapshotRepository>
  <id>internal-snapshots</id>
  <name>Internal Snapshots</name>
  <url>http://localhost:8081/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>

在 POM 的其他位置,我进行了以下设置以允许使用这些存储库来获取 Artifact :

<repositories>
<repository>
  <id>internal-releases</id>
  <url>http://localhost:8081/nexus/content/repositories/releases</url>
  <snapshots><enabled>false</enabled></snapshots>
</repository>
<repository>
  <id>internal-snapshots</id>
  <url>http://localhost:8081/nexus/content/repositories/snapshots</url>
  <snapshots><enabled>true</enabled></snapshots>
</repository>
<!-- other repos, etc, etc -->
</repositories>

我的 settings.xml 中有正确的设置,以提供能够发布到我的计算机上运行的测试 Nexus 实例的凭据,并且它实际上正在成功部署快照。

问题在于它还尝试将快照部署到发布存储库,该存储库配置为禁止快照。

“mvn deploy”的输出包括以下内容:

[INFO] [deploy:deploy {execution: default-deploy}]
[INFO] Retrieving previous build number from internal-snapshots
Uploading: http://localhost:8081/nexus/content/repositories/snapshots/com/internal/service/1.0.0-SNAPSHOT/service-1.0.0-20101104.170338-8.war
405K uploaded  (service-1.0.0-20101104.170338-8.war)
[INFO] Retrieving previous metadata from internal-snapshots
[INFO] Uploading repository metadata for: 'snapshot com.internal:service:1.0.0-SNAPSHOT'
[INFO] Retrieving previous metadata from internal-snapshots
[INFO] Uploading repository metadata for: 'artifact com.internal:service'
[INFO] Uploading project information for service 1.0.0-20101104.170338-8
[INFO] [deploy:deploy-file {execution: default}]
[INFO] Retrieving previous build number from remote-repository
[INFO] repository metadata for: 'snapshot com.internal:service:1.0.0-SNAPSHOT' could not be found on repository: remote-repository, so will be created
Uploading: http://localhost:8081/nexus/content/repositories/releases/com/internal/service/1.0.0-SNAPSHOT/service-1.0.0-20101104.170338-1.jar
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Error deploying artifact: Failed to transfer file: http://localhost:8081/nexus/content/repositories/releases/com/internal/service/1.0.0-SNAPSHOT/service-1.0.0-20101104.170338-1.jar. Return code is: 400

来自 Nexus 的日志包含以下内容(正如我所期望的那样):

jvm 1    | 2010-11-04 13:03:39 INFO  [p-759477796-118] - o.s.n.p.m.m.M2Repos~          - Storing of item releases:/com/internal/service/1.0.0-SNAPSHOT/service-1.0.0-20101104.170338-1.jar is forbidden by Maven Repository policy. Because releases is a RELEASE repository
jvm 1    | 2010-11-04 13:03:39 ERROR [p-759477796-118] - o.s.n.r.ContentPlex~          - Got exception during processing request "PUT http://localhost:8081/nexus/content/repositories/releases/com/internal/service/1.0.0-SNAPSHOT/service-1.0.0-20101104.170338-1.jar": Storing of item releases:/com/internal/service/1.0.0-SNAPSHOT/service-1.0.0-20101104.170338-1.jar is forbidden by Maven Repository policy. Because releases is a RELEASE repository

最佳答案

  1. 在 pom 中定义以下属性

    <deployFileUrl>${project.distributionManagement.snapshotRepository.url}</deployFileUrl>
    
  2. 按如下方式更改 maven-deploy-plugin 的配置:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-deploy-plugin</artifactId>
        <version>2.5</version>
        <configuration>
            <skip>true</skip>
        </configuration>
        <executions>
            <execution>
                <phase>deploy</phase>
                <configuration>
                    <packaging>jar</packaging>
                    <generatePom>true</generatePom>
                    <url>${deployFileUrl}</url>
                    <artifactId>${project.artifactId}</artifactId>
                    <groupId>${project.groupId}</groupId>
                    <version>${project.version}</version>
                    <file>${project.build.directory}/${project.build.finalName}.jar</file>
                </configuration>
                <goals>
                    <goal>deploy-file</goal>
                </goals>
            </execution>
         </executions>
     </plugin>
    
  3. 添加以下配置文件以使用存储库 url 设置deployFileUrl 属性

    <profiles>
        <profile>
            <id>release-mode</id>
            <properties>
                <deployFileUrl>${project.distributionManagement.repository.url}</deployFileUrl>
            </properties>
        </profile>
    </profiles>
    
  4. 最后在maven-release-plugin中调用这个配置文件

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-release-plugin</artifactId>
        <version>2.0-beta-9</version>
        <configuration>
            <releaseProfiles>release-mode</releaseProfiles>
        </configuration>
     </plugin>
    

关于maven - 为什么 Maven(错误地?)将我的 SNAPSHOT 部署到发布存储库和快照存储库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4099412/

相关文章:

java - Maven spring-boot-starter-security 将 org.springframework.security 限制为版本 3.2.8 并且不使用 4.0.2

java - Maven测试: Checkout some other repository for some test classes During Jenkin Build

Maven 许可

java - 如何重构Maven多模块项目?

svn - 无法 mvn 发布 :prepare, 服务器证书验证失败

java - Maven 突然抛出错误,类文件被截断

java - 我在 JEE 套接字项目 SLF4J : Failed to load class "org.slf4j.impl.StaticLoggerBinder" 中遇到错误

java - maven 缺少依赖项 jta-1.0.1b

java - 如何防止部署 Maven 创建的 ZIP 文件?

java - 在 Maven POM 中禁用 distributionManagement