如何使用 maven 和 eclipse 声明所有子模块的全局依赖

How to declare global dependency for all submodules with maven and eclipse

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

对我来说,很明显我会为所有这些项目编写单元测试。所以我认为这足以将依赖添加到 parent 的 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 时,完全没有错误。

我在 children 中引用 parent 模块如下:

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

请帮忙

您可以在父模块中配置依赖项,但您仍然必须在子模块中指定依赖项。 这允许您在父 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>

AJNeufeld:

"but you must still specify the dependencies in the child modules"

只有依赖在中才是正确的。只需尝试在父级 中设置 并且根本不要在子级中设置相同的依赖性。

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