使用 m2eclipse 设置多个项目的最佳实践

发布于 2024-09-11 15:12:10 字数 505 浏览 7 评论 0原文

示例场景:我有 2 个项目,“common-project”和“application-project”。 application-project依赖于common-project提供的API。这两个项目还使用了第 3 方 jar(例如番石榴)。

我正在尝试转换为使用 maven 和 m2eclipse,但不清楚最佳方法。目前,我的无 Maven 设置已将第 3 方 jar 添加为公共项目上的库,并标记为“已导出”。这样它们就被应用程序项目继承了,我不必在应用程序项目上显式地将它们添加为库。这两个项目都在积极开发中,所以我不希望先构建一个 common-project 的 jar,然后将其“安装”到我的本地存储库,然后才能使用 application-project 中的新功能。

此类项目布局的推荐方法是什么?我看到以下主题大致涉及该主题: Eclipse 中的项目构建了 Eclipse 中另一个项目使用的 jar

谢谢

Example scenario: I have 2 projects, "common-project" and "application-project". The application-project depends on the API provided by common-project. There are also 3rd party jars (example guava) used by both projects.

I am trying convert to using maven and m2eclipse, but am unclear on the best approach. Currently, my maven-free setup has the 3rd party jars added as libraries on common-project, and marked as "exported". This way they are inherited by application-project, and I don't have to explicitly add them as libraries on application project. Both projects are under active development, so I would prefer not to have to build a jar of common-project first, then "install" that to my local repository before I can use the new features in application-project.

What is the recommended approach for this type of project layout? I see the following thread touches roughly on the topic:
Project in Eclipse that builds a jar used by another project in Eclipse

Thanks

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

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

发布评论

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

