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

发布于 2024-10-03 00:04:54 字数 2761 浏览 0 评论 0原文

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

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

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

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>

------ 和 (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>

如果您能给我一个如何更改的示例 ^^^ 我将非常感激!

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.

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?

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

The pom's look like this

(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):

 <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!!

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

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

发布评论

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

评论(2

自由如风 2024-10-10 00:04:54

原答案:
如果您尚未使用 M2Eclipse 插件,则可以执行此操作。安装完成后,从A的pom.xml中删除

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

并确保groupid、artifactid、version与B的pom.xml中定义的匹配

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

编辑:

如果 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>

在 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>

这将告诉 Maven 和 Eclipse 项目 A明确依赖于项目 B 的版本。使用 M2Eclipse,如果您的工作区中有匹配的 groupId、artifactId 和版本,并且启用了“依赖关系解析”,则项目 B 的内容将自动构建并包含到项目 A 中

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

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

<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

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

Edit:

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

<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:

<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.

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.

相思故 2024-10-10 00:04:54

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

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