英文:
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:
<dependency>
<groupId>vision.voltsofdoom</groupId>
<artifactId>voltsofdoom</artifactId>
<version>0.0.1</version>
<type>pom</type>
</dependency>
... 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.
<groupId>vision.voltsofdoom</groupId>
<artifactId>voltsofdoom</artifactId>
<version>0.0.1</version>
<packaging>pom</packaging>
B (voltsofdoom) does not appear in the "Local Repository" tab in the "Maven Repositories" View, instead appearing in "Workspace Projects".
Figure 1 (Adding GSON):
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3.1</version>
</dependency>
==
Answer:
Follow the correct marked answer.
The amended snippet is:
<dependency>
<groupId>vision.voltsofdoom</groupId>
<artifactId>voltsofdoom</artifactId>
<version>0.0.1</version>
</dependency>
答案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 <type>
tag does).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论