首页 > 解决方案 > Maven-3.3.9 总是从远程而不是从本地拉依赖

问题描述

我正在从 Maven-3.0.X 迁移到 Maven-3.3.9,以适应最近的万能插件版本。

但是我面临的问题是在这次迁移过程中,我发现 Maven 总是从远程而不是从本地提取依赖项,即使我们在本地有可用的存储库。我希望 Maven 最初会查找本地,如果依赖项 NA 然后会拉出远程。

笔记

1)我能够强制我的 maven 进行离线构建 [-o] 或传递 -Dmaven.legacyLocalRepo=true 以工作,我不想实现。

2)尝试使用 UpdatePolicy="never" 选项,但没有运气

POM.xml

<?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/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com</groupId>
    <artifactId>test</artifactId>
    <version>1.0.0</version>
    <packaging>mule</packaging>
    <name>Test Application</name>

    <build>
        <plugins>
            <plugin>
                <groupId>org.mule.tools.maven</groupId>
                <artifactId>mule-app-maven-plugin</artifactId>
                <version>1.7</version>
                <extensions>true</extensions>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.2.1</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>project</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>1.7</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.4</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.7</version>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>com.mulesoft.muleesb</groupId>
            <artifactId>mule-core</artifactId>
            <version>3.6.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>Central</id>
            <name>Central</name>
            <url>http://repo1.maven.org/maven2/</url>
            <layout>default</layout>
        </repository>
        <repository>
            <id>mulesoft-releases</id>
            <name>MuleSoft Releases Repository</name>
            <url>http://repository.mulesoft.org/releases/</url>
            <layout>default</layout>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>Central</id>
            <name>Central</name>
            <url>http://repo1.maven.org/maven2/</url>
            <layout>default</layout>
        </pluginRepository>
        <pluginRepository>
            <id>mulesoft-releases</id>
            <name>mulesoft release repository</name>
            <layout>default</layout>
            <url>http://repository.mulesoft.org/releases/</url>
        </pluginRepository>
    </pluginRepositories>
</project>

错误日志

Downloading from repo.nexus: http://127.0.0.1:9080/nexus/content/repositories/public/org/mule/tools/maven/mule-esb-maven-plugins/1.7/mule-esb-maven-plugins-1.7.pom
Downloading from jfrog: http://repo.jfrog.org/artifactory/plugins-releases-local/org/mule/tools/maven/mule-esb-maven-plugins/1.7/mule-esb-maven-plugins-1.7.pom
[ERROR] [ERROR] Some problems were encountered while processing the POMs:
[ERROR] Unresolveable build extension: Plugin org.mule.tools.maven:mule-app-maven-plugin:1.7 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.mule.tools.maven:mule-app-maven-plugin:jar:1.7 @ 
[ERROR] Unknown packaging: mule @ line 8, column 16
 @ 
[ERROR] The build could not read 1 project -> [Help 1]
[ERROR]   
[ERROR]   The project com.test:TEST_PROJECT:1.0-SNAPSHOT (/usr/local/TEST_PROJECT/pom.xml) has 2 errors
[ERROR]     Unresolveable build extension: Plugin org.mule.tools.maven:mule-app-maven-plugin:1.7 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.mule.tools.maven:mule-app-maven-plugin:jar:1.7: Could not transfer artifact org.mule.tools.maven:mule-esb-maven-plugins:pom:1.7 from/to jfrog (http://repo.jfrog.org/artifactory/plugins-releases-local): Connect to repo.jfrog.org:80 [repo.jfrog.org/52.7.30.14, repo.jfrog.org/34.231.202.145] failed: Connection timed out -> [Help 2]
[ERROR]     Unknown packaging: mule @ line 8, column 16
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectBuildingException
[ERROR] [Help 2] http://cwiki.apache.org/confluence/display/MAVEN/PluginManagerException

我希望 Maven 先引用本地然后再引用远程,因为这是它之前在 Maven-3.0.X 中的工作方式。

标签: mavenmaven-3maven-plugin

解决方案


我尝试pom.xml使用 Maven 3.3.9(和 3.6.0)构建您的示例:没有com.mulesoft.muleesb:mule-core:3.6.0:jar; 最接近的似乎是org.mule:mule-core:3.6.0

如果没有找到该依赖的 POM,Maven 将发出警告并存储一个.lastUpdated文件。该文件包含一个时间戳,指示何时为某个存储库发出错误。

当两次执行之间经过一段时间-U或如果使用时,Maven 将重试下载依赖项(或其 POM)。我认为这就是你得到错误的地方。

POM 对于 Maven 不是强制性的,它会发出警告:

[WARNING] The POM for com.mulesoft.muleesb:mule-core:jar:3.6.0 is missing, no dependency information available

但是,当它尝试下载它时,它至少必须达到一些404 NOT FOUND:我使用了一些本地未绑定的 IP,这也失败了:

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Test Application 1.0.0
[INFO] ------------------------------------------------------------------------
Downloading: https://192.168.113.145/com/mulesoft/muleesb/mule-core/3.6.0/mule-core-3.6.0.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 22.336 s
[INFO] Finished at: 2018-12-27T20:04:11+01:00
[INFO] Final Memory: 10M/245M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project test: Could not resolve dependencies for project com:test:mule:1.0.0: Failed to collect dependencies at com.mulesoft.muleesb:mule-core:jar:3.6.0: Failed to read artifact descriptor for com.mulesoft.muleesb:mule-core:jar:3.6.0: Could not transfer artifact com.mulesoft.muleesb:mule-core:pom:3.6.0 from/to bintray (https://192.168.113.145): Connect to 192.168.113.145:443 [/192.168.113.145] failed: Connection timed out: connect -> [Help 1]

此错误与您的错误不同,但至少或多或少地解释了为什么 Maven 选择远程。

还有其他情况:

  • 如果插件/依赖项没有明确的版本,Maven 将尝试从存储库下载插件元数据(这意味着“采用最新的”,可能在有限的范围内 [例如:maven 的版本,传递依赖项......,)。默认插件,如maven-clean-plugin,由 Maven 版本固定。
  • 您使用SNAPSHOT版本,Maven 将获取最新的(除非在您的 中禁用<repository>

话虽这么说,我认为你的问题(你的问题)是错误的:

但是面临的问题是在这次迁移过程中,我发现 Maven 总是从远程而不是从本地提取依赖项,即使我们在本地有可用的存储库。

Maven 由于无法远程找到依赖项而失败的事实意味着,在您的配置中,您的构建将无法在另一台机器上运行:

  1. 您的 Nexus 存储库 ( http://127.0.0.1:9080/nexus ) 没有存储依赖项,这意味着它没有达到目的(拥有企业存储库是为了避免来自外部的问题,首先是删除或替换)。
  2. 127.0.0.1除非您出于其他原因将企业存储库代理到您的本地主机,否则它不应位于单独的服务器上;至少,它应该被备份。
  3. settings.xml应该使用该存储库的镜像(如果您想确保不从外部源下载依赖项/插件)。

特别是,如果您更改本地存储库(例如,指向<localRepository>~/.m2/repository.fresh,则构建不应由于缺少这些依赖项而失败。


推荐阅读