问题描述
我正在开发一个项目,使用Maven进行依赖/构建/任何事情(项目生命周期管理或某事),我正在使用Eclipse进行开发和测试。
I'm working on a project that uses Maven for dependency / building / whatever (project life cycle management or sth), and I'm using Eclipse to develop and test.
该项目使用Vert.x(最新版),我正在尝试使用Hazelcast进行一些集群管理,但我遇到了一个错误的问题,即Vert.x声明的Hazelcast版本是一个依赖项(3.6.3)和解决方案显然是升级到更新版本。
The project uses Vert.x (latest) and I'm trying to use Hazelcast for some cluster management, but I encountered a bug with the Hazelcast version that Vert.x declares as a dependency (3.6.3) and the solution apparently is to upgrade to a more recent version.
我在 pom.xml
中添加了更新的Hazelcast依赖关系:
I've added an updated Hazelcast dependency in my pom.xml
as such:
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
<version>[3.7,)</version>
</dependency>
并且maven确实更新了依赖关系(实际上我称为maven,只要我保存 pom.xml
file - very neat),所以我得到了依赖树:
And maven indeed updates the dependency (actually Eclipse called maven to update as soon as I save the pom.xml
file - pretty neat), and so I get the dependency tree:
$ mvn dependency:tree
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building project 3.8.4
[INFO] ------------------------------------------------------------------------
[WARNING] The POM for jfree:jfreechart:jar:1.0.8 is missing, no dependency information available
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ project ---
[INFO] my.group:project:jar:3.8.4
[INFO] +- some.private.dep ...
[INFO] | +- org.junit:junit4-engine:jar:5.0.0-ALPHA:compile (version selected from constraint [4,))
[INFO] | | \- org.junit:junit-engine-api:jar:5.0.0-ALPHA:compile
[INFO] | | +- org.junit:junit-commons:jar:5.0.0-ALPHA:compile
[INFO] | | \- org.opentest4j:opentest4j:jar:1.0.0-ALPHA:compile
[INFO] | +- io.vertx:vertx-hazelcast:jar:3.3.3:compile (version selected from constraint [3.0.0,))
[INFO] | \- io.vertx:vertx-web:jar:3.4.0.Beta1:compile (version selected from constraint [3.0.0,))
[INFO] | \- io.vertx:vertx-auth-common:jar:3.4.0.Beta1:compile
[INFO] +- io.vertx:vertx-core:jar:3.4.0.Beta1:compile
[INFO] | +- io.netty:netty-common:jar:4.1.8.Final:compile
[INFO] | +- io.netty:netty-buffer:jar:4.1.8.Final:compile
[INFO] | +- io.netty:netty-transport:jar:4.1.8.Final:compile
[INFO] | +- io.netty:netty-handler:jar:4.1.8.Final:compile
[INFO] | | \- io.netty:netty-codec:jar:4.1.8.Final:compile
[INFO] | +- io.netty:netty-handler-proxy:jar:4.1.8.Final:compile
[INFO] | | \- io.netty:netty-codec-socks:jar:4.1.8.Final:compile
[INFO] | +- io.netty:netty-codec-http:jar:4.1.8.Final:compile
[INFO] | +- io.netty:netty-codec-http2:jar:4.1.8.Final:compile
[INFO] | +- io.netty:netty-resolver:jar:4.1.8.Final:compile
[INFO] | \- io.netty:netty-resolver-dns:jar:4.1.8.Final:compile
[INFO] | \- io.netty:netty-codec-dns:jar:4.1.8.Final:compile
[INFO] +- junit:junit:jar:4.12:test
...
[INFO] +- com.hazelcast:hazelcast:jar:3.8-EA:compile
[INFO] \- org.slf4j:slf4j-jdk14:jar:1.7.22:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.785 s
[INFO] Finished at: 2017-02-06T09:52:46+02:00
[INFO] Final Memory: 25M/435M
[INFO] ------------------------------------------------------------------------
当我运行 mvn package
来创建带阴影的Jar时,我得到了正确的版本Hazelcast。
When I run mvn package
to create the shaded Jar, I get the correct version of Hazelcast.
问题是,如果我为运行项目或其单元测试创建Eclipse启动配置,它会插入旧版本的Hazelcast以及新版本的Hazelcase进入了类路径 - 他来自正在运行的单元测试的示例命令行:
The problem is that if I create an Eclipse launch configuration for running the project or its unit tests, it inserts both the old version of Hazelcast as well as the new version of Hazelcase into the classpath - here's an example command line from a unit test being run:
/usr/lib/jvm/java-8-openjdk-amd64/bin/java -agentlib:jdwp=transport=dt_socket,suspend=y,address=localhost:33123 -ea -Dfile.encoding=UTF-8
-classpath ...:
$HOME/.m2/repository/io/vertx/vertx-hazelcast/3.3.3/vertx-hazelcast-3.3.3.jar:
$HOME/.m2/repository/com/hazelcast/hazelcast/3.6.3/hazelcast-3.6.3.jar:
...
$HOME/.m2/repository/com/hazelcast/hazelcast/3.8-EA/hazelcast-3.8-EA.jar:
...
-version 3 -port 38387 -testLoaderClass org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader
-loaderpluginname org.eclipse.jdt.junit4.runtime
-classNames my.group.project.SomeTest
现在因为两个版本都被加载到类路径中,第一个版本(较旧版本)获胜我得到了错误而不是获得更新的固定版本。
Now because both versions are loaded into the classpath, the first one (the older) "wins" and I get the bug instead of getting the newer fixed version.
启动配置Classpath标签看起来非常标准:
The launch configuration "Classpath" tab looks very standard:
+ Bootstrap Entries
\- JRE System Library
+ User Enties
\- project
\- Maven Dependencies
Project Explorer视图中的Maven Dependencies文件夹仅显示较新的Hazelcast版本。
And the "Maven Dependencies" "folder" in the "Project Explorer" view shows only the newer Hazelcast version.
发生了什么?
更新:
根据评论中的讨论,我在 pom.xml
文件中添加了排除项,以防止 vertx -hazelcast
添加旧的 hazelcast
依赖项。因为 vertx-hazelcast
是从另一个(私有)依赖项加载的,所以新设置更复杂,看起来像这样:
As per the discussion in the comments, I added exclusions to the pom.xml
file to prevent vertx-hazelcast
from adding the old hazelcast
dependency. Because vertx-hazelcast
is loaded from yet another (private) dependency, the new setup is a bit more involved and looks like this:
<dependency>
<groupId>some.private</groupId>
<artifactId>dependency</artifactId>
<version>[1.3,)</version>
<exclusions>
<exclusion>
<groupId>io.vertx</groupId>
<artifactId>vertx-hazelcast</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
<version>[3.0.0,)</version>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
<version>[3.7,)</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-hazelcast</artifactId>
<version>[3.0.0,)</version>
<exclusions>
<exclusion>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
</exclusion>
</exclusions>
</dependency>
然后我删除旧的启动配置并重新创建它(通过右键单击JUnit测试用例并选择调试为Junit) - 但这并没有改变Eclipse行为 - 类路径中的一些jar已经移动了一点,但结果仍然是 vertx-hazelcast
及其 hazelcast-3.6.3.jar
依赖关系在 hazelcast-3.8.jar
之前加载。
I then removed the old launch configuration and recreated it (by right clicking the JUnit test case and choosing "Debug as Junit") - But that does not change the Eclipse behavior - some jars in the classpath have moved around a bit, but the result is still that vertx-hazelcast
and its hazelcast-3.6.3.jar
dependency are loaded before hazelcast-3.8.jar
.
推荐答案
注意此答案是与OP讨论的摘要
看起来像声明两个编译并测试 hazelcast
的范围依赖性。这些范围不是传递性的,所以我的理解是这个版本将被设计包含在类路径中。 @Echnalb建议使用排除是解决方法。
It looks like vertex-hazelcast declares both compile and test scoped dependencies on hazelcast
. These scopes are not transitive so it's my understanding that this version will be included in the classpath by design. @Echnalb suggestion to use an exclusion is the recommended way to resolve this.
由于这种情况没有奏效,它可能是一个祖先也宣布了一个也需要排除的依赖。
As that hasn't worked, it could be an ancestor is also declaring a dependency which would also need to be excluded.
在查看了在 vertex-hazelcast
中声明的其他依赖项之后,我检查了 hazelcast-client
看看它是否也声明了对早期版本的 hazelcast
的依赖,假设祖先依赖被添加到类路径中,尽管被排除在父类之外。
After looking through some other dependencies that were declared in vertex-hazelcast
, I checked hazelcast-client
to see if it also declared a dependency on the earlier version of hazelcast
the assumption being that the ancestral dependency was being added to the classpath despite the exclusion from the parent.
包括这样的依赖:
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
<scope>test</scope>
<version>${project.parent.version}</version>
<classifier>tests</classifier>
</dependency>
我建议( ed )为 hazelcast-client 这似乎解决了这个问题。
I suggest(ed) adding an exclusion for hazelcast-client
which seems to have solved the problem.
@Guss评论
- 在排除之前,运行
依赖:树
,hazelcast-client是
根本没有列出 - 有趣的是,
依赖:tree
没有检测到测试范围的依赖关系。有没有办法强制它切换范围?
- Prior to the exclusion, running
dependency:tree
, hazelcast-client isnot listed at all - It is interesting that
dependency:tree
does not detect test scoped dependencies. Is there's a way to force it to switch scopes?
这篇关于Eclipse在启动配置中使用错误的maven依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!