Maven依赖:从settings.xml获取LATEST忽略存储库

时间:2016-02-23 15:14:11

标签: java maven artifactory maven-dependency-plugin

我正在尝试从我的远程存储库下载最后一个快照版本(Artifactory。为此,我使用以下命令:

mvn dependency:get \
  -Dartifact=com.acme:my-application:LATEST:war \
  -DremoteRepositories=http://localhost:8085/artifactory/libs-snapshots-local/

libs-snapshot-local存储库是Artifactory的一部分,其中存储了所有快照。如果我在以下路径中打开maven-metadata.xml文件:

http://localhost:8085/artifactory/libs-snapshots-local/com/acme/my-application/maven-metadata.xml

然后我得到以下XML响应:

<?xml version="1.0" encoding="UTF-8"?>
<metadata>
  <groupId>com.acme</groupId>
  <artifactId>my-application</artifactId>
  <version>2.0.0-20160222.105839-1</version>
  <versioning>
    <latest>2.0.0-SNAPSHOT</latest>
    <versions>
      <version>2.0.0-SNAPSHOT</version>
    </versions>
    <lastUpdated>20160223132257</lastUpdated>
  </versioning>
</metadata>

因此,根据此文件,版本2.0.0-SNAPSHOT的WAR应该是检索到的。

然而,当我执行命令时,我收到以下响应:

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:2.8:get (default-cli) @ standalone-pom ---
[INFO] Resolving com.acme:my-application:war:LATEST with transitive dependencies
Downloading: http://download.java.net/maven/2/com/acme/my-application/maven-metadata.xml
Downloading: http://localhost:8085/artifactory/libs-releases/com/acme/my-application/maven-metadata.xml
Downloading: http://repo1.maven.org/maven2/com/acme/my-application/maven-metadata.xml
Downloading: http://localhost:8085/artifactory/libs-snapshots/com/acme/my-application/maven-metadata.xml
Downloading: http://localhost:8085/artifactory/libs-snapshots-local/com/acme/my-application/maven-metadata.xml
Downloaded: http://localhost:8085/artifactory/libs-snapshots-local/com/acme/my-application/maven-metadata.xml (378 B at 23.1 KB/sec)
Downloaded: http://localhost:8085/artifactory/libs-snapshots/com/acme/my-application/maven-metadata.xml (378 B at 0.5 KB/sec)
Downloaded: http://localhost:8085/artifactory/libs-releases/com/acme/my-application/maven-metadata.xml (898 B at 0.6 KB/sec)
Downloading: http://localhost:8085/artifactory/libs-releases/com/acme/my-application/1.0.0/my-application-1.0.0.pom
Downloaded: http://localhost:8085/artifactory/libs-releases/com/acme/my-application/1.0.0/my-application-1.0.0.pom (26 KB at 1475.0 KB/sec)
Downloading: http://localhost:8085/artifactory/libs-releases/com/acme/my-application/1.0.0/my-application-1.0.0.war
Downloaded: http://localhost:8085/artifactory/libs-releases/com/acme/my-application/1.0.0/my-application-1.0.0.war (77415 KB at 9657.4 KB/sec)

看起来dependency:get目标是通过查看settings.xml文件中的所有存储库和-DremoteRepositories参数来尝试下载工件。

由于这个原因,它也在查看我的发行版存储库,其中包含一个1.0.0版本,该版本的发布时间晚于2.2.0-SNAPSHOT。

我的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">

    <pluginGroups>
        <pluginGroup>org.mortbay.jetty</pluginGroup>
    </pluginGroups>
    <profiles>
        <profile>
            <repositories>
                <repository>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
                    <id>central</id>
                    <name>libs-releases</name>
                    <url>http://localhost:8085/artifactory/libs-releases</url>
                </repository>
                <repository>
                    <snapshots />
                    <id>snapshots</id>
                    <name>libs-snapshots</name>
                    <url>http://localhost:8085/artifactory/libs-snapshots</url>
                </repository>
                <repository>
                    <id>Maven</id>
                    <name>Maven Repository</name>
                    <url>http://repo1.maven.org/maven2/</url>
                </repository>
                <repository>
                    <id>Java</id>
                    <name>Java Repository</name>
                    <url>http://download.java.net/maven/2/</url>
                </repository>
            </repositories>
            <id>artifactory</id>
        </profile>
    </profiles>
    <activeProfiles>
        <activeProfile>artifactory</activeProfile>
    </activeProfiles>
</settings>

有没有办法告诉maven-dependency-plugin不要使用settings.xml中定义的存储库,只使用命令中给出的依赖关系?

1 个答案:

答案 0 :(得分:2)

Reading the source code,您所描述的正是dependency:get目标的行为。它将添加到命令行中指定的remoteRepositories,即当前活动存储库的列表。

复制此处的代码以供参考:

if ( pomRemoteRepositories != null )
{
    repoList.addAll( pomRemoteRepositories );
}

可以看到repoList变量(稍后用于要检查的存储库列表)将始终包含活动存储库,并且目前无法解决此问题。

活动存储库列表is retrieved

@Parameter( defaultValue = "${project.remoteArtifactRepositories}", readonly = true, required = true )
private List<ArtifactRepository> pomRemoteRepositories;

也意味着它不能在命令行覆盖(它只读取且没有用户属性)。

因此,在您的情况下,它会导致在artifactory配置文件下定义的所有存储库也被使用,因为它被声明为活动状态。

我能看到的唯一可能的解决方法是invoke Maven with a default settings.xml file(例如Maven安装中的那个)。或create a feature request at their Jira要求提供此用例(我无法找到与此相关的内容)。