带有Spring Boot的多模块maven

时间:2014-07-04 19:01:05

标签: spring maven spring-boot

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

有人能告诉我一个关于"父母和朋友的例子吗?和" child-pom"?

由于

4 个答案:

答案 0 :(得分:3)

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

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

var process = spawn('ffmpeg', [
  '-i',
  inFile,
  '-metadata',
  'title="Test 123"',     
  '-c',
  'copy',
  outFile
]);

我会用pom包装来定义项目的project folder |---pom.xml |---module1 |---pom.xml |---src |---module2 |---pom.xml |---src |---module3 |---pom.xml |---src ,例如:

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>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的{​​{1}} {。}}。

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

答案 1 :(得分:0)

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

步骤: 1)创建Maven项目。父项目的pom.xml如下所示,

    <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文件。(包装类型:耳朵) 其中包含所有模块的依赖关系,如下所示,

<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文件)

- 祝你好运

答案 2 :(得分:0)

模块作为弹簧启动项目

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

<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 :(得分:0)

您的父级POM具有Spring Boot作为父级。但这是行不通的。您不会在模块中获得依赖项。

您必须配置一个不继承自Spring Boot Parent的项目,并将Spring Boot依赖项添加到依赖项管理中。

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

相关问题