两个不同的项目在 Maven 中具有共享的依赖项目

发布于 2024-09-10 16:12:54 字数 504 浏览 10 评论 0原文

我有两个项目“appA”和“appB”,每个项目都依赖于第三个项目“common”。我正在将它们从 ant 转移到 Maven,但我在找出在 Maven 中执行此操作的最佳方法时遇到了一些困难。我的文件夹结构如下所示:

root
+ common  
+ appA  
+ appB  

我已经能够将父 pom.xml 添加到根目录,并将 common、appA 和 appB 作为此父项目的模块来构建 appA 和 appB,但这不是我想要的,根 pom.xml 是不合适的,因为 appA 和 appB 是完全不相关的项目。

我希望 appA 和 appB 成为单独的 Maven 项目,它们都依赖于第三个公共项目,并在必要时构建它,这样我就可以进入 appA 或 appB 文件夹并输入“mvn package”来构建 appA + common或 appB + common 分别。这就是我在 Ant 中设置它的方式,但在 Maven 中可能没有适当的相似之处。非常感谢任何朝着正确方向的帮助或推动:)

I have two projects, "appA" and "appB", that each depend on a third project, "common". I'm in the process of moving them from ant to maven, but I'm having some difficulty figuring out the best way to do it in maven. My folder structure looks like this:

root
+ common  
+ appA  
+ appB  

I've been able to add a parent pom.xml to the root and have common, appA and appB as modules of this parent project to get appA and appB to build, but this is not what I want, a root pom.xml is not appropriate since appA and appB are completely unrelated projects.

I'd like appA and appB to be separate maven projects which both depend on the third common project and will build it if necessary, so that I can go into either the appA or appB folder and type "mvn package" to build appA + common or appB + common, respectively. This is how I have it set up in Ant but there may not be an appropriate parallel in maven. Any help or nudges in the right direction is greatly appreciated :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

表情可笑 2024-09-17 16:12:54

(...) 但这不是我想要的,根 pom.xml 不合适,因为 appA 和 appB 是完全不相关的项目。

那么就不要这样做,如果不想的话就不要使用聚合,只需在 appA 的 POM 和appB。像这样的事情:

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>foo</groupId>
  <artifactId>appA</artifactId>
  <packaging>jar</packaging>
  <version>X-SNAPSHOT</version>
  ...
  <dependencies>
    <dependency>
      <groupId>bar</groupId><!-- at your discretion -->
      <artifactId>common</artifactId>
      <version>Y.Z-SNAPSHOT</version>
    </dependency>
    ...
  </dependencies>
  ...
</project>

依赖关系是通过本地存储库解决的,因此您必须首先 mvn install common 项目(并在进行任何更改以使它们“可见”之后)。

在开发过程中,常见的做法是在 IDE 中导入依赖项目,并将它们配置为依赖于工作空间内的项目(相对于二进制依赖项)。这将节省安装步骤以使更改可见。大多数 IDE 都支持这种方法。

(...) but this is not what I want, a root pom.xml is not appropriate since appA and appB are completely unrelated projects.

Then don't do it, don't use aggregation if you don't want to and just declare common as a dependency in the POM of appA and in the POM of appB. Something like this:

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>foo</groupId>
  <artifactId>appA</artifactId>
  <packaging>jar</packaging>
  <version>X-SNAPSHOT</version>
  ...
  <dependencies>
    <dependency>
      <groupId>bar</groupId><!-- at your discretion -->
      <artifactId>common</artifactId>
      <version>Y.Z-SNAPSHOT</version>
    </dependency>
    ...
  </dependencies>
  ...
</project>

Dependencies are resolved through the local repository so you'll have to mvn install the common project first (and after any change to make them "visible").

During development, a common practice is to import dependent projects in your IDE and to configure them to depend on project inside the workspace (vs binary dependencies). This saves the install step to make changes visible. Most IDEs support this approach.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文