建立一個 SpringBoot 項目很是的簡單,簡單到這裏根本不用再提。你能夠在使用 IDEA 新建項目時直接選擇 Spring Initlalize
建立一個 Spring Boot 項目,也可使用 Spring 官方提供的 Spring Boot 項目生成頁面獲得一個項目。html
<!-- more -->java
下面介紹一下使用 Spring 官方生成的方式,若是你已經有了一個 Spring Boot 項目,這部分能夠直接跳過。git
打開 https://start.spring.io/github
填寫 group
和 Artifact
信息,選擇依賴(我選擇了 Spring Web 和 Lombok )。web
點擊 Generate
按鈕下載項目。spring
打開下載的項目,刪除無用的 .mvn
文件夾,mvnw
、 mvnw.cmd
、HELP.md
文件。shell
到這裏已經獲得了一個 Spring Boot 初始項目了,咱們直接導入到 IDEA 中,看一眼 pom.xml
的內容。apache
<?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 https://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>2.2.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.wdbyte</groupId> <artifactId>springboot-module-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot-module-demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
把目錄結構調整成本身想要的結構,而後添加 controller
和 entity
用於測試。緩存
ProductController 類源代碼。tomcat
@RestController @RequestMapping("/product") public class ProductController { /** * 獲取商品列表 * * @return */ @GetMapping("/list") public Map list() { // 模擬查詢商品邏輯 Product product = new Product(); product.setProductName("小米粥"); product.setProductPrice(new BigDecimal(2.0)); product.setProductStock(100); Map<String, Object> resultMap = new HashMap<>(); resultMap.put("code", 000); resultMap.put("message", "成功"); resultMap.put("data", Arrays.asList(product)); return resultMap; } }
Product 類源代碼。
@Data public class Product { /** 商品名稱. */ private String productName; /** 商品價格. */ private BigDecimal productPrice; /** 商品庫存。 */ private int productStock; }
藉助 IDEA 工具能夠快速的把項目改形成 maven 多模塊,這裏咱們把準備測試 demo 拆分爲 common 和 web 兩個模塊,common 模塊存放實體類。web 模塊存放 controller 層(這裏項目雖小,拆分只是爲了演示)。話很少說,直接開始。
配置主 pom.xml 打包方式 爲 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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <!-- 配置主 pom 打包方式爲 pom --> <packaging>pom</packaging> .... ....
建立 common 模塊
項目直接 new -> module。
選擇 maven -> next,填寫模塊名稱。
繼續 next 完成模塊建立。
建立 web 模塊
web 模塊的建立和 common 模塊一模一樣,再也不贅述。完成兩個模塊的建立以後,你會發現你的主 pom.xml 文件裏自動添加了 module 部分。
<modules> <module>product-common</module> <module>product-web</module> </modules>
移動代碼到指定模塊
移動 Product.java
到 product-common
模塊,其餘部分代碼和 resource 部分直接移動到 product-web
模塊,移動完後你的代碼結構是這個樣子。
到這裏,多模塊已經拆分完成了, 可是 ProductController
代碼裏的紅色警告讓你發現事情尚未結束。
你發現了代碼裏的紅色警告,不過你也瞬間想到了是由於把 Product
類移動到了 product-common
模塊,致使這裏引用不到了。
而後你查看了下 product-common
模塊的 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"> <parent> <artifactId>springboot-module-demo</artifactId> <groupId>com.wdbyte</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>product-common</artifactId> </project>
機智的在 Product-web
模塊的 pom.xml 裏引入 product-common,手起鍵落,輕鬆搞定。
<?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"> <parent> <artifactId>springboot-module-demo</artifactId> <groupId>com.wdbyte</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>product-web</artifactId> <dependencies> <dependency> <groupId>com.wdbyte</groupId> <artifactId>product-common</artifactId> </dependency> </dependencies> </project>
滿心歡喜的你快速的點擊 Build-> Build Project,獲得的 Error 警告刺痛了頂着黑眼圈的你。
不過你仍是迅速定位了問題,查看 maven 依賴,你發現是由於沒有指定 product-common
依賴的版本號。
原來如此,由於沒有指定版本號,咱們指定上不就完事了嘛。在最外層的主 pom.xml 中添加 <dependencyManagement>
添加上指定依賴和要指定的版本號。
<dependencyManagement> <dependencies> <dependency> <groupId>com.wdbyte</groupId> <artifactId>product-common</artifactId> <version>0.0.1-SNAPSHOT</version><!-- maven 打包默認 0.0.1-SNAPSHOT 版本 --> </dependency> </dependencies> </dependencyManagement>
刷新 maven ,發現項目已經不報錯了,編譯成功,運行啓動類,熟悉的 Spring logo 又出如今眼前。
是的,Spring Boot 應用在改形成多模塊後成功運行了起來,可是你貌似發現一個問題,模塊 common
和模塊 web
都繼承了主 pom ,主 pom 中有 Lombok 、Spring Boot Web 和 Spring Boot Test 依賴,而 common
模塊裏只用到了 Lombok 啊,卻同樣繼承了 Spring Boot 其餘依賴,看來仍是要改造一把。
只有 common
模塊用到的依賴移動到 common
模塊。
<?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"> <parent> <artifactId>springboot-module-demo</artifactId> <groupId>com.wdbyte</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>product-common</artifactId> <dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> </dependencies> </project>
只有 web
模塊用到的依賴移動到 web
模塊。
<?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"> <parent> <artifactId>springboot-module-demo</artifactId> <groupId>com.wdbyte</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>product-web</artifactId> <dependencies> <dependency> <groupId>com.wdbyte</groupId> <artifactId>product-common</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> </project>
抽取用到的版本號到 <properties>
,這裏抽取 common
模塊的依賴版本。
到這裏最外層主 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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <packaging>pom</packaging> <modules> <module>product-common</module> <module>product-web</module> </modules> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.wdbyte</groupId> <artifactId>springboot-module-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot-module-demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <product-common.version>0.0.1-SNAPSHOT</product-common.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>com.wdbyte</groupId> <artifactId>product-common</artifactId> <version>${product-common.version}</version> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
看似完美,從新 Build-> Build Project ,發現一切正常,運行發現一切正常,訪問正常。
好了,終於到了最後一步了,你感受到勝利的曙光已經照到了頭頂,反射出耀眼的光芒。接着就是 mvn package
。
[INFO] springboot-module-demo ............................. SUCCESS [ 2.653 s] [INFO] product-common ..................................... FAILURE [ 2.718 s] [INFO] product-web ........................................ SKIPPED [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 6.084 s [INFO] Finished at: 2020-03-19T08:15:52+08:00 [INFO] Final Memory: 22M/87M [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.2.5.RELEASE:repackage (repackage) on project product-common: Execution repackage of goal org.springframework.boot:spring-boot-m aven-plugin:2.2.5.RELEASE:repackage failed: Unable to find main class -> [Help 1] [ERROR]
ERROR 讓你傷心了,可是你仍是從報錯中尋找到了一些蛛絲馬跡,你看到是 spring-boot-maven-plugin 報出的錯誤。從新審視你的主 pom 發現 <build>
編譯插件用到了 spring-boot-maven-plugin。
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
略加思索後將這段移動到 web
模塊的 pom,由於這是 Spring Boot 的打包方式,如今放在主 pom 中全部的模塊都會繼承到,那麼對於 common
模塊來講是確定不須要的。
移動後從新打包,無論你是運行命令 mvn package
仍是雙擊 IDEA 中的 maven 管理中的 package ,想必這時候你都已經打包成功了
在 web
模塊下的目錄 target 裏也能夠看到打包後的 jar 文件 product-web-0.0.1-SNAPSHOT.jar。可使用 java 命令直接運行。
$ \springboot-module-demo\product-web\target>java -jar product-web-0.0.1-SNAPSHOT.jar . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.2.5.RELEASE) 2020-03-19 08:33:03.337 INFO 15324 --- [ main] com.wdbyte.Application : Starting Application v0.0.1-SNAPSHOT on DESKTOP-8SCFV4M with PID 15324 (C:\Users\83981\Desktop\springboot-mod ule-demo\product-web\target\product-web-0.0.1-SNAPSHOT.jar started by 83981 in C:\Users\83981\Desktop\springboot-module-demo\product-web\target) 2020-03-19 08:33:03.340 INFO 15324 --- [ main] com.wdbyte.Application : No active profile set, falling back to default profiles: default 2020-03-19 08:33:04.410 INFO 15324 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2020-03-19 08:33:04.432 INFO 15324 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2020-03-19 08:33:04.432 INFO 15324 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.31] 2020-03-19 08:33:04.493 INFO 15324 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2020-03-19 08:33:04.493 INFO 15324 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1107 ms 2020-03-19 08:33:04.636 INFO 15324 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2020-03-19 08:33:04.769 INFO 15324 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2020-03-19 08:33:04.772 INFO 15324 --- [ main] com.wdbyte.Application : Started Application in 1.924 seconds (JVM running for 2.649) 2020-03-19 08:33:07.087 INFO 15324 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
想必少了點什麼,多模塊不只爲告終構清晰,更是爲了其餘項目能夠複用模塊(如 common 模塊),如今這個時候若是你新打開了一個項目,依賴 common
發現是引用不到的,由於你須要把模塊安裝到本地倉庫。能夠點擊 IDEA -> Maven -> install,也能夠經過 maven 命令。
# -Dmaven.test.skip=true 跳過測試 # -U 強制刷新 # clean 清理緩存 # install 安裝到本地倉庫 $ \springboot-module-demo> mvn -Dmaven.test.skip=true -U clean install
從新引入發現沒有問題了。 文中代碼已經上傳到 Github: https://github.com/niumoo/springboot
這篇文章到這裏就結束了,若是你以爲不錯能夠關注個人公衆號一塊兒交流。 公衆號滿載乾貨,童叟無欺。
原文出處:https://www.cnblogs.com/niumoo/p/12550178.html