我想知道如何使用 Spring Boot 使用 Maven 创建一个多模块项目。

有人可以给我看一个“parent-pom”和“child-pom”的例子吗?

谢谢

请您参考如下方法:

这个问题太简单而无法回答或太复杂而无法理解,否则我无法解释为什么没有答案。

我会选择使用如下文件夹结构的解决方案

project folder 
|---pom.xml 
|---module1 
    |---pom.xml 
    |---src 
|---module2 
    |---pom.xml 
    |---src 
|---module3 
    |---pom.xml 
    |---src 

我会定义 pom.xml带有 pom 包装的项目,例如:
<?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> 
 
    <parent> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-starter-parent</artifactId> 
        <version>1.2.7-RELEASE</version> 
        <relativePath/> 
    </parent> 
 
    <groupId>com.myproject</groupId> 
    <artifactId>project</artifactId> 
    <packaging>pom</packaging> 
    <version>0.0.1-SNAPSHOT</version> 
 
    <modules> 
        <module>module1</module> 
        <module>module2</module> 
        <module>module3</module> 
    </modules> 
 
    <name>MySupercoolProject</name> 
    <description>What else?</description> 
 
    <properties> 
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 
        <java.version>1.8</java.version> 
        <maven.compiler.source>1.8</maven.compiler.source> 
        <maven.compiler.target>1.8</maven.compiler.target> 
    </properties> 
 
    <dependencies> 
      <!-- Put here dependencies shared by your project --> 
    </dependencies> 
</project> 

如您所见,您的项目的父级将是 spring-boot-starter-parent .

然后, 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> 
 
    <parent> 
        <groupId>com.myproject</groupId> 
        <artifactId>project</artifactId> 
        <version>0.0.1-SNAPSHOT</version> 
    </parent> 
 
    <groupId>com.myproject</groupId> 
    <artifactId>module1</artifactId> 
    <packaging>war</packaging> 
    <version>0.0.1-SNAPSHOT</version> 
 
    <name>Application</name> 
    <description>SudenGut application</description> 
 
    <properties> 
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 
        <start-class>com.myproject.MyApplication</start-class> 
        <java.version>1.8</java.version> 
        <tomcat.version>8.0.24</tomcat.version> 
        <maven.compiler.source>1.8</maven.compiler.source> 
        <maven.compiler.target>1.8</maven.compiler.target> 
    </properties> 
 
    <dependencies> 
        <dependency> 
            <groupId>org.springframework.boot</groupId> 
            <artifactId>spring-boot-starter-web</artifactId> 
        </dependency> 
        <!--- this will not be enough to provide a cool app :) --> 
    </dependencies> 
 
    <build> 
        <plugins> 
            <plugin> 
                <groupId>org.springframework.boot</groupId> 
                <artifactId>spring-boot-maven-plugin</artifactId> 
            </plugin> 
        </plugins 
    </build> 
</project> 

其中主要应用程序是 MyApplication.java包装内 com.myprojectmodule1 .

我会对没有主应用程序的其他模块使用 jar 打包,除非您有其他子模块(即再次 pom 打包)。


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!