从Maven 3部署到Nexus OSS 2.12服务器

时间:2016-03-25 17:05:13

标签: java maven curl nexus

每当我尝试使用nexus-staging-maven-plugin将工件从maven 3.05部署到我的Nexus OSS 2.12服务器时,我收到一条错误消息:

[ERROR] Failed to execute goal org.sonatype.plugins:nexus-staging-maven-plugin:1.6.7:deploy (default-deploy) on project myproject
Failed to deploy artifacts: Could not transfer artifact com myproject:myproject:jar:0.0.1-20160325.164052-1 from/to snapshots (http://nexus.myproject.com/content/repositories/snapshots)
Failed to transfer file: http://nexus.myproject.com/content repositories/snapshots/com/myproject/myproject/0.0.1-SNAPSHOT/myproject-0.0.1-20160325.164052-1.jar. 
Return code is: 401, ReasonPhrase: Unauthorized.

现在奇怪的是,我可以使用手动指定凭据的cURL部署pom:

curl -u deployment:deployment123 http://nexus.myproject.com/content/repositories/snapshots/com/myproject/myproject/0.0.1-SNAPSHOT/myproject-0.0.1.pom --request PUT --data @pom.xml

我知道我的~/.m2/settings.xml文件中配置了服务器凭据。这是我的:

<?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">
    <servers>
        <server>
            <id>nexus</id>
            <username>deployment</username>
            <password>deployment123</password>
        </server>
    </servers>
    <mirrors>
        <mirror>
            <id>central</id>
            <url>http://nexus.myproject.com/content/repositories/central/</url>
            <mirrorOf>*</mirrorOf>
        </mirror>
    </mirrors>
</settings>

请注意,该文件中的mirrors部分工作正常 - 当我构建时,maven使用匿名用户从nexus服务器抓取我的依赖项。

我在~/.m2/settings.xml文件中配置的凭据与ID为nexus的服务器相关联。这与我在pom.xml文件的plugins部分中引用的服务器相同:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-deploy-plugin</artifactId>
    <version>2.8.2</version>
    <configuration>
        <skip>true</skip>
    </configuration>
</plugin>
<plugin>
    <groupId>org.sonatype.plugins</groupId>
    <artifactId>nexus-staging-maven-plugin</artifactId>
    <version>1.6.7</version>
    <extensions>true</extensions>
    <executions>
        <execution>
            <id>default-deploy</id>
            <phase>deploy</phase>
            <goals>
                <goal>deploy</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <serverId>nexus</serverId>
        <nexusUrl>http://nexus.myproject.com/</nexusUrl>
    </configuration>
</plugin>

要上传到的存储库是在我的pom文件的repositories部分配置的:

<repositories>
    <repository>
        <id>snapshots</id>
        <url>http://nexus.myproject.com/content/repositories/snapshots</url>
    </repository>
    <repository>
        <id>releases</id>
        <url>http://nexus.myproject.com/content/repositories/releases</url>
    </repository>
</repositories>

最后,我可以确定通过运行~/.m2/settings.xml来读取mvn help:effective-settings文件。

当我尝试从命令行运行部署时,我使用命令mvn clean deploy -DskipTests=true

回顾一下,我有一台Nexus服务器,它启用了库存部署帐户。我可以使用该帐户将工件从cURL部署到存储库,但是当我尝试使用相同的凭据从Maven部署时,我得到了一个HTTP 401。

有人知道为什么会出现这种差异吗?

2 个答案:

答案 0 :(得分:2)

我已经能够使用mvn clean deploy成功部署工件,只有以下内容:

的pom.xml

...

<properties>
  <nexus.url>http://mynexus.com:8081/nexus/content</nexus.url>
</properties>

<repositories>
  <repository>
    <id>nexus</id>
    <url>${nexus.url}/groups/public</url>
    <snapshots>
      <enabled>true</enabled>
      <updatePolicy>always</updatePolicy>
    </snapshots>
  </repository>
</repositories>

<pluginRepositories>
  <pluginRepository>
    <id>nexus</id>
    <url>${nexus.url}/groups/public</url>
  </pluginRepository>
</pluginRepositories>

<distributionManagement>
  <repository>
    <uniqueVersion>false</uniqueVersion>
    <id>nexus</id>
    <name>Releases</name>
    <url>${nexus.url}/repositories/releases</url>
  </repository>
  <snapshotRepository>
    <uniqueVersion>false</uniqueVersion>
    <id>nexus</id>
    <name>Snapshots</name>
    <url>${nexus.url}/repositories/snapshots</url>
  </snapshotRepository>
</distributionManagement>

...

的settings.xml

...

<servers>
    <server>
        <id>nexus</id>
        <username>deployment</username>
        <password>deployment123</password>
    </server>
</servers>

...

我认为部署到nexus的重要部分是<distributionManagement>部分。我从未需要明确声明maven-deploy-pluginnexus-staging-maven-plugin

希望有所帮助!

答案 1 :(得分:0)

如果要使用Nexus Staging Maven插件,则需要声明存储库使用登台端点,或者需要显式配置登台配置文件ID。在您的情况下,您已配置实际的版本存储库而不是登台端点。

一般查看Maven pluginstaging suite的文档。我还记录了free video seriessuite of example projects