使用Intellij&创建多模块项目Maven的

时间:2018-05-07 18:41:18

标签: java maven intellij-idea

我有一个项目结构,它有一个核心工具和一个REST API来交互和配置该工具。

我有这样的包结构:

com.example.api -> contains API definitions
com.example.commons -> contains module common to both core and api
com.exmaple.core -> contains the core module

这主要是一个Spring Boot应用程序,它们都将在不同的容器中运行。所以API和Core jar会有所不同。

如何使用此结构创建多模块项目,以便我也可以在子包(core& api)中使用commons模块。 关于如何在这样的环境中配置Maven和IntelliJ的建议?

我的模式是对的吗?这种项目结构还有其他众所周知的模式吗?

1 个答案:

答案 0 :(得分:2)

您可以根据需要拥有一个多Maven模块项目。这是一个关于如何配置模块的示例:

如下所示创建父pom.xml(请注意,我没有将spring-boot-starter-parent添加为项目的父项,我将spring-boot-dependencies)引入Spring Boot依赖项:

<?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>parent</artifactId>
    <packaging>pom</packaging>
    <version>1.0.0-SNAPSHOT</version>

    <modules>
        <module>example-commons</module>
        <module>example-core</module>
        <module>example-api</module>
    </modules>

    <dependencyManagement>
        <dependencies>

            <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-dependencies -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>1.5.3.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

        </dependencies>
    </dependencyManagement>

    <dependencies>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.5.3.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>1.5.3.RELEASE</version>
        </dependency>

    </dependencies>

</project>

不可执行模块的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">
    <parent>
        <artifactId>parent</artifactId>
        <groupId>com.example</groupId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>example-commons</artifactId>

</project>

而可执行模块的pom.xml就像(注意spring-boot-maven-plugin仅存在于此依赖项中):

<?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">
    <parent>
        <artifactId>parent</artifactId>
        <groupId>com.example</groupId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>example-core</artifactId>

    <build>
        <plugins>
            <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-maven-plugin -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.5.3.RELEASE</version>
                <configuration>
                    <executable>true</executable>
                </configuration>
            </plugin>

        </plugins>
    </build>

    <dependencies>

        <dependency>
            <groupId>com.example</groupId>
            <artifactId>example-commons</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>

    </dependencies>

</project>