如何获取maven项目层次结构的所有远程存储库?

Kar*_*ter 5 maven

我正在使用以下内容将所有maven存储库访问重定向到Artifactory ~/.m2/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">
<!-- unclear what version changes -> use 1.1.0 because it's higher -->
  <servers>
    <server>
      <id>central</id>
      <username>admin</username>
    </server>
    <server>
      <id>snapshots</id>
      <username>admin</username>
    </server>
  </servers>
  <mirrors>
    <mirror>
      <id>artifactory</id>
      <name>artifactory</name>
      <mirrorOf>*</mirrorOf>
      <url>https://[hostname]:[port]/artifactory/remote-repos/</url>
    </mirror>
  </mirrors>
  <profiles>
    <profile>
      <id>artifactory</id>
      <repositories>
        <repository>
          <id>central</id>
          <name>remote-repos</name>
          <url>https://[hostname]:[port]/artifactory/remote-repos</url>
          <snapshots>
            <enabled>false</enabled>
            <updatePolicy>interval:25200</updatePolicy>
          </snapshots>
        </repository>
        <repository>
          <id>snapshots</id>
          <name>remote-repos</name>
          <url>https://[hostname]:[port]/artifactory/remote-repos</url>
          <snapshots />
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
          <id>central</id>
          <name>remote-repos</name>
          <url>https://[hostname]:[port]/artifactory/remote-repos</url>
          <snapshots>
            <enabled>false</enabled>
            <updatePolicy>interval:25200</updatePolicy>
          </snapshots>
        </pluginRepository>
        <pluginRepository>
          <id>snapshots</id>
          <name>remote-repos</name>
          <url>https://[hostname]:[port]/artifactory/remote-repos</url>
          <snapshots />
        </pluginRepository>
      </pluginRepositories>
    </profile>
  </profiles>
  <activeProfiles>
    <activeProfile>artifactory</activeProfile>
  </activeProfiles>
</settings>
Run Code Online (Sandbox Code Playgroud)

因此必须将项目(及其子项目)指定的额外远程存储库添加到Artifactory实例.我目前正在使用

find . -name pom.xml -exec grep -B 5 -C 5 '<repository>' {} +
Run Code Online (Sandbox Code Playgroud)

如果URL是一个变量并在其他地方声明并且它不跳过重复,这不是很方便.这不是世界上最糟糕的事情,但也许有一个改进.

以下不起作用:

  • mvn versions:display-dependency-updates 不显示远程存储库
  • mvn dependency:list-repositories只有在启用了代理才能获取第一个依赖项时才会起作用,这样我就必须弄清楚从哪里获取它,将研究过的远程存储库添加到Artifactory或移到~/.m2/settings.xml一边 - 比find上面的命令更不方便

解决方案应该递归地工作,即包括所有子项目和子项目项目中的所有存储库等.

很有意义的是,解决方案不需要在没有代理的情况下直接从远程存储库下载依赖项,因为我希望在可能的情况下立即通过Maven代理传输它们 - 但这不是必需的.

Seb*_*use 3

一个有点hacky的方法可能是这两个步骤:

  1. 获取有效的 POM。请注意,以下目标会立即生成包含所有 POM 的 XML 文件。但是,变量名称已经被解析。

    mvn help:effective-pom -Doutput="effective-pom.xml"
    
    Run Code Online (Sandbox Code Playgroud)
  2. 解析生成的 XML 文件并收集存储库,例如使用 Python 脚本gather-repos.py

    #!/usr/bin/python
    
    import sys, xml.etree.ElementTree as ET
    root = ET.parse('effective-pom.xml').getroot()
    repositories = dict()
    for node in root.iter('{http://maven.apache.org/POM/4.0.0}repository'):
        repo_id = node.findtext('{http://maven.apache.org/POM/4.0.0}id')
        repositories[repo_id] = node
    for node in repositories.itervalues():
        ET.ElementTree(node).write(sys.stdout, default_namespace='http://maven.apache.org/POM/4.0.0')
    
    Run Code Online (Sandbox Code Playgroud)

当然,该脚本可以通过以下方式运行

chmod +x gather-repos.py
./gather-repos.py
Run Code Online (Sandbox Code Playgroud)