Maven:在尝试在线之前如何配置以使用本地存在的工件(~/.m2/repository)?

Posted

技术标签:

【中文标题】Maven:在尝试在线之前如何配置以使用本地存在的工件(~/.m2/repository)?【英文标题】:Maven: how to configure to use an artifact present locally (~/.m2/repository) before trying online? 【发布时间】:2019-01-18 22:37:38 【问题描述】:

在本地有两个maven项目,分别是AuthenticationCommons, 我需要在身份验证的 pom.xml 中使用 Commons 作为依赖项, 在 Commons 根目录中运行 mvn clean install 后,它已正确部署在 ~/.m2/repository 的本地存储库中。

但是,当我在身份验证根目录中运行 maven clean install 时,我不知道如何阻止它尝试从在线下载此本地依赖项。

这是我得到的错误:

[ERROR] Failed to execute goal on project authentication: Could not resolve dependencies for project io.project:authentication:jar:0.0.1-SNAPSHOT: Failure to find io.project.commons:project-commons:jar:0.0.1.RELEASE in https://repo.spring.io/snapshot was cached in the local repository, resolution will not be reattempted until the update interval of spring-snapshots has elapsed or updates are forced

这是Authentication的pom:

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

<groupId>io.project</groupId>
<artifactId>authentication</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>authentication</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.5.BUILD-SNAPSHOT</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <spring-cloud.version>Finchley.BUILD-SNAPSHOT</spring-cloud.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt</artifactId>
        <version>0.9.0</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>io.project.commons</groupId>
        <artifactId>project-commons</artifactId>
        <version>0.0.1.RELEASE</version>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>$spring-cloud.version</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<repositories>
    <repository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/snapshot</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

<pluginRepositories>
    <pluginRepository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/snapshot</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </pluginRepository>
    <pluginRepository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>

虽然这是 Commons 的 pom:

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

<groupId>io.project</groupId>
<artifactId>project-commons</artifactId>
<version>0.0.1.RELEASE</version>
<packaging>jar</packaging>

<name>commons</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.4.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <spring-cloud.version>Finchley.BUILD-SNAPSHOT</spring-cloud.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>$spring-cloud.version</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<repositories>
    <repository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/snapshot</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

<pluginRepositories>
    <pluginRepository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/snapshot</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </pluginRepository>
    <pluginRepository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>

是不是可以配置pom先看本地仓库,再去网上搜索?

【问题讨论】:

检查***.com/q/33548395/6407858。 【参考方案1】:

查看您的坐标。在身份验证中,你依赖io.project.commons:project-commons

<dependency>
    <groupId>io.project.commons</groupId>
    <artifactId>project-commons</artifactId>
    <version>0.0.1.RELEASE</version>
</dependency>

但是commons项目上的实际坐标是io.project:project-commons

<groupId>io.project</groupId>
<artifactId>project-commons</artifactId>

【讨论】:

以上是关于Maven:在尝试在线之前如何配置以使用本地存在的工件(~/.m2/repository)?的主要内容,如果未能解决你的问题,请参考以下文章

maven是怎么判断包在本地仓库和远程仓库哪个是新的

怎么自定义maven的中央仓库位置

怎么自定义maven的中央仓库位置?

eclipse怎么配置阿里云maven?

如何配置 maven 以生成更详细的 Jmeter 结果,类似于 Jmeter GUI 中的视图结果树?

eclipse怎么配置阿里云maven?