简体   繁体   English

多模块Maven项目

[英]Multi-module Maven projects

I'm trying to get a Maven setup going. 我正在尝试进行Maven安装。 My project layout is simple: 我的项目布局很简单:

+
+--- libA
+--- libB
+--- projX
+--- projY

libA and libB (actually, about 5 of them) are shared code among the projects. libAlibB (实际上大约5个)在项目之间共享代码。 projX and projY (actually, about 10) are standalone applications with a Main each. projXprojY (实际上是大约10个)是独立的应用程序,每个应用程序都有一个Main

My requirements are simple: 我的要求很简单:

1) Be able to run projX and projY in Eclipse 1)能够在Eclipse中运行projX和projY
2) Compile standalone JARs (with dependencies) for projX and projY with single commands each 2)分别使用单个命令为projX和projY编译独立的JAR(具有依赖项)
3) DRY 3)干

I current have a working setup from my predecessor. 目前,我的前任有一个有效的设置。 It has a pom.xml file per library, which is fine, but two pom.xml files per project, which is not. 每个库都有一个pom.xml文件,这很好,但是每个项目有两个pom.xml文件,不是。 One of them does not even compile , but is needed by the other for compile assembly:single . 其中一个甚至不进行compile ,但是另一个需要进行compile assembly:single The two files are almost duplicates of eachother, and adding a new library to a project requires changes in 3-4 different places. 这两个文件几乎是彼此重复的,向项目添加新库需要在3-4个不同位置进行更改。

I have tried various things. 我尝试过各种东西。 Surprisingly, adding a super-parent pom.xml in the root was the easiest. 令人惊讶的是,在根中添加超级父pom.xml是最简单的。 That took care of project-wide settings such as UTF-8 source file encoding and whatnot. 这样就可以处理项目范围的设置,例如UTF-8源文件编码等等。 What did not work was everything else. 没有用的是其他所有东西。 Between POMs not being able to refer to eachother unless certain modifications are made, refusing to compile source folders when they are, insisting on a local repository which is ever-empty or requiring absolute paths otherwise, I was not able to meet the requirements I listed. 在POM之间无法进行相互引用,除非进行了某些修改,拒绝对它们进行编译,坚持使用永远为空的本地存储库或否则需要绝对路径,否则我无法满足我列出的要求。 This is despite reading the official documentation and scouring for examples online. 尽管阅读了官方文档并在网上搜寻示例。

Can anyone help? 有人可以帮忙吗?

You will need 3 pom.xml files one for parent project and one for each child project. 您将需要3个pom.xml文件,其中一个用于父项目,一个用于每个子项目。 You will have 3 separate projects. 您将有3个独立的项目。 In parent project you will have only dependencies for jars which are same for the child projects. 在父项目中,您将仅具有与子项目相同的jar依赖项。 for eg. 例如

Parent pom.xml 父pom.xml

<?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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.multimodule</groupId>
    <artifactId>multimodule</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <modules>
        <module>model</module>
        <module>presentation</module>
    </modules>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

child pom.xml 子pom.xml

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.multimodule</groupId>
        <artifactId>multimodule</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>presentation</artifactId>
    <packaging>war</packaging>
    <dependencies>

    <dependency>
        <groupId>com.multimodule</groupId>
        <artifactId>model</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>

another child pom.xml 另一个孩子pom.xml

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.multimodule</groupId>
    <artifactId>multimodule</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>model</artifactId>
</project>...

This sample is used for web application where you have separately model and presentation layer. 此样本用于Web应用程序,其中您分别具有模型层和表示层。 Multimodule project can be easily created via Eclipse or Intellij Idea. 可以通过Eclipse或Intellij Idea轻松创建多模块项目。

Normally, this shouldn't be too complicated with Maven. 通常,使用Maven不应太复杂。 The main idea is to put most of the redundant information in the parent (for the sake of simplicity we'll use only one parent) and then have the child modules reference the parent. 主要思想是将大多数冗余信息放入父级(为简单起见,我们仅使用一个父级),然后让子级模块引用父级。

