Maven-制作可以独立于其父代的子项目 [英] Maven - making child projects that can be independent of their parent

查看:199
本文介绍了Maven-制作可以独立于其父代的子项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点Maven新手,我试图建立一个可以构建多个子项目的maven项目,但仍然允许某人仅抓取其中一个子项目并在没有父项的情况下独立进行构建. /p>

I'm a bit of a Maven newb, and I'm trying to setup a maven project that builds several child projects, but still allows someone to just grab one of the child projects and build it independently without the parent.

parentfolder
  ->pom.xml
  ->project1
    ->pom.xml
    ->src
  ->project2
    ->pom.xml
    ->src
  ->project3
    ->pom.xml
    ->src

基本上,我希望某人能够检出parentfolder并进行mvn编译以构建所有项目,并且还希望某人能够仅检出project1并进行mvn编译以构建它.

Basically I want someone to be able to checkout parentfolder and do mvn compile to build all the projects, and also for someone to be able to checkout just project1 and do mvn compile to build just it.

我尝试将子项目声明为顶级pom.xml中的模块,

I've tried declaring the sub-projects as modules in the top-level pom.xml,

<modules>
  <module>project1</module>
  <module>project2</module>
  <module>project3</module>
</modules>

但是,这似乎需要在子级中声明父级pom.xml信息.这使得子项目依赖于存在的父pom.xml,这是我想要避免的.

But that seems to require that the parent pom.xml info is declared in the children. This makes the child projects dependent on the parent pom.xml being present, which is what I wanted to avoid.

推荐答案

您必须注意Maven2中父子关系和聚合概念之间的区别.即使实际上经常同时使用它们,它们也不是同一原理.

You have to take care of the differences between the parent-child relation and the aggregation concept in Maven2. They are not the same principle, even if they are really often used at the same time.

父母

第一个概念是项目在其pom.xml中声明一个父项:

The first concept is that a project declares in his pom.xml a parent:

<project>
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>foo</groupId>
        <artifactId>bar</artifactId>
        <version>42</version>
    </parent>
    ...

在这种情况下,为了构建此组件,必须在本地资源库中找到父项目.这是您的情况.

In this case, in order to build this component, the parent project must be found in the local repository. This is your case here.

Maven 2中父级概念的兴趣在于继承属性,依赖项,配置.在这里将放置子项目的所有常用信息.

The interest of the parent concept in Maven 2 is to inherit properties, dependencies, configuration. This is the place where you will put all the common information of the children projects.

这与Java语言中的extends完全相同.

This is the exact same concept of the extends in Java language.

AGGREGATION

在这种情况下,您有一个项目,通过在module节点中指定子模块的名称来聚合几个子模块:

In this case, you have a project that aggregates several sub modules by specifying their names in module nodes:

<modules>
    <module>commons</module>
    <module>client</module>
    <module>server</module>
    ...
</modules>

这意味着将在每个模块上执行将在此根项目上运行的每个命令(该顺序由Maven 2 Reactor定义).例如,如果在根项目上运行mvn clean install,则Maven 2将在根项目上运行该命令,然后在项目commons上运行,然后在client上运行,最后在server上运行.

This means that every command that you will run on this root project will be executed on each module (the order is defined by the Maven 2 Reactor). For example, if you run mvn clean install on the root project, Maven 2 will run this command on the root project, then on project commons, then on client and finally on server.

按照这种概念,您可以编译一个项目而无需编译其他任何项目(当然,除非存在相互依赖性).

In this concept, you are able to compile one project without compiling any other one project (except if there are inter-dependencies of course).

以下是显示两个不同概念的架构:

Here is a schema that shows the two different concepts:

您可以在《 Maven:权威指南》(

You have a more detailed explanations on these two concepts in the Maven : The Definitive Guide, here.

这篇关于Maven-制作可以独立于其父代的子项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