Maven 3不会从本地存储库更新快照依赖关系 [英] Maven 3 does not update snapshot dependency from local repository
问题描述
我正在尝试在我的Maven项目中使用外部库.因为我希望项目可以在任何计算机上直接使用,所以我不想使用mvn install
解决方案.因此,我在pom.xml中定义了本地存储库:
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 install
solution. I have therefore defined local repository in my 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>
问题是,当我替换libRepo
中的jar(因为它只是另一个快照而没有更新版本号)时,即使对于
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 .m2
directory is used instead) even for mvn -U clean install
How to make maven to update this jar?
编辑: 根据> Maven快照到底是什么以及我们为什么需要它? maven绝不会尝试查找SNAPSHOT依赖项的版本,即使在本地存储库中找到了该库的一个版本".我的设置怎么了?
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 2程序集的回答:范围系统"下的jar不包括以下对我原始解决方案的扩展似乎有效:
DIRTY SOLUTION: Based on answer from Maven 2 assembly with dependencies: jar under scope "system" not included following extension of my original solution seems to work:
<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>
如对该解决方案的评论中所述,它不能单独工作,因此它可以与in-project
存储库结合使用(当本地.m2
存储库中不存在依赖项时可以使用),第二部分刷新了
As mentioned in comment to that solution, it does not work alone, thus it works in combination with in-project
repository (which works when the dependency is not available in local .m2
repository) and this second parts refreshes .m2
during every build.
但是,我仍然不清楚为什么普通的"SNAPSHOT"机制不起作用(即,当前的脏解决方案在没有SNAPSHOT的情况下也可以工作,因为每次都会显式更新本地.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 .m2
repo is explicitly updated every time). Is there any cleaner way?
解决方案(基于亚伦的回答和讨论):问题是我试图使用install-file
将文件安装到libRepo
中.实际的解决方案是,如果库更新,请使用
SOLUTION (based on Aaron's answer & discussion): The problem was that I tried to install file into libRepo
using install-file
. The actual solution is that if library updates, use
mvn deploy:deploy-file -Dfile=fooLib.jar -DgroupId=com.test \
-DartifactId=fooLib -Dversion=1.0-SNAPSHOT -Dpackaging=jar \
-Durl=file://..\libRepo -DrepositoryId=in-project
部署它以回购.正确部署后,maven可以正确处理SNAPSHOT.
to deployed it to repo. After proper deploy, maven correctly handles SNAPSHOTs.
推荐答案
如果为此使用存储库,则Maven会将JAR复制一次到其本地存储库中(通常在$HOME/.m2/repository/
中).除非版本号更改,否则Maven不会认为此文件已更改,也不会复制它.注意,版本号是Maven唯一要看的东西.它不在乎校验和或文件大小或日期.
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可以在内部注意到快照已被更新.
Incidentally, snapshots are assigned a version number internally for just this purpose: So that Maven can internally notice that a snapshot has been updated.
我建议使用系统依赖项代替.这样,实际的JAR将被添加到类路径中(没有任何复制或填充).您也不需要为此方法复制存储库结构,它可以清楚地传达您的意图.
I suggest to use a system dependency instead. 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.
我了解Maven处理范围为system
的依赖项的方式有所不同.我不确定这是否有意义(如果它使用依赖项进行编译,那么肯定可以使用它来运行吗?)
I understand that Maven handles dependencies with scope system
differently. I'm not sure whether this makes sense or not (if it uses the dependency to compile, it surely can use it to run?)
如我所见,您有以下选择:
As I see it, you have these options:
Install the dependency into your
libRepo
using deploy:deploy-file instead of copying it yourself. That should update the meta data in such a way that Maven will copy it again when you runmvn install
again on the real project.
请注意,file:install
不起作用.该文件插件用于访问本地存储库,但是您需要使用知道如何更新共享/服务器存储库的deploy插件.
Note that file:install
doesn'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.
将依赖项安装到本地存储库中;我建议为此使用脚本,您可以将其包含在项目中.这样,您可以避免所有问题,并且可以很容易地在新计算机上进行设置.
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.
更改依赖项的版本号,但这很繁琐,当依赖项的实际版本号更改时,您可能会遇到麻烦.
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.
为您的公司设置本地回购服务器,并将依赖项部署到该服务器.这将需要几个小时,但是a)您将获得所有依赖项的本地缓存,从而使您的初始构建速度更快,并且b)它将使其他开发人员的安装速度更快.
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.
这篇关于Maven 3不会从本地存储库更新快照依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!