Java 如何配置 settings.xml 以从 Artifactory 私有虚拟存储库中提取?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35326481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:33:24  来源:igfitidea点击:

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

javamavenmaven-2maven-3artifactory

提问by Chris Harris

I'm an Apache Archiva veteran who has recently switched over to Artifactory Pro. I've been attempting to replicate, in Artifactory, a setup where I implemented virtual, private repositories within Archiva. The end result should be that different teams have access to their own team's artifacts, remote repos, the default local repos, and a company-wide repository.

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

However, I'm having trouble downloading only somedependencies, that are available on and pulled from Maven Central, whenever I configure my Maven settings.xml to use my private virtual repository. If I use Artifactory's default settings.xml (libs-release, libs-snapshot, plugins-release, plugins-snapshot and no virtual repository), then I can download all dependencies that are available on Maven Central and that my projects require.

但是,每当我将 Maven settings.xml 配置为使用我的私有虚拟存储库时,我都无法仅下载一些依赖项,这些依赖项在 Maven Central 上可用并从 Maven Central 中提取。如果我使用 Artifactory 的默认 settings.xml(libs-release、libs-snapshot、plugins-release、plugins-snapshot 并且没有虚拟存储库),那么我可以下载 Maven Central 上可用的所有依赖项,并且我的项目需要这些依赖项。

I'm running Artifactory v4.4.2 on their provided Tomcat within a Windows server 2012 virtual server. I am not running behind a proxy. I have no such issues with Archiva.

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

In regard to my private virtual repository setup, I have a virtual repository named "test" that is a "generic" Package Type. That repository includes the following repos:

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

  • remote-repos
  • libs-release
  • libs-snapshot
  • plugins-release
  • plugins-snapshot
  • test-release-local
  • test-snapshot-local
  • company-release-local
  • company-snapshot-local
  • 远程仓库
  • 库释放
  • 库快照
  • 插件发布
  • 插件快照
  • 测试发布本地
  • 测试快照本地
  • 公司发布本地
  • 公司快照本地

The first 5 repos listed are the default virtual repository repos provided Artifactory. The last 4 repos listed are the local repos, and they are of a Generic Package Type.

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

I think I should use a mirror.

我想我应该用一面镜子。

Here is an example settings.xml that I am using (my username and password are getting populated within the generated settings.xml that I save to my Maven conf folder):

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

<?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>

An example of an artifact that I cannot pull from Maven Central through my virtual repository is poi-ooxml-schemas-3.10-FINAL.jar or many Apache Maven plugins. As soon as I remove the mirror section, I can download all dependencies from Maven Central. This makes no sense to me. My virtual repository is configured to include the remote-repos virtual repository and therefore should be able to pull everything from Maven Central.

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

What am I doing wrong?

我究竟做错了什么?

采纳答案by eranb

Well, it seems that the actual thing here is that this is a bugwhich is related to the predefined layoutof the generic virtual repository. The suggested workaround, until this will be fixed, is to create a new repository layout based on the simple-default layout. So, instead of the simple-default layout: [orgPath]/[module]/[module]-[baseRev](-[fileItegRev]).[ext]

好吧,这里的实际情况似乎是这是一个与通用虚拟存储库的预定义布局相关的错误。在解决此问题之前,建议的解决方法是基于简单默认布局创建新的存储库布局。所以,而不是简单的默认布局:[orgPath]/[module]/[module]-[baseRev](-[fileItegRev]).[ext]

One should create a new layout which looks like this: [orgPath]/[module]/[module]-[baseRev].[ext]

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

Once you create this new repository layout you can change the defined layout of the repository to the newly created one.

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

This will fix the behaviour you mentioned.

这将修复您提到的行为。

回答by Chris Harris

O.K. I figured out part of the answer myself and another part of the answer via JFrog support's help.

好的,我自己找出了部分答案,并通过 JFrog 支持人员的帮助找出了另一部分答案。

Here was the first set of issues that I discovered:

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

  1. I noticed that if I use a virtual repository that's of a Generic type with a local repository of a Generic type, then artifact retrieval would download most of my pom's dependencies but not all of the dependencies.
  2. I noticed that if I use a virtual repository that's of a Generic type with a local repository of a Maven type, then artifact retrieval would download most of my pom's dependencies but not all of the dependencies.
  1. 我注意到,如果我使用一个 Generic 类型的虚拟存储库和一个 Generic 类型的本地存储库,那么工件检索将下载我的 pom 的大部分依赖项,但不是所有的依赖项。
  2. 我注意到,如果我使用 Generic 类型的虚拟存储库和 Maven 类型的本地存储库,那么工件检索将下载我的大部分 pom 依赖项,但不是所有依赖项。

Those issues seem like bugs to me.

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

So, I ended up defining a virtual repository (named test-maven in the config below) and two local repositories, all of which were a Maven type. The two local repositories are included within the virtual repository.

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

I came up with a settings.xml that worked, but it was using a Mirror, which JFrog support advised me that is not a best practice. Bearing that advice in mind, here is the final settings.xml that works for me:

我想出了一个有效的 settings.xml,但它使用的是镜像,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>

Using this config, I verified that I can download all artifacts published to Maven Central, further remote repositories, snapshots from the local snapshot Maven repository, and releases from the local snapshot Maven repository. This setup allows me to utilize private repositories per development team.

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

回答by jreedick

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

删除镜像部分后,我就可以从 Maven Central 下载所有依赖项。

You cannot access the mirror because you have not specified the credentials to use. (Also you don't have anonymous access enabled)

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

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

来自: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.

文件中指定的每个 <repository> 和 <mirror> 元素都必须有一个对应的 <server> 元素,其中包含一个指定用户名和密码的匹配 <id>。

Your mirror's id is test. So you need to add another server entry for it:

您的镜像 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>

It's a tad annoying that Artifactory's "Set me up" feature for creating settings.xml files doesn't add a <server> entry for mirrors automatically.

Artifactory 用于创建 settings.xml 文件的“设置我”功能不会自动为镜像添加 <server> 条目,这有点烦人。