Maven 启动多模块的SpringBoot 项目

Maven 启动多模块的SpringBoot 项目

项目结构

----submodule
------baseModule
--------pom.xml
------startModule1
--------pom.xml
------startModule2
--------pom.xml
------pom.xml

其中 start module是可以启动的项目

submodule 的pom.xml配置如下

 
        
            
                src/main/resources
                true
            
        
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                2.5.1
                
                    1.8
                    1.8
                    -Xlint:all
                    true
                    true
                
            

            
                org.apache.maven.plugins
                maven-resources-plugin
                3.1.0
                
                    
                        test
                        
                            resources
                            testResources
                        
                    
                
            

            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    true
                
            
        
    

关键问题在于spring-boot-maven-plugin这个插件,在父模块中一定将skip这个属性设置为true

因为这个模块里面没有实际代码,所以不能集成SpringBoot 的启动相关属性,如果不跳过这个插件就会在目录中查找可供启动的Application类,找不到自然会报一个缺少mainClass 的错误。

子模块的pom.xml 设置如下

 
        xxxxxx
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    false
                
            
        
  

因为一个工程可能有多个模块,但是只有一个或者几个模块会生成最终的可执行jar包,所以只有最终要生成可运行jar包的模块需要配置spring-boot-maven-plugin这个插件。

而在 一个依赖正常的jar包中,一个最终的可执行模块不应该依赖于其他的可执行模块。所以

我们在所有的可执行模块中配置spring-boot-maven-plugin 插件,然后通过maven的profile执行包含哪几个包

比如上面的module1 module2都依赖basemodule ,它们分别最后要生成最后的可执行文件,那么在submodule的pom.xml中,我们就可以这么配置

 
   basemodule
 
...


        
            module1
            
            
            
                module1
            
        
    
        
            module2
            
            
            
                module2
            
        
    

就在基础配置的时候只配置basemodule,而在profile中加入最终要运行的module

最后附上完整的mvn启动命令

mvn clean package -P module1 -Dmave.test.skip=true spring-boot:run

#参数解释
# -P 指定配置文件
#  -Dmave.test.skip=true 跳过单元测试
上一篇
下一篇