Maven依赖在Eclipse子项目中不会显示在Java类路径中。

huangapple 未分类评论48阅读模式
英文:

Maven dependencies in Eclipse subprojects not appearing the Java classpath

问题

在Eclipse Java中,我有以下情况:

项目1
+- 项目2
   +- 项目3

项目1、项目2和项目3都是常规的Java项目,通过文件 > 新建Java项目创建,然后通过在项目资源管理器中右键单击 > Maven > 转换为Maven项目转换为Maven项目。项目1包含我的应用程序入口点(静态main方法)。项目1的代码调用项目2的代码,而项目2的代码又调用项目3的代码:

// 项目1
public class 项目1类 {
	public static void main(String[] args)
	{
		项目2类.foo();
		System.out.println("完成");
	}
}
// 项目2
public class 项目2类 {
	public static void foo()
	{
		项目3类.bar();
	}
}
// 项目3
public class 项目3类 {
	public static void bar()
	{
       // 在这里使用本地代码
	}
}

项目3使用带有本地库文件(Windows上的DLL文件)的Maven依赖项。让我们称这个依赖为org.foo.bar/mylibrary:

<!-- 这个依赖项需要加载本地库 -->
<dependencies>
 <dependency>
  <groupId>org.foo.bar</groupId>
  <artifactId>mylibrary</artifactId>
 </dependency>
</dependencies>

我的问题是:

当运行或调试项目1时,我可以看到项目1、项目2和项目3的'target/classes'文件夹在类路径中,如预期所示,项目1和项目2的Maven依赖项也在类路径中,但是项目3的Maven依赖项却丢失了。结果是,无法加载本地库。

如果我将org.foo.bar/mylibrary依赖项的声明从项目3移到项目2,那么Maven依赖项确实会出现在类路径中。

我尝试过Eclipse 2019-09和2020-03,结果相同。

似乎Eclipse不会添加驻留在间接依赖项中的Maven依赖项。
我如何告诉它将Maven依赖项添加到类路径中,包括所有依赖项,即直接和间接依赖项?

编辑:我在项目2的pom.xml文件中添加了此显式声明:

<dependencies>
	<dependency>
		<groupId>项目3</groupId>
		<artifactId>项目3</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</dependency>
</dependencies>

而且它有效。但这对我来说没有太多意义。考虑到其他情况,如果我在项目1和项目2之间不添加显式的Maven 声明,事情确实可以正常工作。为什么?为什么我在项目2和项目3之间必须这样做,而在项目1和项目2之间却不必呢?

英文:

In Eclipse Java, I have the following situation:

Project1
+- Project2
   +- Project3

Project1, Project2 and Project3 are regular Java projects that have been created through File > New Java project, then converted to Maven projects through a right-click in Project Explorer > Maven > Convert to Maven. Project1 contains my application entrypoint (static main method). Project1 code calls Project2 code, which in turns calls Project3 code:

// Project1
public class Project1Class {
	public static void main(String[] args)
	{
		Project2Class.foo();
		System.out.println(&quot;Done&quot;);
	}
}
// Project2
public class Project2Class {
	public static void foo()
	{
		Project3Class.bar();
	}
}
// Project3
public class Project3Class {
	public static void bar()
	{
       // Use native code here
	}
}

Project3 uses a Maven dependency with native library files (DLL files on Windows). Let's call this dependency org.foo.bar/mylibrary:

&lt;!-- This dependency needs to load a native library --&gt;
&lt;dependencies&gt;
 &lt;dependency&gt;
  &lt;groupId&gt;org.foo.bar&lt;/groupId&gt;
  &lt;artifactId&gt;mylibrary&lt;/artifactId&gt;
 &lt;/dependency&gt;
&lt;/dependencies&gt;

My problem is the following:

When running or debugging Project1, I can see that Project1, Project2 and Project3 'target/classes' folders are in the classpath, as expected, Maven dependencies for Project1 and Project2 also are in the classpath, but the Maven dependencies of Project3 are missing. As a result, native libraries cannot be loaded.

If I move the org.foo.bar/mylibrary dependency declaration from Project3 to Project2, then the Maven dependency indeed ends up in the classpath.

I tried with Eclipse 2019-09 and 2020-03, same result.

It seems Eclipse does not add Maven dependencies that reside in indirect dependencies.
How can I tell it to add Maven dependencies in the classpath for all dependencies, i.e. both direct and indirect?

EDIT: I added this explicit declaration in Project2's pom.xml file:

&lt;dependencies&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;Project3&lt;/groupId&gt;
		&lt;artifactId&gt;Project3&lt;/artifactId&gt;
		&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
	&lt;/dependency&gt;
&lt;/dependencies&gt;

and it works. But that makes little sense to me. Considering the other scenario, things do work if I don't add an explicit Maven <dependency> declaration between Project1 and Project2. Why? Why do I have to do it between Project2 and Project3 and not between Project1 and Project2 for it to work?

huangapple
  • 本文由 发表于 2020年5月3日 19:20:37
  • 转载请务必保留本文链接:https://java.coder-hub.com/61573588.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定