java 让 Maven 项目构建自己的依赖项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4330222/
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
Having a maven project build its own dependencies?
提问by oym
With maven is it possible to have a top-level project who's packaging type is "war" which will build itself and all of its dependent modules (packaged as jar) and have the build generate a project.war file?
使用 maven 是否有可能拥有一个打包类型为“war”的顶级项目,它将构建自身及其所有依赖模块(打包为 jar)并让构建生成一个 project.war 文件?
Much of the documentation examples and other examples I've seen often use a top-level project with packaging type of "pom" and the project only serves the purpose of tying the modules together. Can I avoid this?
我见过的许多文档示例和其他示例经常使用打包类型为“pom”的顶级项目,并且该项目仅用于将模块绑定在一起。我可以避免这种情况吗?
So basically I need something which is effectively like declaring a <module>my-module</module>
for maven to build, and in that same POM, declaring a <dependency>...my-module's artifact...</dependency>
on that same module which needs to be built. Maybe a plugin as someone already suggested?
所以基本上我需要一些有效的东西,比如声明一个<module>my-module</module>
for Maven 来构建,并且在同一个 POM 中,<dependency>...my-module's artifact...</dependency>
在需要构建的同一个模块上声明一个。也许是有人已经建议的插件?
Update: In other words (to simplify the problem): If I have project A
and project B
, where project A
depends on project B
- is there a way for me to execute a build on project A
and also have it automatically build project B
(and include project B
as its dependency - creating projectA.war which contains projectB.jar)?
更新:换句话说(为了简化问题):如果我有project A
and project B
,project A
取决于哪里project B
- 有没有办法让我执行构建project A
并让它自动构建project B
(并包括project B
作为它的依赖项 - 创建 projectA.war 其中包含 projectB.jar)?
采纳答案by super_aardvark
That's not really what a top-level project is for. Your WAR project has dependencies, which are the artifacts (e.g. jars) that will be included in the WAR (in WEB-INF/lib) when you run 'mvn package'. Your WAR project pom can have the top-level project as its parent, but it shouldn't be the parent of its dependencies. You may want to have that top-level project be the parent of both the WAR project and of the JAR projects that are dependencies in the WAR.
这并不是顶级项目的真正用途。您的 WAR 项目具有依赖项,即当您运行“mvn package”时将包含在 WAR(在 WEB-INF/lib 中)中的工件(例如 jar)。您的 WAR 项目 pom 可以将顶级项目作为其父项,但它不应是其依赖项的父项。您可能希望该顶级项目成为 WAR 项目和作为 WAR 依赖项的 JAR 项目的父项目。
回答by Jigar Joshi
super_aardvark suggested correct way but,
For requirement I would suggest following structure It is suitable and goodstructure also :
super_aardvark 建议了正确的方法,但是,
对于要求,我建议使用以下结构它也是合适且良好的结构:
Consedering ProjectA
as project-webapp
, ProjectB
as project-core
ConsederingProjectA
的project-webapp
,ProjectB
如project-core
You can have following structure :
您可以具有以下结构:
Your Grand Project :
你的大项目:
<?xml version="1.0" encoding="UTF-8"?>
<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>com.mycompany.project</groupId>
<artifactId>project</artifactId>
<version>2.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Project Repository System</name>
<description>Project Repository System R2</description>
<modules>
<module>project-core</module>
<module>project-webapp</module>
</modules>
</project>
Your WebApp Project:
您的 WebApp 项目:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<groupId>com.mycompany.project</groupId>
<artifactId>project</artifactId>
<version>2.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>project-webapp</artifactId>
<version>2.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>Project Web Application</name>
<description>Project Repository</description>
<dependency>
<groupId>com.mycompany.project</groupId>
<artifactId>project-core</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
</project>
Your Core Project:
您的核心项目:
<project>
<parent>
<groupId>com.mycompany.project</groupId>
<artifactId>project</artifactId>
<version>2.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>project-core</artifactId>
<version>2.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Project Core</name>
<description>ProjectCore</description>
</project>
Your Directory structureshould look like:
您的目录结构应如下所示:
-------Grand Parent.pom
|
|--------project-webapp
| |
| project-webapp.pom
|
| -------project-core.pom
|
project-core.pom
From parent pom execute mvn clean install
it will build both the web-app and core project
从父 pom 执行mvn clean install
,它将构建 web-app 和核心项目
回答by rustyx
This is not possible in Maven 1, 2 or 3.
这在 Maven 1、2 或 3 中是不可能的。
I'd recommend to give up this idea, because Maven's whole purpose is to enforce standardized development process. Don't fight the structure, just create a parent POM module and make the WAR module and other dependencies underneath it.
我建议放弃这个想法,因为 Maven 的全部目的是强制执行标准化的开发过程。不要与结构作斗争,只需创建一个父 POM 模块并在其下创建 WAR 模块和其他依赖项。
回答by Russ Hymanson
When you have a multi-module project and you're doing work in several modules simultaneously it can be tedious and error-prone to make sure all the necessary dependencies are updated.
当您有一个多模块项目并且同时在多个模块中工作时,确保更新所有必要的依赖项可能会很乏味且容易出错。
In my situation, I would like my build system to detect changes and only build the modules that are necessary. One way this might be possible with maven is for someone to write a custom plugin that does this, which doesn't seem insurmountable given there are already complex plugins available, like the maven release plugin.
在我的情况下,我希望我的构建系统能够检测更改并仅构建必要的模块。使用 maven 可能实现的一种方法是让某人编写一个自定义插件来执行此操作,鉴于已经有复杂的插件可用,例如 maven 发布插件,这似乎并非不可逾越。
Others have already mentioned the aggregation pom concept, which is repeatable and does produce the necessary artifacts. But sometimes you end up building more than you really need to.
其他人已经提到了聚合 pom 概念,它是可重复的并且确实会产生必要的工件。但有时你最终构建的比你真正需要的更多。
Maven profiles can help and here's a good article in that regard:
Maven 配置文件可以提供帮助,这里有一篇关于这方面的好文章:
Using Aggregate and Parent POMs
Also note in the article the concept of the batch pom, which I was not previously aware of.
还要注意文章中的批处理 pom 的概念,这是我以前不知道的。
Remember, mvn clean install will push your artifact into your local repo. So if module A depends on module B, as long as your local repo has the latest build of module B then you should be all set. So, if there were an external tool that was watching for changes to module B and automatically built it when there were and pushed those changes into the local repo then when module A was rebuilt it would pick up those changes. There are continuous integration (CI) tools that can do this, like Jenkins. But you would need a local install to have this work directly with your local repo. It's still an option, though.
请记住, mvn clean install 会将您的工件推送到您的本地存储库中。因此,如果模块 A 依赖于模块 B,只要您的本地存储库具有模块 B 的最新版本,那么您就应该一切就绪。因此,如果有一个外部工具正在监视模块 B 的更改并在发生更改时自动构建它并将这些更改推送到本地存储库中,那么当模块 A 被重建时,它会接收这些更改。有持续集成 (CI) 工具可以做到这一点,例如 Jenkins。但是您需要本地安装才能直接与本地存储库一起使用。不过,这仍然是一种选择。
Another option would be for the CI environment to push your builds to an external maven repo (or even one you setup locally with something like Nexus). Then you setup your CI builds to pull from that location as well.
另一种选择是让 CI 环境将您的构建推送到外部 maven 存储库(甚至是您使用 Nexus 之类的东西在本地设置的存储库)。然后,您也可以设置 CI 构建以从该位置拉取。
So, there are solutions that rely on other tools or potential plugins to do what you want - just depends how much time and effort you want to invest to get it all setup. But, once you get over that hurdle you'll have a system (and knowledge and experience) that you can use on all your projects, not to mention you'll be familiar with how many development shops/teams work.
因此,有些解决方案依赖于其他工具或潜在插件来执行您想要的操作——这取决于您想要投入多少时间和精力来完成所有设置。但是,一旦你克服了这个障碍,你就会拥有一个可以在所有项目中使用的系统(以及知识和经验),更不用说你会熟悉有多少开发商店/团队在工作。
I would recommend researching continuous integration and continuous delivery for more information and ideas.
我建议研究持续集成和持续交付以获取更多信息和想法。
回答by gigadot
In parent pom, you have to define a sequential order of modules to be compiled. You can add a war packing module to the last in that list. It will simply aggregate all previous compiled code together.
在父 pom 中,您必须定义要编译的模块的顺序。您可以将War打包模块添加到该列表的最后一个。它将简单地将所有先前编译的代码聚合在一起。
回答by Cristian Vrabie
NetBeans has an option that allows you to do exactly this with Maven projects but I don't know any pure Maven solutions. I think that the task is more suited for an IDE, because it knows for what depended projects you have the code (based of what projects you have opened in the workspace). How would Maven itself differentiate between a dependency that you want to build and one that needs to be fetched from the repository. And for those that need to be built, where should it look for the source code?
NetBeans 有一个选项可以让您对 Maven 项目完全做到这一点,但我不知道任何纯 Maven 解决方案。我认为该任务更适合 IDE,因为它知道您拥有哪些依赖项目的代码(基于您在工作区中打开的项目)。Maven 本身如何区分您要构建的依赖项和需要从存储库中获取的依赖项。而对于那些需要构建的,它应该在哪里寻找源代码?
Anyway, another solution to the problem, that I used successfully a few times, is to create a simple shell script that navigates to your projects folders and starts the build then it waits for it to finish then proceeds to the next project and so on.
无论如何,我成功使用过几次的问题的另一个解决方案是创建一个简单的 shell 脚本,该脚本导航到您的项目文件夹并开始构建,然后等待它完成,然后继续下一个项目,依此类推。
回答by Gwyn Evans
Not really - (Well, I can think of a couple of ways, but I'd not use them as they're convoluted and go against the basic ethos/practices of Maven).
不是真的 - (嗯,我可以想到几种方法,但我不会使用它们,因为它们很复杂并且违背了 Maven 的基本精神/实践)。
Don't forget that the other purpose of the top-level pom is to provide a single point to set common details such the particular versions of dependencies used in the modules of the project.
不要忘记顶级 pom 的另一个目的是提供一个点来设置公共细节,例如项目模块中使用的依赖项的特定版本。