maven 在 Eclipse 中构建依赖项,多个项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4197798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 15:21:22  来源:igfitidea点击:

maven build dependencies in Eclipse, multiple projects

javaeclipsemaven-2m2eclipse

提问by rybit

I have two projects in eclipse, both are maven controlled. A references B and they are on a flat system. What we currently have setup is that we have to a build on B - generate a jar on the system, and A references that jar.

我在eclipse中有两个项目,都是maven控制的。A 引用 B 并且它们在平面系统上。我们目前的设置是我们必须在 B 上构建 - 在系统上生成一个 jar,并且 A 引用该 jar。

I'd like to change this to be we just have to build A and it will go automatically build B? Can I do this in Maven/eclipse such that I don't have to create some higher project?

我想将其更改为我们只需要构建 A,它就会自动构建 B?我可以在 Maven/eclipse 中做到这一点,这样我就不必创建一些更高的项目吗?

I have looked into some of the maven docs - but they just really confuse me :). Thanks for your help.

我已经查看了一些 Maven 文档 - 但它们真的让我感到困惑:)。谢谢你的帮助。

The pom's look like this

pom看起来像这样

(B)

(乙)

    <project xmlns="..." xmlns:xsi="..."
 xsi:schemaLocation="...">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.thetus</groupId>
 <artifactId>irisMDK</artifactId>
 <packaging>jar</packaging>
 <name>irisMDK</name>
 <version>0.1</version>
 <url>...maven.apache...</url>
 <build>
  <sourceDirectory>java/src/main</sourceDirectory>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.1</version>
    <configuration>
     <source>1.6</source>
     <target>1.6</target>
     <testExcludes>
      <exclude>**/*.java</exclude>
     </testExcludes>
    </configuration>
   </plugin>
  </plugins>
  <resources>
   <resource>
    <directory>java/src/main</directory>
   </resource>
  </resources>
 </build>
 <dependencies>
  ...
 </dependencies>
</project>

------ And (A):

------ 和(A):

 <project xmlns="..." xmlns:xsi="..."
 xsi:schemaLocation="...">
 <modelVersion>4.0.0</modelVersion>
 <groupId>local.b</groupId>
 <artifactId>projectB</artifactId>
 <packaging>jar</packaging>
 <name>B</name>
 <version>RELEASE</version>
 <url>...</url>
 <repositories>
  <repository>
   <id>maven2-repository.dev.java.net</id>
   <name>Java.net Repository for Maven</name>
   <url>...download.java.net/maven...</url>
  </repository>
 </repositories>
 <build>
  <plugins>
                   ...
  </plugins>
  <sourceDirectory>java/src/main</sourceDirectory>
  <testSourceDirectory>java/src/test</testSourceDirectory>
  <resources>
  </resources>
 </build>
 <dependencies>
  <dependency>
   <groupId>local.b</groupId>
   <artifactId>projectB</artifactId>
   <scope>system</scope>
   <version>RELEASE</version>
   <systemPath>${basedir}/../B/target/B-0.1.jar</systemPath>
  </dependency>
 ...
 </dependencies>
 <reporting>
  ...
 </reporting>
</project>

If you could show me an example of how to change ^^^ I would be most grateful!!

如果您能向我展示如何更改的示例 ^^^ 我将不胜感激!!

回答by BuffaloBuffalo

Original Answer: The M2Eclipseplugin, if you are not using it already, allows you to do this. Once installed, remove

原始答案:M2Eclipse插件(如果您尚未使用它)允许您执行此操作。安装后,删除

<systemPath>${basedir}/../B/target/B-0.1.jar</systemPath>

from A's pom.xml and make sure the groupid, artifactid, version match up with what is defined in B's pom.xml

从 A 的 pom.xml 并确保 groupid、artifactid、version 与 B 的 pom.xml 中定义的内容相匹配

Then right-click on the project, Maven-> Enable Dependency Resolution. The build should now look at B's local project

然后右键项目,Maven->启用依赖解析。构建现在应该查看 B 的本地项目

Edit:

编辑:

If B's pom.xml looks like this (from your example):

如果 B 的 pom.xml 看起来像这样(来自您的示例):

<project xmlns="..." xmlns:xsi="..."
      xsi:schemaLocation="...">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.thetus</groupId>
      <artifactId>irisMDK</artifactId>
      <packaging>jar</packaging>
      <name>irisMDK</name>
      <version>0.1</version>

      ...
 </project>

In A's pom (which depends on Project B) your dependency should look like:

在 A 的 pom(取决于项目 B)中,您的依赖项应如下所示:

<project>
...
    <dependencies>
      <!--Attributes of project that this project is dependent upon, as defined in that projects POM -->
      <dependency>
           <groupId>com.thetus</groupId>
           <artifactId>irisMDK</artifactId>
           <version>0.1</version>
      </dependency>
 ...
     </dependencies>
....
</project>

This will tell maven and eclipse that project A explicitly depends on that version of Project B. With M2Eclipse, if you have a matching groupId, artifactId, and version in your workspace and you have "Dependency Resolution" enabled, Project B's contents will automatically be built and included into Project A.

这将告诉 maven 和 eclipse 项目 A 明确依赖于项目 B 的那个版本。使用 M2Eclipse,如果您的工作区中有匹配的 groupId、artifactId 和版本,并且您启用了“依赖解决方案”,则项目 B 的内容将自动成为构建并包含在项目 A 中。

Also, opening the Maven console in eclipse (console view->new console dropdown->new maven console) could help in debugging why project B isn't be picked up by Project A.

此外,在 Eclipse 中打开 Maven 控制台(控制台视图->新控制台下拉菜单->新 Maven 控制台)可以帮助调试为什么项目 B 没有被项目 A 选中。

回答by Jon Freedman

Sounds like you want to use a multi-module project, hereis a simple tutorial. You would create a parent POM, and have both A and B as children, with A keeping its dependency on B.

听起来您想使用多模块项目,这里有一个简单的教程。您将创建一个父 POM,并将 A 和 B 作为子代,而 A 保持对 B 的依赖。