多模块 Maven 项目

Multi-module Maven projects

我正在尝试启动 Maven 设置。我的项目布局很简单:

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

libAlibB (实际上,大约有 5 个)是项目之间的共享代码。 projXprojY(实际上,大约 10 个)是独立的应用程序,每个应用程序都有 Main

我的要求很简单:

1) 能够在 Eclipse 中 运行 projX 和 projY
2) 为 projX 和 projY 编译独立的 JAR(带有依赖项)每个命令
3) 干

我目前有一个来自我的前任的工作设置。它每个库有一个 pom.xml 文件,这很好,但每个项目有两个 pom.xml 文件,这不是。其中一个甚至 compile 都不需要,但另一个 compile assembly:single 需要。这两个文件几乎是相互重复的,一个项目中添加一个新的库需要更改3-4个不同的地方。

我尝试过各种方法。令人惊讶的是,在根中添加超级父 pom.xml 是最简单的。它负责项目范围的设置,例如 UTF-8 源文件编码等等。没有用的是其他一切。在 POM 之间无法相互引用,除非进行某些修改,拒绝编译源文件夹,坚持使用永远为空的本地存储库或否则需要绝对路径,我无法满足我列出的要求.尽管阅读了官方文档并在线搜索了示例,但还是如此。

有人能帮忙吗?

您将需要 3 个 pom.xml 个文件,一个用于 parent 个项目,一个用于每个 child 个项目。您将拥有 3 个独立的项目。在 parent 项目中,您将仅具有与 child 项目相同的 jar 的依赖项。例如

Parent 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

<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>

另一个childpom.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>...

此示例用于具有单独模型层和表示层的 Web 应用程序。可以通过 Eclipse 或 Intellij Idea 轻松创建多模块项目。

通常情况下,这对于 Maven 来说应该不会太复杂。主要思想是将大部分冗余信息放在父模块中(为了简单起见,我们只使用一个父模块),然后让子模块引用父模块。

这带来了将大多数 "configurable" 设置集中在一处的优势,并限制了冗余量。

See this link for information regarding the different forms of inheritance in Maven

你的项目结构应该是这样的:

+
     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

您将有 3 种 pom.xml(每个 project/library 1 种)

  1. 父根项目(列出所有 'modules' 并提供所有可继承的设置,如依赖版本、插件版本和配置、属性等...)

<?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. 每个库一个 pom.xml(libA、libB 等...)

<?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. 每个项目(projX、projY 等)一个 pom.xml,在这里我们还将 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>

在此配置中,maven-assembly 附加到 "package" 阶段,这意味着通过在您的根项目中调用 mvn package,您将拥有所有 jar 和 jar-with-dependencies 构建一口气