简体   繁体   English

为什么Maven不在多模块项目中安装依赖项?

[英]Why Maven doesn't install dependencies in multimodule project?

My project structure (multi module) is like this 我的项目结构(多模块)是这样的

parent
    projectA
    projectB
    ... other modules

parent also actually has a parent (Spring Boot). parent实际上也有一个父(Spring Boot)。

I have set up a Jenkins jobs to compile & test on every commit, so it runs: 我已经设置了一个Jenkins作业来编译和测试每次提交,所以它运行:

mvn -f pom.xml clean install

And that all works fine. 一切正常。 ProjectB depends on ProjectA (which is like a common classes type of project) and is a Spring boot application. ProjectB依赖于ProjectA (它类似于一个常见的类项目)并且是一个Spring启动应用程序。 So the dependency information is the regular: 所以依赖信息是常规的:

<dependency>
        <groupId>Group</groupId>
        <artifactId>ProjectA</artifactId>
        <version>1.0-SNAPSHOT</version>
</dependency>

ProjectB has a separate job in Jenkins to build the deployable jar file and deploy it to server. ProjectB在Jenkins中有一个单独的工作来构建可部署的jar文件并将其部署到服务器。 So the command there is: 那么命令就是:

mvn -f ProjectB/pom.xml clean install antrun:run

This fails with a message like: 这会失败并显示如下消息:

[WARNING] The POM for Group:ProjectB:1.0-SNAPSHOT is missing, no dependency information available
...
[ERROR] Failed to execute goal on project host-api: Could not resolve dependencies for project Group:ProjectB:1.0-SNAPSHOT: The following artifacts could not be resolved: Group:ProjectA:jar:1.0-SNAPSHOT...

Now I can resolve this by doing a mvn install in the ProjectA directory - I've tested this and it does resolve the issue. 现在我可以通过在ProjectA目录中执行mvn install来解决这个问题 - 我已经对此进行了测试,它确实解决了这个问题。

But my question is why should I have to ? 但我的问题是我为什么要这样做 Shouldn't Maven figure out it should be installing the jar in the local repo? 不应该Maven弄清楚应该在本地仓库中安装jar吗?

Thanks in advance 提前致谢

TL;DR Tell maven about the structure of your project. TL; DR告诉maven关于项目的结构。

When you run the command 运行命令时

mvn -f pom.xml clean install

Then maven uses the reactor to work out the order of the modules, something like the following is output: 然后maven使用reactor来计算模块的顺序,输出如下:

[INFO] Reactor build order: [INFO] Reactor构建顺序:
[INFO] ProjectA [INFO] ProjectA
[INFO] ProjectB [INFO] ProjectB

So Maven first builds project A, then builds project B. 所以Maven首先构建项目A,然后构建项目B.

When you run the command: 运行命令时:

mvn -f ProjectB/pom.xml clean install antrun:run

Then you have a problem; 那你有问题; maven isn't starting from the parent - it's starting from a child. maven不是从父母那里开始的 - 它是从一个孩子开始的。 It's not told about the hierarchy of projects needed to be built first. 没有告诉我们需要首先建立项目的层次结构。

If you want to build a single project from a maven multimodule project, along with dependencies you should use: 如果要从maven多模块项目构建单个项目,还应使用依赖项:

mvn -f pom.xml -pl ProjectB -am install antrun:run

Where: 哪里:

  • -pl ProjectB is the "project list" option, it tells maven to build these specific projects -pl ProjectB是“项目列表”选项,它告诉maven构建这些特定项目
  • -am is the "also make" option, it tells maven to build any projects that the projects in pl are dependant on -am是“也是make”选项,它告诉maven构建pl中的项目所依赖的任何项目

Specify the dependencies build part when you run the build: 运行构建时指定依赖项构建部件:

Guide to Working with Multiple Modules 使用多个模块的指南

So I suppose the rule is, always build the parent project first and then run goals on subprojects afterwards. 所以我认为规则是,首先建立父项目,然后在子项目上运行目标。

The fix for me was to run clean install on the parent project first and then have a second build configuration in Jenkins that ran -f ProjectB/pom.xml antrun:run 我的修复方法是首先在父项目上运行clean install ,然后在Jenkins中运行第二个构建配置-f ProjectB/pom.xml antrun:run

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM