Maven在编译阶段会访问https://repo.maven.apache.org/maven2/,即使不同的仓库被定义为“central”。

3

我有一个pom文件,里面我在repositories和pluginRepositories中定义了不同的中央仓库,但是当我运行mvn install时,在日志中看到maven仍然去访问https://repo.maven.apache.org

我的pom文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>Test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <repositories>
        <repository>
            <id>central</id>
            <name>My-repo</name>
            <url>http://My-repo/remote</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>central</id>
            <name>My-repo-release</name>
            <url>http://my-repo/plugins-release</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>

        <pluginRepository>
            <id>release</id>
            <name>My-repo-snapshot</name>
            <url>http://my-repo/plugins-snapshot</url>
            <releases>
                <enabled>false</enabled>
            </releases>
        </pluginRepository>

    </pluginRepositories>

    <dependencies>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>22.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.1.1</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <finalName>${project.artifactId}</finalName>
                    <appendAssemblyId>false</appendAssemblyId>
                    <outputDirectory>${project.parent.basedir}/plugins</outputDirectory>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>


日志记录:
Downloaded from central: http://my-repo/plugins-release/commons-codec/commons-codec/1.6/commons-codec-1.6.jar (233 kB at 226 kB/s)
Downloaded from central: http://my-repo/plugins-release/org/codehaus/plexus/plexus-utils/3.1.0/plexus-utils-3.1.0.jar (262 kB at 253 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/felix/maven-bundle-plugin/2.5.0/maven-bundle-plugin-2.5.0.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/felix/maven-bundle-plugin/2.5.0/maven-bundle-plugin-2.5.0.pom (0 B at 0 B/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/felix/felix-parent/2.1/felix-parent-2.1.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/felix/felix-parent/2.1/felix-parent-2.1.pom (0 B at 0 B/s)
Downloading from central: https://repo.maven.apache.org/maven2/biz/aQute/bnd/bndlib/2.3.0/bndlib-2.3.0.pom
Downloaded from central: https://repo.maven.apache.org/maven2/biz/aQute/bnd/bndlib/2.3.0/bndlib-2.3.0.pom (0 B at 0 B/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1.pom (0 B at 0 B/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/felix/org.apache.felix.bundlerepository/1.6.6/org.apache.felix.bundlerepository-1.6.6.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/felix/org.apache.felix.bundlerepository/1.6.6/org.apache.felix.bundlerepository-1.6.6.pom (0 B at 0 B/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/osgi/org.osgi.core/4.1.0/org.osgi.core-4.1.0.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/osgi/org.osgi.core/4.1.0/org.osgi.core-4.1.0.pom (0 B at 0 B/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/easymock/easymock/2.4/easymock-2.4.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/easymock/easymock/2.4/easymock-2.4.pom (0 B at 0 B/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/felix/org.apache.felix.utils/1.6.0/org.apache.felix.utils-1.6.0.pom
Progress (1): 2.2/3.3 kB
Progress (1): 3.3 kB   


当我查看我的artifactory(jfrog)时,我发现元数据和有时更多的文件(如source.jar)被定义为“未缓存”。也许它与问题有关?
此外,当我使用镜像时,Maven没有去maven-central,但编译成功通过。
我的settings.xml中的镜像:
<mirrors>
    <mirror>
      <id>internal-repository</id>
      <name>Maven Repository Manager running on repo.mycompany.com</name>
      <url>http://My-repo/remote</url>
      <mirrorOf>central</mirrorOf>
    </mirror>
  </mirrors>

2个回答

2
尽管您没有将中央存储库添加到项目中,但是您的项目从超级POM继承了中央存储库。您可以通过镜像完全排除中央存储库,您可以在此处阅读更多信息。
默认情况下,Maven首先检查本地存储库,然后检查setting.xml中添加的存储库。
另外,我在您的日志中看到Maven正在下载一些核心依赖项,我认为这些依赖项用于运行组装插件,我猜您没有这些基础依赖项,因此Maven会从中央存储库获取它们。

所以,即使我将不同的repo定义为中央仓库,如果存在缺失的依赖项,Maven会去maven-central吗? 因为这并不总是发生,当我添加一个我在我的artifactory中没有的新依赖项(或新版本)时,完成失败了(它不会去maven-central)。 此外,当我使用镜像时,编译成功通过而不需要去maven-central。这意味着我在我的artifactory中有这些构件,那么为什么要去maven-central呢? - yiaca

0

这里的问题在于id标签“central”中给定的名称。

<repositories>
        <repository>
            <id>maven-central</id>
            <name>My-repo</name>
            <url>http://My-repo/remote</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>maven-central</id>
            <name>My-repo-release</name>
            <url>http://my-repo/plugins-release</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>

        <pluginRepository>
            <id>release</id>
            <name>My-repo-snapshot</name>
            <url>http://my-repo/plugins-snapshot</url>
            <releases>
                <enabled>false</enabled>
            </releases>
        </pluginRepository>

    </pluginRepositories>

这里是链接(在此处搜索“central”单词)。其中有一点指出,“central”指向Maven中央仓库(https://repo.maven.apache.org/maven2/)。

因此,如果您的ID指向某个自定义仓库,解决方案不是使用“central”单词。例如- 您的Nexus仓库等。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接