作者:humanity11

 

(1)项目的结构:搭建spring-boot多模块时,需要有一个父模块作为所有子模块的统一依赖,父模块有时只是一个文件夹,文件夹中放着父类的pom.xml文件,子模块的文件夹。比如我将建立3个子模块,我的目录结构是这样的:spring-boot 多模块的搭建_SpringBoot

 在dataCollection文件夹下:有collection1,collection2,collection3和pom.xml.其中pom.xml是父类的pom.xml.

(2)创建项目:以eclipse为例,打开file->new->maven project->next->next->maven-archetype-quickstart->next->填写groupId和artifactId(项目名,这里dataCollection)->finish;

删除除pom.xml外的其他文件,在pom.xml中配置maven的相关依赖:

<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>cdmvs-dataColl</groupId>
  <artifactId>dataCollection</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
  <name>dataCollection</name>
  <url>http://maven.apache.org</url>
  <!-- 指向spring的依赖库 -->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.0.RELEASE</version>
  </parent>
<span style="white-space:pre;">     </span><!-- 设置版本的相关属性-->  
<properties>  
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
    <spring.version>4.2.3.RELEASE</spring.version>  
    <spring.boot.version>1.3.0.RELEASE</spring.boot.version>  
</properties>
<span style="white-space:pre;">     </span><!--这里指明有3个子模块 -->  
    <modules>  
        <module>collection1</module>  
        <module>collection2</module>  
        <module>collection3</module>  
    </modules>
<span style="white-space:pre;">     </span><!-- 插件配置,标明jdk版本1.8 -->  
    <build>  
        <plugins>  
            <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-compiler-plugin</artifactId>  
                <version>3.6.0</version>  
                <configuration>  
                    <source>1.8</source>  
                    <target>1.8</target>  
                </configuration>  
            </plugin>  
        </plugins>  
    </build>  
</project>

ok,一个父类的pom.xml就完成了。

接下来创建子项目collection1,collection2,collection3:

以colleciton1为例,collection2和collection3同理:file->new->maven project->next->next->maven-archetype-quickstart->next->填写groupId和artifactId(项目名,这里collection1)->finish;

colleciton1,2,3的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>cdmvs-dataColl</groupId>  
    <artifactId>collection2</artifactId>  
    <version>0.0.1-SNAPSHOT</version>  
    <name>collection2</name>  
    <url>http://maven.apache.org</url>
<span style="white-space:pre;"> </span><!-- 指明父类的依赖时dataCollection -->  
    <parent>  
        <groupId>cdmvs-dataColl</groupId>  
        <artifactId>dataCollection</artifactId>  
        <version>0.0.1-SNAPSHOT</version>  
    </parent>  
    <properties>  
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
    </properties>  
    <dependencies>  
          
    </dependencies>  
</project>

(3)接下来根据各模块的特定来配置各模块的pom.xml

比如本文我是collection1作为spring boot启动点,collection2做为controller 层,collection3暂时没想到,那我collection1则应该依赖springboot的相关配置依赖,ok,引入maven如下:

<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>cdmvs-dataColl</groupId>
  <artifactId>collection1</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>collection1</name>
  <url>http://maven.apache.org</url>
  <parent>
    <groupId>cdmvs-dataColl</groupId>
    <artifactId>dataCollection</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- automatically restart whenever files on the classpath change -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
      <optional>true</optional>
    </dependency>
  </dependencies>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <!-- Import dependency management from Spring Boot -->
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>1.3.5.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>

collecicton2 层是controller,它的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>cdmvs-dataColl</groupId>  
    <artifactId>collection2</artifactId>  
    <version>0.0.1-SNAPSHOT</version>  
    <name>collection2</name>  
    <url>http://maven.apache.org</url>  
    <parent>  
        <groupId>cdmvs-dataColl</groupId>  
        <artifactId>dataCollection</artifactId>  
        <version>0.0.1-SNAPSHOT</version>  
    </parent>  
    <properties>  
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
    </properties>  
    <dependencies>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-webmvc</artifactId>  
            <version>${spring.version}</version>  
        </dependency>  
    </dependencies>  
</project>

(4)jar包的maven依赖弄好了,该配置启动springboot类了,在collection1->src->main->java文件夹下新建App.java如下:

package cdmvs_dataColl_collection3;  
import org.slf4j.Logger;  
import org.slf4j.LoggerFactory;  
import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
import org.springframework.context.annotation.ComponentScan;  
import org.springframework.context.annotation.Configuration;  
  
/** 
 * Hello world! 
 * 
 */  
@Configuration  
@SpringBootApplication  
@ComponentScan(basePackages = "cdmvs_dataColl_collection2")//这里是什么鬼?  
public class App   
{  
    private static Logger logger= LoggerFactory.getLogger(App.class);  
    public static void main( String[] args )  
    {  
        SpringApplication.run(App.class, args);  
    }  
}

在app.java 文件中有一个这样的注解:@ComponentScan(basePackages = "cdmvs_dataColl_collection2") 

刚才不是说collection2做为我们的controller吗,那coolection1我怎么能找到colleciton2?那就通过basePackages指向我们要扫描的文件包,同时将collection1通过依赖进来就行了。在collection2 src/main/java 下新建包basePackages,同时写controller文件类AppController.java:

package cdmvs_dataColl_collection2;  
  
import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.RequestMethod;  
import org.springframework.web.bind.annotation.ResponseBody;  
  
/**  
 * Hello world!  
 *  
 */  
@Controller  
public class AppController   
{  
    @RequestMapping(value="/hello", method=RequestMethod.GET)    
    @ResponseBody  
    public String hello(){  
       return "hello world";  
   }  
}

(不知道@Controller和@RequestMapping(value="/hello", method=RequestMethod.GET) ,@ResponseBody的,自己去http://www.javaworld.com/article/2078034/spring-framework/mastering-spring-mvc.html扫盲)

然后在collection1的pom.xml中将collection1引入:

<span style="white-space:pre;">     </span><dependency>  
            <groupId>cdmvs-dataColl</groupId>  
            <artifactId>collection1</artifactId>  
            <version>${project.version}</version>  
<span style="white-space:pre;">     </span></dependency>

(5)启动

spring-boot 多模块的搭建_SpringBoot_02