将Maven项目作为依赖项添加到Maven项目中

huangapple go评论63阅读模式
英文:

Add maven project as dependency in maven project

问题

我有两个Maven项目,都是从Eclipse的默认Maven结构创建的。

项目A依赖于项目B。为了在B中使用来自A的类,我可以将B添加到A的构建路径中。

如何使用Maven实现相同的效果?

目前,我在文件系统上都有这两个项目,但我希望以尽可能类似的方式将这个依赖关系添加进来,就像从远程存储库添加GSON一样(图1),因为这是我未来处理自己的存储库的方式。

目前,当我尝试这样做时:

<dependency>
	<groupId>vision.voltsofdoom</groupId>
	<artifactId>voltsofdoom</artifactId>
	<version>0.0.1</version>
	<type>pom</type>
</dependency>

...并且从构建路径中删除任何其他项目,Eclipse无法解析我的任何导入(当我将项目B(voltsofdoom)添加到项目A(casketofazamgarath)的构建路径时,一切正常)。

项目B(voltsofdoom)的基本pom文件如下。

<groupId>vision.voltsofdoom</groupId>
<artifactId>voltsofdoom</artifactId>
<version>0.0.1</version>
<packaging>pom</packaging>

项目B(voltsofdoom)未出现在“Maven Repositories”视图中的“Local Repository”选项卡中,而是出现在“Workspace Projects”中。

图1(添加GSON):

<dependency>
    <groupId>com.google.code.gson</groupId>
	<artifactId>gson</artifactId>
	<version>2.3.1</version>
</dependency>

==

回答:
按照正确标记的答案操作。
修改后的片段如下:

<dependency>
	<groupId>vision.voltsofdoom</groupId>
	<artifactId>voltsofdoom</artifactId>
	<version>0.0.1</version>
</dependency>
英文:

I have two Maven projects, both created from the default Maven structure in Eclipse.

Project A is dependant on Project B. In order to utilise classes from A in B, I can add B to A's build path.

How can I achieve the same effect using Maven?

Currently, I have both of the projects on my file system, but I would like to add this dependency in as similar a way as possible to, for example, adding GSON from a remote repository (Fig. 1), as that is how I would like to handle my own repositories in future.

Currently, when I try this:

&lt;dependency&gt;
	&lt;groupId&gt;vision.voltsofdoom&lt;/groupId&gt;
	&lt;artifactId&gt;voltsofdoom&lt;/artifactId&gt;
	&lt;version&gt;0.0.1&lt;/version&gt;
	&lt;type&gt;pom&lt;/type&gt;
&lt;/dependency&gt;

... and remove any other projects from the build path, Eclipse is unable to resolve any of my imports (everything works fine when I add Project B (voltsofdoom) to A's (casketofazamgarath) build path.

B's (voltsofdoom) basic pom looks like this.

	&lt;groupId&gt;vision.voltsofdoom&lt;/groupId&gt;
	&lt;artifactId&gt;voltsofdoom&lt;/artifactId&gt;
	&lt;version&gt;0.0.1&lt;/version&gt;
	&lt;packaging&gt;pom&lt;/packaging&gt;

B (voltsofdoom) does not appear in the "Local Repository" tab in the "Maven Repositories" View, instead appearing in "Workspace Projects".

Figure 1 (Adding GSON):

&lt;dependency&gt;
    &lt;groupId&gt;com.google.code.gson&lt;/groupId&gt;
	&lt;artifactId&gt;gson&lt;/artifactId&gt;
	&lt;version&gt;2.3.1&lt;/version&gt;
&lt;/dependency&gt;

==

Answer:
Follow the correct marked answer.
The amended snippet is:

&lt;dependency&gt;
	&lt;groupId&gt;vision.voltsofdoom&lt;/groupId&gt;
	&lt;artifactId&gt;voltsofdoom&lt;/artifactId&gt;
	&lt;version&gt;0.0.1&lt;/version&gt;
&lt;/dependency&gt;

答案1

得分: 0

你可以在不依赖其他项目(即“dependency”)的项目上运行“maven clean install”目标,这将把你的项目“导出”到你用户目录中的本地.m2目录。

之后,你应该可以使用你在问题中提到的代码片段从依赖项目中访问它(尽管我不太确定<type>标签的作用是什么)。

英文:

You can run the maven clean install goal on the project that has no dependency on the other (i.e. the "dependency"), which will "export" your project to your local .m2 directory in your users directory.

After that, you should be able to access it from the depending project with the code snippet you mentioned in your question (although I'm not quite sure what the &lt;type&gt; tag does).

huangapple
  • 本文由 发表于 2020年9月14日 01:18:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63873544.html
匿名

发表评论

匿名网友

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

确定