【问题标题】:Integration in Maven module project在 Maven 模块项目中集成
【发布时间】:2018-05-23 14:47:55
【问题描述】:

我在组织 Maven 多模块项目时遇到问题。我想使用来自C, D and E 的其他子项目的one child project(B) 中的文件。以下是描述的完整场景。

我有父项目A,其子项目是B(output jar)C(output pom)D(output pom)。项目C 有自己的四个孩子(F, G, Example1 and Example2)。并且,项目D 有它的孩子,命名为E(output pom)。它看起来像这样:

Project A(parent) |-- project B(child1; outputs jar which is my GUI) |-- project C(child2; outputs pom) |-- project F(child of project C, outputs war) |-- project G(child of project C, outputs war) |-- project Example1(child of project C, outputs war) |-- project Example2(child of project C, outputs war) |-- project D(child3; outputs pom) |-- project E(child of project D; outputs pom)

现在,项目 B 是我的项目的 GUI,它在内部使用来自 project C, D, E, F. G. Example1 and 2 的一些文件。在编程期间,我能够执行project B 中所有项目的文件。但是当我发布项目的最终包并仅使用project B 中的jar 时出现问题,因为我无法执行来自C, D, E, F. G. Example1 and 2 的其他项目的文件。谁能帮我如何使用其他项目?我是 Maven 的初学者,所以我不知道如何在不同的子项目之间集成文件。

【问题讨论】:

标签: java maven maven-3 parent-child


【解决方案1】:

尝试以下方式 - 希望它应该工作

主要思想是使用汇编插件创建project-bFAT jar(参考下面的project-b pom.xml文件内容)

基本上,我根据您的描述创建了项目结构 和 pom 文件条目如下:

(项目结构见最后图片)

**project-a** pom.xml file content:

 <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.test</groupId>
            <artifactId>project-a</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <packaging>pom</packaging>
            <modules>
                <module>project-b</module>
                <module>project-c</module>
                <module>project-d</module>
            </modules>
            <build>
                <finalName>project-b</finalName>
                <pluginManagement>
                    <plugins>
                        <!-- Set a compiler level -->
                        <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-compiler-plugin</artifactId>
                            <version>2.3.2</version>
                            <configuration>
                                <source>${jdk.version}</source>
                                <target>${jdk.version}</target>
                            </configuration>
                        </plugin>

                        <!-- test plugin -->
                        <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-surefire-plugin</artifactId>
                            <version>2.18.1</version>
                        </plugin>

                        <!-- War plugin -->
                        <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-war-plugin</artifactId>
                            <configuration>
                                <webXml>src\main\webapp\WEB-INF\web.xml</webXml>
                            </configuration>
                        </plugin>

                        <!-- Maven Assembly Plugin -->
                        <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-assembly-plugin</artifactId>
                            <version>2.4.1</version>
                            <configuration>
                                <!-- get all project dependencies -->
                                <descriptorRefs>
                                    <descriptorRef>jar-with-dependencies</descriptorRef>
                                </descriptorRefs>
                                <!-- MainClass in mainfest make a executable jar -->
                                <archive>
                                    <manifest>
                                        <mainClass>com.test.MainApp</mainClass>
                                    </manifest>
                                </archive>
                            </configuration>
                            <executions>
                                <execution>
                                    <id>make-assembly</id>
                                    <!-- bind to the packaging phase -->
                                    <phase>package</phase>
                                    <goals>
                                        <goal>single</goal>
                                    </goals>
                                </execution>
                            </executions>
                        </plugin>
                    </plugins>
                </pluginManagement>
            </build>
        </project>


        **project-b** pom.xml file content

            <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.test</groupId>
                <artifactId>project-a</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </parent>
            <artifactId>project-b</artifactId>
            <properties>
                <jdk.version>1.7</jdk.version>
            </properties>
            <dependencies>
                <dependency>
                    <groupId>com.test</groupId>
                    <artifactId>project-c</artifactId>
                    <version>0.0.1-SNAPSHOT</version>
                    <type>war</type>
                </dependency>
                <dependency>
                    <groupId>com.test</groupId>
                    <artifactId>project-d</artifactId>
                    <version>0.0.1-SNAPSHOT</version>
                    <type>pom</type>
                </dependency>
                <dependency>
                    <groupId>com.test</groupId>
                    <artifactId>project-e</artifactId>
                    <version>0.0.1-SNAPSHOT</version>
                </dependency>
            </dependencies>
            <build>
                <plugins>
                    <!-- Set a compiler level -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-compiler-plugin</artifactId>
                    </plugin>

                    <!-- test plugin -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                    </plugin>

                    <!-- Maven Assembly Plugin -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-assembly-plugin</artifactId>
                        <configuration>
                            <!-- get all project dependencies -->
                            <descriptorRefs>
                                <descriptorRef>jar-with-dependencies</descriptorRef>
                            </descriptorRefs>
                            <!-- MainClass in mainfest make a executable jar -->
                            <archive>
                                <manifest>
                                    <mainClass>com.test.MainApp</mainClass>
                                </manifest>
                            </archive>
                        </configuration>
                        <executions>
                            <execution>
                                <id>make-assembly</id>
                                <!-- bind to the packaging phase -->
                                <phase>package</phase>
                                <goals>
                                    <goal>single</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </project>

        **project-c** pom.xml file content

            <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.test</groupId>
                <artifactId>project-a</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </parent>
            <artifactId>project-c</artifactId>
            <packaging>war</packaging>
            <build>
                <plugins>
                    <!-- Set a compiler level -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-compiler-plugin</artifactId>
                    </plugin>


                    <!-- War plugin -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-war-plugin</artifactId>
                    </plugin>
                </plugins>
            </build>
        </project>

    **project-d** pom.xml file content

    <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.test</groupId>
        <artifactId>project-a</artifactId>
        <version>0.0.1-SNAPSHOT</version>
      </parent>
      <artifactId>project-d</artifactId>
      <packaging>pom</packaging>
      <modules>
        <module>project-e</module>
      </modules>
    </project>

    **project-e** pom.xml file content
    <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.test</groupId>
            <artifactId>project-d</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </parent>
        <artifactId>project-e</artifactId>
        <build>
            <plugins>
                <!-- Set a compiler level -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                </plugin>

                <!-- test plugin -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    </project>



Finally open git bash or cmd from folder project-a and run following maven command

mvn clean install -DskipTests

它会生成项目-b的胖jar(project-b-jar-with-dependencies.jar,见图片),输出应该如下所示-

[INFO] project-a ......................................... SUCCESS [  1.084 s]
[INFO] project-c ......................................... SUCCESS [  2.525 s]
[INFO] project-d ......................................... SUCCESS [  0.019 s]
[INFO] project-e ......................................... SUCCESS [  1.552 s]
[INFO] project-b ......................................... SUCCESS [  2.873 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8.369 s
[INFO] Finished at: 2018-05-23T23:51:25+05:30
[INFO] Final Memory: 22M/172M
[INFO] ------------------------------------------------------------------------

项目结构图:

【讨论】:

  • 非常感谢您的时间和精力。但我意识到我的项目更复杂,我已经编辑了描述。你能帮我编辑一下场景吗?
  • 我建议您首先了解我分享的概念(FAT jar 创建)和想法,并基于您可以为任何嵌套层次结构项目执行的类似方式
猜你喜欢
  • 1970-01-01
  • 2018-09-10
  • 1970-01-01
  • 1970-01-01
  • 2021-07-22
  • 1970-01-01
  • 1970-01-01
  • 2013-12-20
  • 1970-01-01
相关资源
最近更新 更多