首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Spring Boot的多模块maven示例

使用Spring Boot的多模块maven示例
EN

Stack Overflow用户
提问于 2014-07-05 03:01:05
回答 3查看 11.6K关注 0票数 11

我想知道如何使用Spring Boot在maven中创建多模块项目。

谁能给我举个"parent-pom“和"child-pom”的例子?

谢谢

EN

回答 3

Stack Overflow用户

发布于 2015-11-01 20:55:46

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

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

代码语言:javascript
复制
project folder
|---pom.xml
|---module1
    |---pom.xml
    |---src
|---module2
    |---pom.xml
    |---src
|---module3
    |---pom.xml
    |---src

我会用一个pom包来定义项目的pom.xml,例如:

代码语言:javascript
复制
<?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将是,例如:

代码语言:javascript
复制
<?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>

其中,主应用程序是module1com.myproject包中的MyApplication.java

我会在没有主应用程序的情况下将jar打包用于其他模块,除非您有其他子模块(即,再次打包pom )。

票数 10
EN

Stack Overflow用户

发布于 2017-12-12 20:25:06

作为spring boot项目的模块

Spring boot通常与父母一起工作。但是,如果你创建了多模块项目,如果你想在你的一些模块上使用spring boot,那么有一个重要的问题,那就是哪个模块将成为父模块?当然,您的主项目必须是父项目。因此,您应该在模块中使用spring boot作为依赖项。

代码语言:javascript
复制
<dependencyManagement>
     <dependencies>
        <dependency>
            <!-- Import dependency management from Spring Boot -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>1.5.9.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

有关详细信息,请查看以下文档;

https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-build-systems.html#using-boot-maven-without-a-parent

票数 3
EN

Stack Overflow用户

发布于 2017-12-11 17:14:33

@juanhl很简单。您可以使用Spring Boot创建多模块maven项目。

步骤:1)创建Maven project.The父项目的pom.xml如下:

代码语言:javascript
复制
    <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.springmultumoduleweb</groupId>
      <artifactId>multimodule-web-app</artifactId>
      <packaging>pom</packaging>

      <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
      </parent> 
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
      <modules>
        <module>multimodule-api</module>
        <module>multimodule-service</module>
        <module>EarModule</module>
      </modules>
      <version>0.0.1-SNAPSHOT</version>
    </project>

2)将maven模块添加到父模块子模块可以有其他模块的一个或多个依赖,打包类型可以是jar或war。

3)再创建一个模块生成ear文件(打包类型: ear ),包含所有模块的依赖关系,如下所示

代码语言:javascript
复制
<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.springbootparent.demo</groupId>
    <artifactId>com.springbootparent.demo</artifactId>
    <version>0.0.1-SNAPSHOT</version
  </parent>
  <artifactId>com.springboot.ear</artifactId>
  <packaging>ear</packaging>
  <dependencies>
    <dependency>
        <groupId>com.springbootparent.demo</groupId>
        <artifactId>com.springboot.service1</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.springbootparent.demo</groupId>
        <artifactId>com.springboot.service2</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.springbootparent.demo</groupId>
        <artifactId>com.springboot.war1</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <type>war</type>
    </dependency>
    <dependency>
        <groupId>com.springbootparent.demo</groupId>
        <artifactId>com.springboot.war2</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <type>war</type>
    </dependency>
  </dependencies>
</project> 

4)清理并安装maven父项目。

5)将创建好的ear/war文件部署到服务器上。(对于ear文件,您必须部署在企业应用服务器上)

-Good幸运

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24579692

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档