Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用maven和eclipse声明所有子模块的全局依赖关系_Java_Eclipse_Maven_Multi Module - Fatal编程技术网

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

Java 如何使用maven和eclipse声明所有子模块的全局依赖关系,java,eclipse,maven,multi-module,Java,Eclipse,Maven,Multi Module,我有以下多模块结构化maven项目: <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <parent> <grou

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

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>
<parent>
    <groupId>pl.daniel.erp</groupId>
    <artifactId>erp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>
  • 母公司
    • 服务器
    • 共享
    • 客户
对我来说,很明显我会为所有这些项目编写单元测试。因此,我认为将依赖项添加到父级的pom.xml中就足够了,如下所示:

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>
<parent>
    <groupId>pl.daniel.erp</groupId>
    <artifactId>erp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>
但是我在Eclipse中遇到了一些错误,比如:
import org.junit无法解析

当我执行Maven->updateproject或
mvn clean install
时,根本没有错误

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

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>
<parent>
    <groupId>pl.daniel.erp</groupId>
    <artifactId>erp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>

pl.daniel.erp
企业资源计划
0.0.1-快照

请提供帮助

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

父POM:

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

...
朱尼特
朱尼特
4.12
测试
...
...
儿童POM:

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

...
朱尼特
朱尼特
...
...

AJNeufeld:

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

只有在依赖项处于中时,它才是正确的。只需尝试在父级中设置,而不要在子级中设置相同的依赖项

父POM:

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

...
...
朱尼特
朱尼特
4.12
测试
...
...
儿童POM:

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

...
...

您是否在子POM中声明了父项?我已经更新了post check。嗯。我会经历:如果这不是一个问题,请尝试从Eclipse进行完全干净的构建。可能是一个愚蠢的配置问题或Eclipse出现问题,需要彻底清理。您是否在父模块pom中声明了子模块依赖项?您的意思是“”?是的。那么
标记之间的区别是什么?为什么两者都存在?是一种“抽象”声明(与抽象类的想法相同:),它可以在父项目树中的任何位置声明公共设置(即版本、范围等),但依赖项只有在中定义时才会添加到类路径中。当它处于DependencyManager中时,实际的依赖项只需要有groupId/artifactId。所以分层设置有很大的空间。。。这同样适用于和。对于插件来说,它更有用,因为它们可能有大型的特定配置。。。“祖父母”可以有公共设置,然后它的孩子可能有实际的特定设置,他们的所有“孩子”根本不需要定义插件。。。差不多吧。实际上,我有100多个这样的项目。所有依赖版本和插件都只在一个地方定义。通过版本升级很容易管理。