简体   繁体   中英

Integration in Maven module project

I am facing issues in organizing Maven multi-module projects. I want to use the files in one child project(B) from other child projects which are C, D and E . Below is complete scenario described.

I have Parent project A and its child projects are B(output jar) , C(output pom) and D(output pom) . Project C has its own four children( F, G, Example1 and Example2 ). And, project D has its children which is named as E(output pom) . It looks like this:

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)

Now, project B is my GUI of the project which internally uses the some of the files from project C, D, E, FG Example1 and 2 . During programming I am able to execute the files from all projects in project B . But problems come when I release the final package of the project and use only jar of the project B because I am unable to execute files from the other projects which are C, D, E, FG Example1 and 2 . Could anyone please help me how to use other projects? I am beginner in the Maven so I do not have idea how to integrate files between different child projects.

Try with following way - hope it should work

The main idea is to create FAT jar of project-b using assembly plugin (refer project-b pom.xml file content below)

Basically, I created project structure as per your description and pom file entries are like below:

(refer image for project structure at the end)

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

it will generate fat jar (project-b-jar-with-dependencies.jar, see in image) of project -b and output should be something like below-

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

Project structure image:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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