首页 文章

如何强制Maven使用我的本地存储库而不是去远程repos来检索工件?

提问于
浏览
53

我在Mac Yosemite上使用Maven 3.3.3和Java 8 . 我有一个多模块项目 .

<modules>
            <module>first-module</module>
            <module>my-module</module>
                …
    </modules>

当我构建我的一个子模块,例如,上面的“my-module”,使用“mvn clean install”时,构建尝试从我在〜/ .m2中定义的远程存储库下载子模块工件 . /settings.xml文件 . 输出低于

[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building my-module 87.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: https://my.remoterepository.com/nexus/content/repositories/snapshots/org/mainco/subco/first-module/87.0.0-SNAPSHOT/maven-metadata.xml
Downloading: http://download.java.net/maven/2/org/mainco/subco/first-module/87.0.0-SNAPSHOT/maven-metadata.xml
Downloaded: https://my.remoterepository.com/nexus/content/repositories/snapshots/org/mainco/subco/first-module/87.0.0-SNAPSHOT/maven-metadata.xml (788 B at 0.9 KB/sec)
Downloading: https://my.remoterepository.com/nexus/content/repositories/snapshots/org/mainco/subco/first-module/87.0.0-SNAPSHOT/first-module-87.0.0-20151104.200545-4.pom

在尝试从远程存储库下载之前,如何强制Maven先检查我的本地〜/ .m2 / repository?下面是我在〜/ .m2 / settings.xml文件中定义远程存储库的地方......

<profile>
    <id>releases</id>
    <activation>
        <property>
            <name>!releases.off</name>
        </property>
    </activation>
    <repositories>
        <repository>
            <id>releases</id>
            <url>https://my.remoterepository.com/nexus/content/repositories/releases/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
</profile>
<profile>
    <id>snapshots</id>
    <activation>
        <property>
            <name>!snapshots.off</name>
        </property>
    </activation>
    <repositories>
        <repository>
            <id>snapshots</id>
            <url>https://my.remoterepository.com/nexus/content/repositories/snapshots/</url>
            <releases>
                <enabled>false</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
</profile>

Edit: 响应答案说下载发生在神器不存在的情况下,下面是终端输出,其中我证明文件在我的仓库中,但是Maven无论如何都试图下载它...

Daves-MacBook-Pro-2:my-module davea$ ls -al ~/.m2/repository/org/mainco/subco/first-module/87.0.0-SNAPSHOT/first-module-87.0.0-SNAPSHOT.jar 
-rw-r--r--  1 davea  staff  10171 Nov  5 10:22 /Users/davea/.m2/repository/org/mainco/subco/first-module/87.0.0-SNAPSHOT/first-module-87.0.0-SNAPSHOT.jar
Daves-MacBook-Pro-2:my-module davea$ mvn clean install
[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for org.mainco.subco:my-module:jar:87.0.0-SNAPSHOT
[WARNING] 'build.plugins.plugin.(groupId:artifactId)' must be unique but found duplicate declaration of plugin org.apache.maven.plugins:maven-antrun-plugin @ org.mainco.subco:my-module:[unknown-version], /Users/davea/Documents/sb_workspace/my-module/pom.xml, line 678, column 12
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building my-module 87.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: https://my.remoterepository.com/nexus/content/repositories/snapshots/org/mainco/subco/first-module/87.0.0-SNAPSHOT/maven-metadata.xml
Downloading: http://download.java.net/maven/2/org/mainco/subco/first-module/87.0.0-SNAPSHOT/maven-metadata.xml
Downloaded: https://my.remoterepository.com/nexus/content/repositories/snapshots/org/mainco/subco/first-module/87.0.0-SNAPSHOT/maven-metadata.xml (788 B at 0.8 KB/sec)
Downloading: https://my.remoterepository.com/nexus/content/repositories/snapshots/org/mainco/subco/first-module/87.0.0-SNAPSHOT/first-module-87.0.0-20151106.043202-8.pom
Downloaded: https://my.remoterepository.com/nexus/content/repositories/snapshots/org/mainco/subco/first-module/87.0.0-SNAPSHOT/first-   module-87.0.0-20151106.043202-8.pom (3 KB at 21.9 KB/sec)
Downloading: http://download.java.net/maven/2/org/mainco/subco/subco/87.0.0-SNAPSHOT/maven-metadata.xml
Downloading: https://my.remoterepository.com/nexus/content/repositories/snapshots/org/mainco/subco/subco/87.0.0-SNAPSHOT/maven-metadata.xml

6 回答

  • 5

    要真正强制maven仅使用您的本地仓库,您可以使用 mvn <goals> -o 运行 . -o 告诉maven让你工作"offline",它将离开网络 .

  • 3

    依赖项具有快照版本 . 对于快照,Maven将检查本地存储库,如果在本地存储库中找到的工件太旧,它将尝试在远程存储库中查找更新的工件 . 这可能就是你所看到的 .

    请注意,此行为由存储库配置中的updatePolicy指令控制(对于快照存储库,默认情况下为 daily ) .

  • -3

    使用 mvn --help ,您可以看到选项列表 .

    有像 -nsu,--no-snapshot-updates Suppress SNAPSHOT updates 这样的选项

    因此使用命令 mvn install -nsu 可以强制使用本地存储库进行编译 .

  • 1

    就我而言,我有一个像你一样的多模块项目 . 我必须更改我的项目所依赖的外部库之一的组ID,如下所示 .

    从:

    <dependencyManagement>
        <dependency>
            <groupId>org.thirdparty</groupId>
            <artifactId>calculation-api</artifactId>
            <version>2.0</version>
            <type>jar</type>
            <scope>provided</scope>
        </dependency>
    <dependencyManagement>
    

    至:

    <dependencyManagement>
       <dependency>
          <groupId>org.thirdparty.module</groupId>
            <artifactId>calculation-api</artifactId>
            <version>2.0</version>
            <type>jar</type>
            <scope>provided</scope>
        </dependency>
    <dependencyManagement>
    

    注意<groupId>部分 . 事实证明,我忘记修改在其pom文件中定义此依赖关系的子模块的相应部分 .

    它让我非常疯狂,因为该模块在本地可用 .

  • 16

    -o选项对我不起作用,因为工件仍处于开发状态且尚未上传,并且maven(3.5.x)仍尝试从远程存储库下载它,因为这是第一次,根据我得到的错误 .

    但是这为我修好了:https://maven.apache.org/general.html#importing-jars

    手动安装后,无需使用离线选项 .

    UPDATE

    我刚刚重建了依赖项,我不得不重新导入它:常规 mvn clean install 对我来说还不够

  • 27

    Maven总是首先检查您的本地存储库,但是,您的依赖项需要安装在您的repo中以便maven找到它 .

    首先在依赖项模块中运行 mvn install ,然后构建依赖模块 .

相关问题