如何配置 settings.xml 从 Artifactory 私有虚拟仓库中拉取?

How to configure settings.xml to pull from Artifactory private virtual repository?

我是 Apache Archiva 老手,最近转向了 Artifactory Pro。我一直在尝试在 Artifactory 中复制一个我在 Archiva 中实现虚拟私有存储库的设置。最终结果应该是不同的团队可以访问他们自己团队的工件、远程存储库、默认本地存储库和公司范围的存储库。

但是,每当我配置我的 Maven settings.xml 以使用我的私有虚拟存储库。如果我使用 Artifactory 的默认 settings.xml(libs-release、libs-snapshot、plugins-release、plugins-snapshot 并且没有虚拟存储库),那么我可以下载 Maven Central 上可用的所有依赖项以及我的项目所需的依赖项。

我 运行 Artifactory v4.4.2 在他们提供的 Tomcat Windows 服务器 2012 虚拟服务器中。我没有 运行 代理。我对 Archiva 没有这样的问题。

关于我的私有虚拟存储库设置,我有一个名为“test”的虚拟存储库,它是一个“通用”包类型。该存储库包括以下存储库:

列出的前 5 个存储库是 Artifactory 提供的默认虚拟存储库存储库。列出的最后 4 个存储库是本地存储库,它们属于通用包类型。

我想我应该用一面镜子。

这是我正在使用的示例 settings.xml(我的用户名和密码在我保存到我的 Maven conf 文件夹的生成的 settings.xml 中填充):

<?xml version="1.0" encoding="UTF-8"?>
<settings xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd" xmlns="http://maven.apache.org/SETTINGS/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <servers>
    <server>
      <username>${security.getCurrentUsername()}</username>
      <password>${security.getEscapedEncryptedPassword()!"*** Insert encrypted password here ***"}</password>
      <id>central</id>
    </server>
    <server>
      <username>${security.getCurrentUsername()}</username>
      <password>${security.getEscapedEncryptedPassword()!"*** Insert encrypted password here ***"}</password>
      <id>snapshots</id>
    </server>
  </servers>
  <mirrors>
    <mirror>
      <mirrorOf>*</mirrorOf>
      <name>test</name>
      <url>http://localhost:8082/artifactory/test</url>
      <id>test</id>
    </mirror>
  </mirrors>
  <profiles>
    <profile>
      <repositories>
        <repository>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
          <id>central</id>
          <name>libs-release</name>
          <url>http://localhost:8082/artifactory/libs-release</url>
        </repository>
        <repository>
          <snapshots />
          <id>snapshots</id>
          <name>libs-snapshot</name>
          <url>http://localhost:8082/artifactory/libs-snapshot</url>
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
          <id>central</id>
          <name>plugins-release</name>
          <url>http://localhost:8082/artifactory/plugins-release</url>
        </pluginRepository>
        <pluginRepository>
          <snapshots />
          <id>snapshots</id>
          <name>plugins-snapshot</name>
          <url>http://localhost:8082/artifactory/plugins-snapshot</url>
        </pluginRepository>
      </pluginRepositories>
      <id>artifactory</id>
    </profile>
  </profiles>
  <activeProfiles>
    <activeProfile>artifactory</activeProfile>
  </activeProfiles>
</settings>

我无法通过我的虚拟存储库从 Maven Central 提取的工件示例是 poi-ooxml-schemas-3.10-FINAL.jar 或许多 Apache Maven 插件。只要删除镜像部分,我就可以从 Maven Central 下载所有依赖项。这对我来说毫无意义。我的虚拟存储库配置为包含 remote-repos 虚拟存储库,因此应该能够从 Maven Central 提取所有内容。

我做错了什么?

O.K。我自己找到了一部分答案,另一部分是在 JFrog 支持的帮助下找到的。

这是我发现的第一组问题:

  1. 我注意到,如果我使用通用类型的虚拟存储库和通用类型的本地存储库,那么工件检索将下载我的 pom 的大部分依赖项,但不是所有依赖项。
  2. 我注意到,如果我使用一个通用类型的虚拟存储库和一个 Maven 类型的本地存储库,那么工件检索将下载我的 pom 的大部分依赖项,但不是所有依赖项。

这些问题对我来说像是错误。

因此,我最终定义了一个虚拟存储库(在下面的配置中名为 test-maven)和两个本地存储库,它们都是 Maven 类型。这两个本地存储库包含在虚拟存储库中。

我想出了一个可行的 settings.xml,但它使用的是 Mirror,JFrog 支持人员建议我这不是最佳实践。牢记该建议,这是对我有用的最终 settings.xml:

<?xml version="1.0" encoding="UTF-8"?>
<settings xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd" xmlns="http://maven.apache.org/SETTINGS/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <servers>
    <server>
      <username>USERNAME</username>
      <password>PASSWORD</password>
      <id>test-maven</id>
    </server>
  </servers>
  <profiles>
    <profile>
      <repositories>
        <repository>
          <snapshots />
          <id>test-maven</id>
          <name>test-maven</name>
          <url>http://localhost:8082/artifactory/test-maven</url>
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
          <snapshots />
          <id>test-maven</id>
          <name>test-maven</name>
          <url>http://localhost:8082/artifactory/test-maven</url>
        </pluginRepository>
      </pluginRepositories>
      <id>artifactory</id>
    </profile>
  </profiles>
  <activeProfiles>
    <activeProfile>artifactory</activeProfile>
  </activeProfiles>
</settings>

使用此配置,我验证了我可以下载发布到 Maven Central 的所有工件、进一步的远程存储库、来自本地快照 Maven 存储库的快照,以及来自本地快照 Maven 存储库的发布。此设置允许我使用每个开发团队的私有存储库。

好吧,这里的实际内容似乎是通用虚拟存储库的 this is a bug which is related to the predefined layout。 建议的解决方法(在修复此问题之前)是基于简单默认布局创建一个新的存储库布局。 所以,而不是简单的默认布局: [orgPath]/[module]/[module]-[baseRev](-[fileItegRev]).[ext]

应该创建一个如下所示的新布局: [orgPath]/[module]/[module]-[baseRev].[ext]

创建这个新的存储库布局后,您可以将定义的存储库布局更改为新创建的布局。

这将解决您提到的行为。

As soon as I remove the mirror section, I can download all dependencies from Maven Central.

您无法访问镜像,因为您没有指定要使用的凭据。 (而且你没有启用匿名访问)

发件人:https://www.jfrog.com/confluence/display/RTF/Maven+Repository#MavenRepository-ConfiguringAuthentication

Each <repository> and <mirror> element specified in the file must have a corresponding <server> element with a matching <id> that specifies the username and password.

你的镜像id是test。所以你需要为它添加另一个服务器条目:

<servers>
    <server>
        <username>USERNAME</username>
        <password>PASSWORD</password>
        <id>test-maven</id>
    </server>
    <server>
        <!-- credentials for mirror -->
        <username>USERNAME</username>
        <password>PASSWORD</password>
        <id>test</id>
    </server>
</servers>

Artifactory 用于创建 settings.xml 文件的 "Set me up" 功能不会自动为镜像添加 条目,这有点烦人。