简体   繁体   English

将JAR Maven项目纳入WAR Maven项目

[英]Include JAR Maven project into WAR Maven project

I have two Maven projects: Project1 that packages to JAR, and Project2 that packages to WAR. 我有两个Maven项目:打包到JAR的Project1和打包到WAR的Project2。

<!-- Project1's POM -->
<groupId>foobar</groupId>
<artifactId>project1</artifactId>
<version>1.0</version>
<packaging>jar</packaging>

<!-- Project2's POM -->
<groupId>foobar</groupId>
<artifactId>project2</artifactId>
<version>1.0</version>
<packaging>war</packaging>

I want to include Project1 into Project2, so in Project2's POM, I added: 我想将Project1包含在Project2中,因此在Project2的POM中,我添加了:

<dependency>
    <groupId>foobar</groupId>
    <artifactId>project1</artifactId>
    <version>1.0</version>
</dependency>

But Project2 is not able to resolve this dependency. 但是Project2无法解决这种依赖性。

In Project2's properties, Java Build Path > Libraries, I can see Project2 looking for "project1-1.0.jar" in my "~/.m2" folder. 在Project2的属性Java构建路径>库中,我可以看到Project2在我的“〜/ .m2”文件夹中寻找“ project1-1.0.jar”。 Is this normal? 这正常吗?

Did anyone meet this problem too and finally find a solution? 有人也遇到过这个问题,终于找到了解决方案吗? I can't make it work, even when looking to answers of related questions in SO. 即使在SO中寻找相关问题的答案时,我也无法使其正常工作。


  • Eclipse Kepler 日蚀开普勒
  • Maven 3.0.4 Maven的3.0.4
  • Maven Integration for Eclipse WTP (Juno) 1.0.1 Eclipse WTP(Juno)1.0.1的Maven集成

Unless you run maven install on your project1 . 除非您在project1上运行maven install。 It would not be packaged as a jar file into your .m2 repository, and hence it wouldnt be resolved in the project2 's pom file. 它不会作为jar文件打包到.m2存储库中,因此不会在project2的pom文件中解析。

This is similar to using a third party jar file. 这类似于使用第三方jar文件。 But not having it in the .m2 repository. 但是.m2存储库中没有它。 Even in that case, you would face the same problem. 即使在这种情况下,您也将面临相同的问题。

For your project 2, project 1 is treated as an independent third party jar file. 对于您的项目2,项目1被视为独立的第三方jar文件。 It needs to be in your .m2 repository so that it resolves it. 它必须位于您的.m2存储库中,以便对其进行解析。

To ensure that Eclipse WTP understand that your projects depend on each other, you need to define a multi-module parent project for both. 为了确保Eclipse WTP了解您的项目相互依赖,您需要为两者定义一个多模块父项目。 Otherwise, you need to "mvn install" project1 every time that you update it, and you will see the dependency go through ~/.m2 as you observed. 否则,您每次更新它时都需要“ mvn install” project1,并且您会看到依赖项遍历〜/ .m2。

There are many resources on the web about multi-module projects - the biggest choice you need to make is whether you want a flat layout (all project directories, including that of the parent module project, in the same base directory) or a hierarchical layout (the parent module project directory contains the sub module project directories). 网上有很多有关多模块项目的资源-您需要做出的最大选择是要使用平面布局(所有项目目录,包括父模块项目的目录,在同一基础目录中)还是分层布局(父模块项目目录包含子模块项目目录)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM