Java Maven 3 不会从本地存储库更新快照依赖项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22015685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:04:56  来源:igfitidea点击:

Maven 3 does not update snapshot dependency from local repository

javamaven

提问by sodik

I am trying to use external library inside my maven project. Since I want the project to build out of the box on any machine, I don't want to use mvn installsolution. I have therefore defined local repository in my pom.xml:

我正在尝试在我的 Maven 项目中使用外部库。由于我希望该项目在任何机器上开箱即用,因此我不想使用mvn install解决方案。因此,我在 pom.xml 中定义了本地存储库:

    <dependency>
        <groupId>com.test</groupId>
        <artifactId>fooLib</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    ....
    <repository>
        <id>in-project</id>
        <snapshots>
            <updatePolicy>always</updatePolicy>
            <enabled>true</enabled>
        </snapshots>
        <name>In Project Repo</name>
        <url>file://${project.basedir}/libRepo</url>
    </repository>

The problem is when I replace the jar in libRepo(without updating version number since it is just another snapshot) that this updated jar is not used (old version from .m2directory is used instead) even for mvn -U clean installHow to make maven to update this jar?

问题是当我替换 jar 时libRepo(不更新版本号,因为它只是另一个快照),这个更新的 jar 没有被使用(而是使用.m2目录中的旧版本),即使是mvn -U clean installHow to make maven to update this jar?

EDIT: According to What exactly is a Maven Snapshot and why do we need it?maven shall try to find never version of SNAPSHOT dependency, "even if a version of this library is found on the local repository". What is wrong with my setting?

编辑:根据什么是 Maven 快照,我们为什么需要它?maven 将尝试查找从未版本的 SNAPSHOT 依赖项,“即使在本地存储库中找到了此库的版本”。我的设置有什么问题?

DIRTY SOLUTION: Based on answer from Maven 2 assembly with dependencies: jar under scope "system" not includedfollowing extension of my original solution seems to work:

肮脏的解决方案:基于来自Maven 2 程序集的回答和依赖项:在我原来的解决方案的扩展之后不包括范围“系统”下的 jar似乎有效:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-install-plugin</artifactId>
            <executions>
                <execution>
                    <id>hack-binary</id>
                    <phase>validate</phase>
                    <configuration>
                        <file>${repo.path.to.jar}</file>
                        <repositoryLayout>default</repositoryLayout>
                        <groupId>com.test</groupId>
                        <artifactId>fooLib</artifactId>
                        <version>1.0-SNAPSHOT</version>
                        <packaging>jar</packaging>
                        <generatePom>true</generatePom>
                    </configuration>
                    <goals>
                        <goal>install-file</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

As mentioned in comment to that solution, it does not work alone, thus it works in combination with in-projectrepository (which works when the dependency is not available in local .m2repository) and this second parts refreshes .m2during every build.

正如对该解决方案的评论中所述,它不能单独工作,因此它与in-project存储库结合使用(当本地.m2存储库中的依赖项不可用时工作),并且第二部分.m2在每次构建期间刷新。

However it is still not clear to me why ordinary "SNAPSHOT" mechanism does not work (i.e. current dirty solution would work also without SNAPSHOTs as local .m2repo is explicitly updated every time). Is there any cleaner way?

然而,我仍然不清楚为什么普通的“快照”机制不起作用(即当前的脏解决方案也可以在没有快照的情况下工作,因为.m2每次都显式更新本地存储库)。有没有更干净的方法?

