繁体   English   中英

使用Intellij和Maven创建多模块项目

[英]Creating multi module project using Intellij & Maven

我有一个项目结构,其中包含一个核心工具和一个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的建议?

我的图案正确吗? 这种项目结构还有其他众所周知的模式吗?

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

如下创建一个父pom.xml (请注意,我没有将spring-boot-starter-parent作为项目的父项添加,而是通过spring-boot-dependencies引入了Spring Boot spring-boot-dependencies ):

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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