我有一个包含多个子模块的 Maven 项目。
我有一个根 pom 文件,其中有 jackson 依赖项
<dependency>
<groupId>fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
我在一个名为'api'的模块中具有jackson依赖项。
<dependency>
<groupId>fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<scope>provided</scope>
</dependency>
我有一个名为'client'的新模块,我在其中导入'api'的maven依赖项
<dependency>
<groupId>abc.com</group>
<artifactId>api</artifactId>
<version>0.1.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
我使用以下命令进行编译。
mvn -Djackson.version=2.4.4 compile
如果我尝试将 jackson 库导入客户端模块,则会收到编译错误,除非我再次显式地将 jackson 依赖项添加到客户端强>模块
为什么添加 api 依赖项还不够,因为它已经包含 jackson 依赖项
最佳答案
您将 api
中的 jackson
依赖项定义为 provided
,因此它不具有传递性:
provided
This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.
关于java - 从另一个模块导入时找不到maven依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37651503/