SOLUTION(based on Aaron's answer & discussion): The problem was that I tried to install file into libRepousing install-file. The actual solution is that if library updates, use

解决方案(基于 Aaron 的回答和讨论):问题是我试图将文件安装到libRepo使用install-file. 实际的解决方案是,如果库更新,使用

mvn deploy:deploy-file -Dfile=fooLib.jar  -DgroupId=com.test \
    -DartifactId=fooLib -Dversion=1.0-SNAPSHOT -Dpackaging=jar \
    -Durl=file://..\libRepo -DrepositoryId=in-project

to deployed it to repo. After proper deploy, maven correctly handles SNAPSHOTs.

将其部署到回购。正确部署后,maven 正确处理快照。

采纳答案by Aaron Digulla

If you use a repository for this, then Maven will copy the JAR once into it's local repository (usually in $HOME/.m2/repository/). Unless the version number changes, Maven won't consider this file to have changed and it won't copy it. Note that the version number is the only thing that Maven looks at; it doesn't care about checksums or file sizes or dates.

如果您为此使用存储库,那么 Maven 会将 JAR 复制一次到它的本地存储库(通常在 中$HOME/.m2/repository/)。除非版本号改变,Maven 不会认为这个文件已经改变,也不会复制它。请注意,版本号是 Maven 唯一查看的内容;它不关心校验和或文件大小或日期。

Incidentally, snapshots are assigned a version number internally for just this purpose: So that Maven can internally notice that a snapshot has been updated.

顺便提一下,快照在内部分配了一个版本号只是为了这个目的:这样 Maven 可以在内部注意到快照已经更新。

I suggest to use a system dependencyinstead. That way, the actual JAR is going to be added to the classpath (without any copying or stuff). You also don't need to replicate a repo structure for this approach and it will clearly communicate your intent.

我建议改用系统依赖项。这样,实际的 JAR 将被添加到类路径中(没有任何复制或内容)。您也不需要为这种方法复制回购结构,它会清楚地传达您的意图。

[EDIT]I understand that Maven handles dependencies with scope systemdifferently. I'm not sure whether this makes sense or not (if it uses the dependency to compile, it surely can use it to run?)

[编辑]我知道 Maven 以system不同的方式处理具有范围的依赖项。我不确定这是否有意义(如果它使用依赖项进行编译,它肯定可以使用它来运行?)

As I see it, you have these options:

在我看来,您有以下选择:

  1. Install the dependency into your libRepousing deploy:deploy-fileinstead of copying it yourself. That should update the meta data in such a way that Maven will copy it again when you run mvn installagain on the real project.

    Note that file:installdoesn't work. The file plugin is used to access the local repository but you need to use the deploy plugin which knows how to update a shared / server repository.

  2. Install the dependency into your local repo; I suggest to use a script for this that you can include in your project. That way, you avoid all the problems and it will be easy to set this up on a new machine.

  3. Change the version number of the dependency but that's tedious and you might get into trouble when the real version number of the dependency changes.

  4. Set up a local repo server for your company and deploy the dependency to it. That will take a few hours but a) you will get a local cache of all your dependencies, making your initial builds faster and b) it will make setup for additional developers much faster.

  1. 将依赖项安装到您libRepo使用的deploy:deploy-file 中,而不是自己复制它。这应该以这样的方式更新元数据,当您mvn install再次在真实项目上运行时,Maven 将再次复制它。

    请注意,file:install这不起作用。文件插件用于访问本地存储库,但您需要使用知道如何更新共享/服务器存储库的部署插件。

  2. 将依赖项安装到本地仓库中;我建议为此使用一个可以包含在项目中的脚本。这样,您就可以避免所有问题,并且很容易在新机器上进行设置。

  3. 更改依赖项的版本号,但这很乏味,而且当依赖项的真实版本号更改时,您可能会遇到麻烦。

  4. 为您的公司设置一个本地 repo 服务器并将依赖项部署到它。这将需要几个小时,但是 a) 您将获得所有依赖项的本地缓存,使您的初始构建速度更快 b) 它将使其他开发人员的设置速度更快。

回答by ItachiUchiha

Maven will never read the jar present inside any library folder. You must first understand how maven works. The only place where maven looks for jar is the localRepository (.m2), if absent it searches the other repository, mentioned in your POM.xml

Maven 永远不会读取存在于任何库文件夹中的 jar。您必须首先了解 maven 是如何工作的。maven 查找 jar 的唯一地方是localRepository (.m2),如果不存在,它将搜索其他存储库,在您的POM.xml

回答by HymanLeEmmerdeur

The previous answerer (and one commenter) pointed out, that maven looks within the local project-repository only once, and on subsequent builds it gets the cached jar from the .m2-repository.

之前的回答者(和一位评论者)指出,maven 只在本地项目存储库中查找一次,并且在后续构建中它从 .m2 存储库中获取缓存的 jar。

I just founda workaround for the need to increment the version number (e.g. on small changes of a dev-library):

我刚刚找到了一种需要增加版本号的解决方法(例如,在开发库的小改动上):

First reinstall the jar into your local project-repository:

首先将 jar 重新安装到本地项目存储库中:

mvn install:install-file -DlocalRepositoryPath=repo -DcreateChecksum=true -Dpackaging=jar -DgroupId=com.johndoe.dev -DartifactId=Helpers -Dversion=0.1 -Dfile=C:/path/to/helpers.jar

(Where -Dfile could point to an external Project which produces the helpers.jar)

(其中 -Dfile 可以指向生成 helpers.jar 的外部项目)

Then purge just this specific artifact in the .m2-repository:

然后只清除 .m2-repository 中的这个特定工件:

mvn dependency:purge-local-repository -DmanualInclude=com.johndoe.dev:Helpers:0.1

(With com.johndoe.dev as the GroupId, Helpers as the ArtifactId and the same version installed in the previous step)

(以com.johndoe.dev为GroupId,Helpers为ArtifactId,与上一步安装的版本相同)

Executing the latter step, maven rebuilds the artifact inside .m2 using your local project-repository jar-file.

执行后一步,maven 使用本地项目存储库 jar 文件重建 .m2 中的工件。

Alternative and maybe dirty variant: Just copy helpers.jar to C:\Users\johndoe\.m2\repository\com\johndoe\dev\Helpers\0.1\Helpers-0.1.jar (don't know about linux as I haven't used mave there).

替代且可能是脏变体:只需将 helpers.jar 复制到 C:\Users\johndoe\.m2\repository\com\johndoe\dev\Helpers\0.1\Helpers-0.1.jar(我不了解 linux,因为我没有)在那里使用过 mave)。