英文:
I can't use inherited dependency in my child modules using maven
问题
我有以下的项目结构:
我希望在模块 B 和 C 中使用相同的依赖项(让我们称之为:**dep-for-B-and-C**
)。
A
├── B
│ └── pom.xml
├── C
│ └── pom.xml
│
└── pom.xml
根目录 'A' 的 pom.xml:
...
<groupId>my-group-id</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>B</module>
<module>C</module>
</modules>
...
<dependencyManagement>
<dependencies>
<dependency>
<groupId>***</groupId>
<artifactId>dep-for-B-and-C</artifactId>
<version>***</version>
</dependency>
</dependencies>
</dependencyManagement>
'B' 和 'C' 子模块的 pom.xml:
...
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>***</groupId>
<artifactId>dep-for-B-and-C</artifactId>
</dependency>
...
如果我将 dep-for-B-and-C
作为 A 和 B 的依赖项,然后运行 mvn clean install
,我会得到以下错误:
[ERROR] 'dependencies.dependency.version' for ***:dep-for-B-and-C-jar:jar is missing. @ line 39, column 21
如果我不将该依赖项放入子模块的 pom.xml 中(我正在使用 dependencyManagement,因此不需要指定版本(这是我的理解)),那么我会遇到编译错误,因为我无法使用我的依赖项。
请注意,对于 B 和 C,我将 spring-boot-starter-parent 作为父级,因为项目 B 和 C 将是 Spring Boot 项目。我之所以不将其放在根目录(A pom.xml)中,是因为可能会有另一个不是 Java 项目的模块。
这可能是原因吗?
如果是的话,那么我应该创建类似这样的项目结构吗?
A
├───Backend
│ ├── B
│ │ └── pom.xml
│ ├── C
│ │ └── pom.xml
│ │
│ └── pom.xml(以spring-boot-starter作为父级)
│
├───Frontend(使用eirslett mvn插件)
│
└──pom.xml
英文:
I have the following project structure:
I want to use the same dependency (let's call it: **dep-for-B-and-C**
) in modules B and C.
A
├── B
│ └── pom.xml
├── C
│ └── pom.xml
│
└── pom.xml
root 'A' pom.xml:
...
<groupId>my-group-id</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>B</module>
<module>C</module>
</modules>
...
<dependencyManagement>
<dependencies>
<dependency>
<groupId>***</groupId>
<artifactId>dep-for-B-and-C</artifactId>
<version>***</version>
</dependency>
</dependencies>
</dependencyManagement>
'B' and 'C' child modules pom.xml:
...
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>***</groupId>
<artifactId>dep-for-B-and-C</artifactId>
</dependency>
...
If I put dep-for-B-and-C
as a dependency of A and B then after I run mvn clean install
, I got the following error:
[ERROR] 'dependencies.dependency.version' for ***:dep-for-B-and-C-jar:jar is missing. @ line 39, column 21
If I don't put the dependency (I'm using dependencyManagement so it should place but I don't need to specify version (this is my understanding)) into my child pom.xml then, I have a compile error because I can't use my dependency.
Please note that I have spring-boot-starter-parent as a parent for B and C, because project B and C will be a Spring boot project. The reason why I don't put this in the root (A pom.xml) is that maybe I will have another module that won't be Java project.
This can be the reason?
If yes, then should I create a project structure like this?
A
├───Backend
│ ├── B
│ │ └── pom.xml
│ ├── C
│ │ └── pom.xml
│ │
│ └── pom.xml (spring-boot-starter as parent)
│
├───Frontend (using: eirslett mvn plugin)
│
└──pom.xml
答案1
得分: 3
以下是翻译好的内容:
这是问题所在:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- 从存储库中查找父级 -->
</parent>
在模块 B 和 C 中。请将模块 A 添加为 B 和 C 的父级。
英文:
Here is the problem:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
in modules B and C. Please add module A as parent of B and C
答案2
得分: 2
如果您不将A用作父POM,则无法使用A的dependencyManagement。
英文:
If you don't use A as parent POM, then cannot use dependencyManagement from A.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论