【问题标题】:Maven module doesn't see dependable submoduleMaven 模块没有看到可靠的子模块
【发布时间】:2018-07-09 11:43:49
【问题描述】:

我有以下项目结构:

Root:  
   module_1   
   module_2  
   shared

module_1 依赖于共享模块。

因此 Root pom.xml 看起来像这样:

<groupId>root</groupId>
<artifactId>root</artifactId>
<version>0.0.1</version>
<packaging>pom</packaging>
<name>root</name>
<modules>
    <module>module_1</module>
    <module>module_2</module>
    <module>shared</module>
</modules>

module_1 pom.xml:

<parent>
    <groupId>root</groupId>
    <artifactId>root</artifactId>
    <version>0.0.1</version>
</parent>
...
<dependencies>
    <dependency>
        <groupId>shared</groupId>
        <artifactId>shared</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <scope>compile</scope>
        <type>pom</type>
    </dependency>
</dependencies>

共享 pom.xml

<parent>
    <groupId>root</groupId>
    <artifactId>root</artifactId>
    <version>0.0.1</version>
</parent>
<artifactId>shared</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>shared</name>
<description>shared</description>

但是当我尝试构建根项目时,maven 输出编译错误,module_1 看不到来自共享模块的类:

[ERROR] Some_class_from module_1 cannot find symbol 
[ERROR] symbol:   Some_class_from_shared_module

我该如何解决这个问题?

【问题讨论】:

  • 共享 pom.xml 中的打包应该是 jar(而不是 pom)。
  • 首先将共享项目做成一个jar包而不是pom...否则依赖它没有意义...

标签: java maven dependencies multi-module


【解决方案1】:

module1shared 的依赖是pom 类型...

<dependency>
    <groupId>shared</groupId>
    <artifactId>shared</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <scope>compile</scope>
    <type>pom</type>
</dependency>

这意味着module1 将继承shared 的依赖关系,但它无法访问shared 中的任何类。

通常&lt;type&gt;pom&lt;/type&gt;&lt;scope&gt;import&lt;/scope&gt; 相关联,因为这会将shared 的任何依赖项包含在module1 中。在您的情况下,您需要更多 shared 的传递性提供的依赖项,您也需要 shared 的类,所以删除...

<type>pom</type>

...这将导致依赖项使用默认类型(jar),然后允许module1 依赖于(a)shared 的依赖项和(b)shared 的类。

【讨论】:

  • 它正在工作,但我想确保在根项目构建期间共享模块也将被重建。这种方法是否支持这一点?
  • 您是说上面的答案解决了您的问题吗?现在您想问另一个问题:“我如何确保在构建 module1 时构建 shared 模块”?
  • module1sharedroot 的子模块,因此无论何时构建root Maven 都会确定需要构建哪些子模块并将构建它们。但是,如果您选择仅构建 module1,那么 Maven 将不会构建 shared。因此,如果您的意图是确保在构建 module1 时构建 shared 模块,那么只需构建 root 并让 Maven 为您处理。
猜你喜欢
  • 2016-02-21
  • 2021-10-26
  • 1970-01-01
  • 2018-08-31
  • 1970-01-01
  • 1970-01-01
  • 2017-11-26
  • 2018-07-24
  • 1970-01-01
相关资源
最近更新 更多