美文网首页
2018-07-11 Maven multiple module

2018-07-11 Maven multiple module

作者: 猪迹 | 来源:发表于2018-07-19 14:06 被阅读0次

Step 1 Create project folder structure

Create a folder as the root-folder of the project, mkdir ex_multimodule.
Inside the root-folder, make the folders for library & application.

cd ex_multimodule
mkdir library application

The folder structure as below.

E:\Downloads\documents\STS_projects\ex_multimodules>dir
 驱动器 E 中的卷是 Tools
 卷的序列号是 E885-5830

 E:\Downloads\documents\STS_projects\ex_multimodules 的目录

2018/07/11  13:57    <DIR>          .
2018/07/11  13:57    <DIR>          ..
2018/07/11  13:57    <DIR>          application
2018/07/11  13:57    <DIR>          library
               0 个文件              0 字节
               4 个目录 56,900,472,832 可用字节

Step 2 Create Maven configuration for Multi-Module project

In the root-folder of the project, execute mvn io.takari:maven:wrapper to let Maven to build the project structure.
Check the folder structure after execution.

E:\Downloads\documents\STS_projects\ex_multimodules>tree /f .
卷 Tools 的文件夹 PATH 列表
卷序列号为 E885-5830
E:\DOWNLOADS\DOCUMENTS\STS_PROJECTS\EX_MULTIMODULES
│  mvnw
│  mvnw.cmd
│
├─.mvn
│  └─wrapper
│          maven-wrapper.jar
│          maven-wrapper.properties
│          MavenWrapperDownloader.java
│
├─application
└─library

Step 3 Prepare the pom.xml for the whole project

<?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>org.springframework</groupId>
    <artifactId>gs-multi-module</artifactId>
    <version>0.1.0</version>
    <packaging>pom</packaging>

    <modules>
        <module>library</module>
        <module>application</module>
    </modules>
</project>

After that, we can check the content of the pom.xml file.

E:\Downloads\documents\STS_projects\ex_multimodules>type 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>org.springframework</groupId>
    <artifactId>gs-multi-module</artifactId>
    <version>0.1.0</version>
    <packaging>pom</packaging>

    <modules>
        <module>library</module>
        <module>application</module>
    </modules>

</project>

Step 4 Make the folders for the library project

E:\Downloads\documents\STS_projects\ex_multimodules>cd library
E:\Downloads\documents\STS_projects\ex_multimodules\library>mkdir src\main\java\hello\service
E:\Downloads\documents\STS_projects\ex_multimodules\library>tree .
卷 Tools 的文件夹 PATH 列表
卷序列号为 E885-5830
E:\DOWNLOADS\DOCUMENTS\STS_PROJECTS\EX_MULTIMODULES\LIBRARY
└─src
    └─main
        └─java
            └─hello
                └─service

Step 5 Prepare Maven for library project

The Spring Boot plugin is NOT used in the library project at all. The main function of the plugin is to create an executable "über-jar" which we don’t need (and don’t want) for a library.
Below is the content of library\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.example</groupId>
    <artifactId>gs-multi-module-library</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

Although the Spring Boot Maven plugin is not being used, you do want to take advantage of Spring Boot dependency management, so that is configured using the spring-boot-starter-parent from Spring Boot as a parent project.

Step 6 Create an Application project

Do not use the same package as the library (or a parent of the library package) unless you want to include all Spring components in the library by @ComponentScan by default in the application.

E:\Downloads\documents\STS_projects\ex_multimodules>cd application
E:\Downloads\documents\STS_projects\ex_multimodules\application>mkdir src\main\java\hello\app
E:\Downloads\documents\STS_projects\ex_multimodules\application>tree .
卷 Tools 的文件夹 PATH 列表
卷序列号为 E885-5830
E:\DOWNLOADS\DOCUMENTS\STS_PROJECTS\EX_MULTIMODULES\APPLICATION
└─src
    └─main
        └─java
            └─hello
                └─app

Step 7 Create Maven configuration for Application project

Below is the content of application\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.example</groupId>
    <artifactId>gs-multi-module-application</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>gs-multi-module-library</artifactId>
            <version>${project.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Step 8 Open the project in Spring-Tool-Suite

To simplify the code writing, we can use Spring-Tool-Suite to open the project.
Use [File] -- [Open Projects from File System...] --- [Directory...] to locate to the root-folder of the project, S-T-S will scan the project and the modules definition automatically.

Step 9 Coding, following the spring guide, in the S-T-S

Reference

相关文章

网友评论

      本文标题:2018-07-11 Maven multiple module

      本文链接:https://www.haomeiwen.com/subject/enshpftx.html