【问题标题】:How to declare global dependency for all submodules with maven and eclipse如何使用maven和eclipse声明所有子模块的全局依赖
【发布时间】:2017-03-31 06:25:00
【问题描述】:

我有如下多模块结构化 maven 项目:

  • 父级
    • 服务器
    • 共享
    • 客户

对我来说,很明显我会为所有这些项目编写单元测试。所以我认为这足以将依赖项添加到父的 pom.xml 如下:

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>

然后在每个子模块中使用它,f.e.:

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

但我在 Eclipse 中遇到错误,例如:The import org.junit cannot be resolved

当我做 Maven-> 更新项目时,或者mvn clean install,完全没有错误。

我在子模块中引用父模块如下:

<parent>
    <groupId>pl.daniel.erp</groupId>
    <artifactId>erp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>

请帮忙

【问题讨论】:

  • 你在子 poms 中声明了父级吗?
  • 我已经更新了检查后。谢谢
  • 嗯。我会通过:stackoverflow.com/questions/3211643/…,如果这不是问题,请尝试从 Eclipse 进行完全干净的构建。可能是一个愚蠢的配置问题或 Eclipse 出现故障并需要彻底清理。
  • 你在父模块pom中声明子模块依赖了吗?
  • 你的意思是“”?是的。

标签: java eclipse maven multi-module


【解决方案1】:

AJNeufeld:

"但您仍必须在子模块中指定依赖项"

仅当依赖项在 中时才正确。只需尝试在父 中设置 并且根本不要在子中设置相同的依赖项。

父 POM:

<project>
 ...
 <dependencies>
   ...
   <dependency>
     <groupId>junit</groupId>
     <artifactId>junit</artifactId>
     <version>4.12</version>
     <scope>test</scope>
   </dependency>
   ...
 <dependencies>
 ...
</project>

子 POM:

<project>
  ...
  <!-- no junit dependency defined at all -->
  ...
</project>

【讨论】:

  • 那么&lt;dependencyManagement&gt;&lt;dependencies&gt;标签有什么区别呢?为什么两者都存在?
  • 是一种“抽象”声明(与抽象类的想法相同:)在父项目树的任何地方,它可以声明通用设置(即版本、范围等),但将添加依赖项仅当它在 中定义时才进入类路径。在dependencyManagement中,实际的依赖只需要groupId/artifactId。所以分层设置有很大的空间...... 中的也是正确的。使用插件会更有用,因为它们可能有大量的特定配置。
  • 所以... "grand-parent" 可以拥有具有通用设置的 ,然后其子级可能具有具有特定设置的实际 并且它们的所有“子级”不需要定义插件根本......类似的东西。实际上,实际上我有 100 多个这样的项目。并且所有依赖版本和插件都只在一个地方定义。通过版本升级很容易管理。
【解决方案2】:

您可以在父模块中配置依赖项,但仍必须在子模块中指定依赖项。 这允许您在父 POM 中指定版本一次,并让所有子模块使用该版本。 但是,每个子模块仍必须列出其依赖项。

父 POM:

<project>
   ...
   <dependencyManagement>
       <dependencies>
           <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>4.12</version>
              <scope>test</scope>
          </dependency>
         ...
       <dependencies>
   </dependencyManagement>
   ...
</project>

子 POM:

<project>
   ...
   <dependencies>
       <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
       </dependency>
      ...
   </dependencies>
   ...
</project>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-21
    • 1970-01-01
    • 2010-10-22
    • 1970-01-01
    • 1970-01-01
    • 2012-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多