This brings the advantage of having most of the "configurable" settings in one place and limit the amount of redundancy. 这带来了将大多数“可配置”设置集中在一处的优势,并限制了冗余量。

See this link for information regarding the different forms of inheritance in Maven 有关Maven中不同继承形式的信息,请参见此链接。

The structure of your projects should be something like this: 您的项目的结构应如下所示:

+
     pom.xml (root)
+--- libA
     +--- src
         +---main
             +---java
             +---resources
     pom.xml
+--- libB
     +--- src
         +---main
             +---java
             +---resources
     pom.xml
+--- projX
     +--- src
         +---main
             +---java
             +---resources
     pom.xml
+--- projY
     +--- src
         +---main
             +---java
             +---resources
     pom.xml

See this link regarding standard Maven layout 请参阅有关标准Maven布局的链接

You'll have 3 types of pom.xml (1 per project/library) 您将拥有3种pom.xml类型(每个项目/库1种)

  1. The parent root project (listing all the 'modules' and providing all inheritable settings like dependency versions, plugin versions and configuration, properties, etc...) 父根项目(列出所有“模块”并提供所有可继承的设置,例如依赖项版本,插件版本和配置,属性等)

<?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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.foo.bar</groupId>
    <artifactId>parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <properties>
        <compiler.source>1.6</compiler.source>
        <compiler.target>1.6</compiler.target>
        <source.encoding>UTF-8</source.encoding>
        <resource.encoding>UTF-8</resource.encoding>
    </properties>

    <modules>
        <module>libA</module>
        <module>projX</module>
    </modules>
    <dependencyManagement>
        <dependencies>
            <!-- Here put all the dependencies of all your project with groupId/artifactId/version-->
        </dependencies>
    </dependencyManagement>
    <build>
        <pluginManagement>
            <!-- Here put all your plugins with groupId/artifactId/version and configuration -->
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>${compiler.source}</source>
                        <target>${compiler.target}</target>
                        <encoding>${source.encoding}</encoding>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>2.5</version>
                    <configuration>
                        <encoding>${resource.encoding}</encoding>
                    </configuration>
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <version>2.5.3</version>
                    <configuration>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                    </configuration>
                    <executions>
                        <execution>
                            <id>build-resources</id>
                            <phase>package</phase>
                            <goals>
                                <goal>single</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>
  1. One pom.xml per library (libA, libB, etc...) 每个库(libA,libB等)一个pom.xml

<?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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.foo.bar</groupId>
        <artifactId>parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>libA</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <!-- Here list all the actual dependencies but only groupId and artifactId 
            (versions are all put in the parent) -->
        <dependency>
            <groupId>...</groupId>
            <artifactId>...</artifactId>
        </dependency>
    </dependencies>
</project>
  1. One pom.xml per project (projX, projY, etc...) and here we'll also add the maven-assembly to the build 每个项目一个pom.xml(projX,projY等),在这里我们还将把maven-assembly添加到构建中

<?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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.foo.bar</groupId>
        <artifactId>parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>projX</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <!-- Here list all the actual dependencies but only groupId and artifactId (versions are all put in the parent) -->
        <dependency>
            <groupId>com.foo.bar</groupId>
            <artifactId>libA</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin> 
                <groupId>org.apache.maven.plugins</groupId> 
                <artifactId>maven-assembly-plugin</artifactId> 
            </plugin> 
        </plugins>
    </build>
</project>

In this configuration, maven-assembly is attached to the phase "package" which means that by calling mvn package in your root project, you will have all jar's and jar-with-dependencies build in one go. 在此配置中,maven-assembly附加到阶段“ package”,这意味着通过在根项目中调用mvn package ,您将一次性构建所有jar和jar-with-dependencies。

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

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