我正在尝试设置一个多模块Maven项目,并且模块间依赖关系显然没有正确设置。
我有:
<modules>
<module>commons</module>
<module>storage</module>
</modules>
在父POM(有一个包装类型的pom)中,然后是子目录commons/
和storage/
,它们定义了具有相同名称的JAR poms。
存储取决于Commons。
在主(主)目录中,我运行mvn dependency:tree
并查看:
[INFO] Building system
[INFO] task-segment: [dependency:tree]
[INFO] ------------------------------------------------------------------------
[INFO] [dependency:tree {execution: default-cli}]
[INFO] domain:system:pom:1.0-SNAPSHOT
[INFO] \- junit:junit:jar:3.8.1:test
[INFO] ------------------------------------------------------------------------
[INFO] Building commons
[INFO] task-segment: [dependency:tree]
[INFO] ------------------------------------------------------------------------
[INFO] [dependency:tree {execution: default-cli}]
...correct tree...
[INFO] ------------------------------------------------------------------------
[INFO] Building storage
[INFO] task-segment: [dependency:tree]
[INFO] ------------------------------------------------------------------------
Downloading: http://my.repo/artifactory/repo/domain/commons/1.0-SNAPSHOT/commons-1.0-SNAPSHOT.jar
[INFO] Unable to find resource 'domain:commons:jar:1.0-SNAPSHOT' in repository my.repo (http://my.repo/artifactory/repo)
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Failed to resolve artifact.
Missing:
----------
1) domain:commons:jar:1.0-SNAPSHOT
为什么对“公共”的依赖性失败,即使反应堆显然已经看到它,因为它成功地处理了它的依赖树?绝对不应该去'网找到它,因为它就在那里......
存储用的pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<parent>
<artifactId>system</artifactId>
<groupId>domain</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>domain</groupId>
<artifactId>storage</artifactId>
<name>storage</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- module dependencies -->
<dependency>
<groupId>domain</groupId>
<artifactId>commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- other dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
谢谢你的任何建议!
(编辑)
为了澄清,我在这里寻找的是:我不想安装模块X来构建依赖于X的模块Y,因为两者都是从同一父POM引用的模块。这对我来说很直观,如果我在同一个源代码树中有两件事,我就不必安装中间产品来继续构建。希望我的想法在这里有所帮助......
我认为问题在于,当你指定依赖时,Maven希望将它作为jar(或其他)打包并至少从本地repo中获取。我敢肯定,如果你在公共项目上运行mvn install
,那么一切都会有效。
正如在this maven mailing list thread中所讨论的那样,依赖:树目标本身将在存储库而不是反应堆中查找。您可以通过mvn安装来解决这个问题,如前所述,或者做一些不那么繁琐的调用反应堆的事情,比如
mvn compile dependency:tree
适合我。
意识到这是一个较旧的线程,但似乎工具进化或第一次可能已经错过了。
可以通过执行reactor构建来执行构建,从而无需安装即可解析依赖项。
如果您在描述项目模块结构的父项中开始构建,那么模块之间的依赖关系将在构建过程中通过内部Maven反应器解析。
当然,这不是完美的解决方案,因为它无法解决结构中单个模块的构建问题。在这种情况下,Maven将不会在他的反应堆中具有依赖关系,并且希望在存储库中解决它。因此,对于单个构建,您仍然必须首先安装依赖项。
这里有一些描述这种情况的reference。
对我来说,是什么导致我这个线程是一个类似的问题,解决方案是确保所有模块依赖pom的
<packaging>pom</packaging>
父母有
POM
我的模型dep有pom - 所以没有找到jar。
唯一对我有用的东西:切换到gradle :(
我有
Parent
+---dep1
+---war1 (using dep1)
我可以在war1中使用cd并使用mvn tomcat7:run-war。我总是必须安装整个项目,尽管war1引用了他的父项,而父项引用了war1和dep1(作为模块),所以所有依赖项都应该是已知的。
我不明白问题是什么。
在像这样的Maven模块结构中:
- parent
- child1
- child2
你将在parent
pom
这:
<modules>
<module>child1</module>
<module>child2</module>
</modules>
如果您现在依靠child1
中的child2
,请将以下内容放入<dependencies>
的child2
中:
<dependency>
<groupId>example</groupId>
<artifactId>child1</artifactId>
</dependency>
您将收到一条错误,指出无法找到child1
的JAR。这可以通过在<dependencyManagement>
中为child1
声明包含pom
的parent
块来解决:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>example</groupId>
<artifactId>child1</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
child1
现在将在compile
上运行package
或parent
等目标时构建,child2
将找到child1
的编译文件。
确保在pom中解析失败的模块,通过在模块的pom文件中包含配置来指向正确的父模块。