sprinboot系列一——搭建併發布多模塊springboot應用

1,創建maven的project,maven選quickstart

2,創建子module,maven選quickstart,添加相互依賴

添加模塊是報錯,改父pom文件的packaging屬性,jar改爲pom即可
pom.xml

 <packaging>pom</packaging>

創建成功後,項目層級如下:
在這裏插入圖片描述
service涉及其他模塊依賴,在service模塊的pom添加其他模塊依賴,其他模塊類似:

  	<dependency>
			<groupId>com.cxz</groupId>
			<artifactId>million-manager</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>

最外層父級pom添加springboot相關依賴,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>

  <groupId>com.cxz</groupId>
  <artifactId>onemillion</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>

  <name>onemillion</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		 <version>2.1.1.RELEASE</version>
	</parent>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
		
		
  </dependencies>
  <modules>
  	<module>million-common</module>
  	<module>million-dao</module>
  	<module>million-manager</module>
  	<module>million-client</module>
  	<module>million-service</module>
  </modules>
  
</project>

3,運行hello world

在service中啓動主線程,添加相關的映射即可完成訪問,其中,啓動的主線程要放最外層包,其它映射方可被掃描,結構如下:
在這裏插入圖片描述

start和controller代碼如下:

package org.million.service;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Hello world!
 *
 */
@SpringBootApplication
public class Start {
   public static void main(String[] args) {
       SpringApplication.run(Start.class, args);
   }
}
package org.million.service.test;

import org.million.common.Common;
import org.million.dao.Hello;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
	@GetMapping("/hi")
	public String hi() {
		return "hi, I'm springboot !"+Common.hello()+Hello.hello;
	}
}

運行Start.java,瀏覽器輸入http://localhost:8080/hi,即可得到相應的返回

在這裏插入圖片描述

3,添加打包配置,指定主工程入口

springboot有官方插件可以進行打包,在運行主工程service進行打包,指明程序執行入口即可,在service的pom中加入插件,父模塊不需要加入build和plugin,最終,service模塊pom內容如下:

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.cxz</groupId>
    <artifactId>onemillion</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>million-service</artifactId>
  <name>million-service</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
        	<dependency>
			<groupId>com.cxz</groupId>
			<artifactId>million-manager</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
  </dependencies>
<build>
		  <resources>
           <resource>
               <directory>src/main/java</directory>
               <includes>
                   <include>**/*.xml</include>
               </includes>
           </resource>
           <resource>
               <directory>src/main/resources</directory>
               <includes>
                   <include>**.*</include>
                   <include>**/*.*</include><!-- i18n能讀取到 -->
                    <include>**/*/*.*</include>
               </includes>
           </resource>
         </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
	             <fork>true</fork> 
	             <!-- 如果沒有該配置,devtools不會生效 -->
	            <!-- 指定該Main Class爲全局的唯一入口 -->
	            <mainClass>org.million.service.Start</mainClass>
	            <layout>ZIP</layout>
          </configuration>
          <executions>
            <execution>
              <goals>
                <goal>repackage</goal><!--可以把依賴的包都打包到生成的Jar包中-->
              </goals>
            </execution>
          </executions>
          </plugin>
        </plugins>
        <finalName>onemilion</finalName>
    </build>
</project>

4,打包發佈

mavan 命令 執行 maven clean install 即可在service模塊的target目錄生成可執行jar文件,java -jar +相關jar包名即可完成啓動
在這裏插入圖片描述

工程下載路徑:
https://download.csdn.net/download/qq_31443653/11074895

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章