Java Maven 无法解析同一个多模块项目中模块的依赖关系
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29712865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Maven cannot resolve dependency for module in same multi-module project
提问by Benjamin George Roberts
When running commands such as
运行命令时,例如
mvn dependency:build-classpath
or
或者
mvn exec:java
Maven is unable to resolve a dependency of one of my modules on another.
Maven 无法解决我的一个模块对另一个模块的依赖。
[ERROR] Failed to execute goal on project parser-app: Could not resolve dependencies for project project_group:A:jar:0.1-SNAPSHOT: Could not find artifact project_group:B:jar:0.1-SNAPSHOT
[错误] 无法在项目解析器应用程序上执行目标:无法解析项目 project_group:A:jar:0.1-SNAPSHOT: 找不到工件 project_group:B:jar:0.1-SNAPSHOT
The project structure is as follows:
项目结构如下:
/pom.xml
/A/pom.xml
/B/pom.xml
The parent pom is as follows:
父pom如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>project_group</groupId>
<artifactId>parent</artifactId>
<packaging>pom</packaging>
<version>0.1-SNAPSHOT</version>
<name>parent</name>
<modules>
<module>A</module>
<module>B</module>
</modules>
The first child module (the one failing to resolve the dependency):
第一个子模块(未能解决依赖的那个):
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>parent_group</groupId>
<artifactId>parent</artifactId>
<version>0.1-SNAPSHOT</version>
</parent>
<artifactId>A</artifactId>
<packaging>jar</packaging>
<name>A</name>
<dependencies>
<dependency>
<groupId>parent_group</groupId>
<artifactId>B</artifactId>
<version>0.1-SNAPSHOT</version>
</dependency>
The second child module (the dependency):
第二个子模块(依赖项):
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>parent_group</groupId>
<artifactId>parent</artifactId>
<version>0.1-SNAPSHOT</version>
</parent>
<artifactId>B</artifactId>
<packaging>jar</packaging>
<name>B</name>
采纳答案by Andrew McKee
Have you run mvn clean install
at least once on the project to install the dependencies within your local repository?
您是否mvn clean install
至少在项目上运行一次以在本地存储库中安装依赖项?
回答by tkruse
The Maven reactor is weird that way, it keeps modules around only for certain tasks. When running a build target that only does something for one subproject, then even if Maven builds dependencies first, it does not keep them around in the reactor (sometimes).
Maven 反应器在这方面很奇怪,它只为某些任务保留模块。当运行一个只为一个子项目做某事的构建目标时,即使 Maven 首先构建依赖项,它也不会将它们保留在反应器中(有时)。
Installing to the local repository is a workaround, but it is horrible and should be avoided when possible, because you can easily end up with outdated build results.
安装到本地存储库是一种解决方法,但它很糟糕,应该尽可能避免,因为您很容易得到过时的构建结果。
A slightly less ugly workaround is to combine two build targets, where the second build target does something harmless, but triggers addition to reactor in all subprojects.
一个稍微不那么丑陋的解决方法是组合两个构建目标,其中第二个构建目标做一些无害的事情,但会触发所有子项目中反应堆的添加。
As an example you can combine the task you want with the 'compile' or 'package' tasks.
例如,您可以将所需的任务与“编译”或“打包”任务结合起来。
Also see highest voted answer at Maven doesn't recognize sibling modules when running mvn dependency:tree
另请参阅Maven 在运行 mvn dependency:tree 时无法识别兄弟模块的最高投票答案
回答by Marv
This error might also be caused by Maven being in offline mode.
此错误也可能是由 Maven 处于离线模式引起的。
Sometimes I seem to accidentally enable offline mode in IntelliJ IDEA. To disable it, toggle the Toggle Offline Mode
toggle in the Maven Toolbar
有时我似乎不小心在 IntelliJ IDEA 中启用了离线模式。要禁用它,请Toggle Offline Mode
在 Maven 工具栏中切换开关
or uncheck the Work Offlinecheckbox in the settings under Build, Execution, Deployment > Build Tools > Maven
.
或取消选中 下设置中的脱机工作复选框Build, Execution, Deployment > Build Tools > Maven
。
回答by simon04
Configuring test-jarin the jar pluginresolved the issue for me:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Afterwards, running mvn clean install
works.
之后,运行mvn clean install
工作。