评论(3

随风而去 2024-09-18 15:12:10

示例场景:我有 2 个项目,“common-project”和“application-project”。 application-project依赖于common-project提供的API。这两个项目还使用了第 3 方 jar(例如番石榴)。

我将创建 3 个 Maven 项目:一个 parent 聚合模块、一个 common-project 模块和一个 application-project 模块,具体取决于 common -project 并将 guava 声明为 parent 模块中的依赖项(以便子项目继承它)。类似这样的:

$ tree Q3337426
Q3337426
├── application-project
│   ├── pom.xml
│   └── src
│       ├── main
│           └── ...
│       └── test
│           └── ...
├── common-project
│   ├── pom.xml
│   └── src
│       ├── main
│           └── ...
│       └── test
│           └── ...
└── pom.xml

父 pom.xml 看起来像这样:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.stackoverflow.Q3337426</groupId>
  <artifactId>Q3337426</artifactId>
  <packaging>pom</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>Q3337426 - Root</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>com.google.guava</groupId>
      <artifactId>guava</artifactId>
      <version>r05</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <modules>
    <module>common-project</module>
    <module>application-project</module>
  </modules>
</project>

公共项目的 pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <artifactId>Q3337426</artifactId>
    <groupId>com.stackoverflow.Q3337426</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <artifactId>common-project</artifactId>
  <name>Q3337426 - Common Project</name>
  <dependencies/>
</project>

应用程序项目的 pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <artifactId>Q3337426</artifactId>
    <groupId>com.stackoverflow.Q3337426</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <artifactId>application-project</artifactId>
  <name>Q3337426 - Application Project</name>
  <dependencies>
    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>common-project</artifactId>
      <version>${project.version}</version>
    </dependency>
  </dependencies>
</project>

这是组织此类项目的 Maven 方式,并且允许触发reactor build 从根项目开始构建一切。

(...) 这两个项目都在积极开发中,所以我不希望先构建一个公共项目的 jar,然后将其“安装”到我的本地存储库,然后才能在应用程序中使用新功能-项目。

m2eclipse 插件可以解决 Workspace 项目的依赖关系(这实际上是默认行为)。因此,如果您同时导入 application-projectcommon-project,前者将配置为依赖于 common- 的项目(而不是依赖于jar)。使用此设置时,对 common-project 所做的更改将立即可见。

这应该可以解决您在 IDE 中的担忧。在 IDE 外部,在顶层项目上运行 Reactor 构建。

Example scenario: I have 2 projects, "common-project" and "application-project". The application-project depends on the API provided by common-project. There are also 3rd party jars (example guava) used by both projects.

I would create 3 maven projects: a parent aggregating module, a common-project module and an application-project module depending on common-project and declare guava as dependency in the parent module (so that child project will inherit it). Something like this:

$ tree Q3337426
Q3337426
├── application-project
│   ├── pom.xml
│   └── src
│       ├── main
│           └── ...
│       └── test
│           └── ...
├── common-project
│   ├── pom.xml
│   └── src
│       ├── main
│           └── ...
│       └── test
│           └── ...
└── pom.xml

Where the parent pom.xml looks like this:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.stackoverflow.Q3337426</groupId>
  <artifactId>Q3337426</artifactId>
  <packaging>pom</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>Q3337426 - Root</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>com.google.guava</groupId>
      <artifactId>guava</artifactId>
      <version>r05</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <modules>
    <module>common-project</module>
    <module>application-project</module>
  </modules>
</project>

The pom.xml for the common-project:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <artifactId>Q3337426</artifactId>
    <groupId>com.stackoverflow.Q3337426</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <artifactId>common-project</artifactId>
  <name>Q3337426 - Common Project</name>
  <dependencies/>
</project>

The pom.xml for the application-project:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <artifactId>Q3337426</artifactId>
    <groupId>com.stackoverflow.Q3337426</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <artifactId>application-project</artifactId>
  <name>Q3337426 - Application Project</name>
  <dependencies>
    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>common-project</artifactId>
      <version>${project.version}</version>
    </dependency>
  </dependencies>
</project>

This is the Maven way to organize such a project and will allow to trigger a reactor build from the root project to build everything.

(...) Both projects are under active development, so I would prefer not to have to build a jar of common-project first, then "install" that to my local repository before I can use the new features in application-project.

The m2eclipse plugin can Resolve dependencies from Workspace projects (this is actually the default behavior). So if you import both application-project and common-project, the former would be configured to depend on the sources of common-project (instead of depending on the jar). Changes made to common-project will be immediately visible when using this setup.

This should solve your concern inside the IDE. Outside the IDE, run a reactor build on the top project.

南街女流氓 2024-09-18 15:12:10

在 POM 的 元素中表达每个项目的依赖关系。

然后,m2eclipse 插件会自动从 pom.xml 中选取这些内容,并将它们作为库引用添加到 Eclipse 内的构建路径中。

如果 application-project 依赖于 common-project,那么它也会继承它的依赖关系 - 没有必要再次列出两者之间的公共依赖关系application-project 的 pom.xml。

Express the dependencies that each project has in the <dependencies> element of the POM.

The m2eclipse plugin will then automatically pick these up, from the pom.xml, and add them as library references to the build path within Eclipse.

If application-project depends on common-project, then it will inherit it's dependencies as well - it will not be necessary to list the common dependencies between the two a second time in the pom.xml of application-project.

花心好男孩 2024-09-18 15:12:10

假设你的电脑已经安装了maven,然后下载m2eclipse插件安装它并重新启动eclipse。

请记住,maven 构建的目标是生成可以分发和重用的工件,如果您不想在拥有“通用”的稳定版本之前进行 mvn install ,那也没关系。 project”API,虽然在开发过程中这样做并没有什么错。

对于您的情况,您可以执行以下操作之一:

  1. 如果 common-project API 是绝对必要的,并且通常不仅会在 application-project 的范围内使用然后将您的项目构建为多模块项目,并将 common-project 声明为您的应用程序项目 的模块。 以下是如何执行此操作的示例。
  2. 如果不是的话common-project 通常是一个共享工件,然后独立构建每个项目,并在其 pom.xml 文件中声明两者的依赖关系,eclipse 应该能够找出两者之间的依赖关系。
  3. 您可以首先开发 common-project 的部分实现,将其打包,然后使用 systemapplication-project 中声明为依赖项。 scope> 指定它在文件系统中的位置,这将告诉 Maven 依赖项始终存在,并且不会在任何存储库中查找它;尽管执行 mvn install 会比这更优雅,但您试图避免这种情况。

问候。

Assuming you already have maven installed in your computer, then download the m2eclipse pluging install it and restart eclipse.

Remember the goal of a maven build is to produce and artifact that can be distributed and reused, it's OK if you don't want to make a mvn install before having a somewhat stable version of you "common-project" API, although there is nothing wrong in doing it during development.

For your case you can do one of the following:

  1. If common-project API is absolutely necessary and will more often than not only be used in the confines of the application-project then build your project as a multi-module project and declare common-project to be a module of your application project. Here is an example of how to do this.
  2. If otherwise common-project will be more often than not a shared artifact, then build each project independently having declared the dependencies of both in their pom.xml files eclipse should be able to figure out the dependency between the two.
  3. You could develop a partial implementation of common-project first, package it, and then declaring as a dependency in application-project with the <scope>system<scope> specifying where in the file system it is located, this will tell maven that the dependency is always present and it would not look it up in any repository; although doing a mvn install will be more elegant than this you're trying to avoid that.

Regards.

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