使用Maven創建Springboot的父子工程


1、在eclipse開發工具中創建一個新的Maven項目,項目類型為quickstart,如下所示:

然后項目類型為quickstart,如下所示:

然后設置Maven項目的信息(Group Id、Artifact Id、Version等),如下所示:

修改pom.xml配置文件,添加SpringBoot的依賴配置與相關插件,如下所示:

 1 <project xmlns="http://maven.apache.org/POM/4.0.0"
 2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
 4     http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <modelVersion>4.0.0</modelVersion>
 6 
 7     <!-- 引入Springboot的支持 -->
 8     <parent>
 9         <!-- spring-boot-starter-parent就是官方給出的快速構建SpringBoot項目的公共父pom.xml配置文件支持。 -->
10         <groupId>org.springframework.boot</groupId>
11         <artifactId>spring-boot-starter-parent</artifactId>
12         <version>2.3.4.RELEASE</version>
13         <relativePath /> <!-- lookup parent from repository -->
14     </parent>
15 
16     <groupId>com.bie</groupId>
17     <artifactId>springboot-parent</artifactId>
18     <version>0.0.1-SNAPSHOT</version>
19     <packaging>jar</packaging>
20 
21     <name>springboot-parent</name>
22     <url>http://maven.apache.org</url>
23 
24     <properties>
25         <jdk.version>1.8</jdk.version>
26         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
27         <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
28     </properties>
29 
30     <dependencies>
31         <dependency>
32             <groupId>org.springframework.boot</groupId>
33             <artifactId>spring-boot-starter-web</artifactId>
34         </dependency>
35     </dependencies>
36 
37     <build>
38         <finalName>springboot-parent</finalName>
39         <plugins>
40             <!-- <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> 
41                 </plugin> -->
42             <plugin>
43                 <groupId>org.apache.maven.plugins</groupId>
44                 <artifactId>maven-compiler-plugin</artifactId>
45                 <configuration>
46                     <!-- 源代碼使用的開發版本 -->
47                     <source>${jdk.version}</source>
48                     <!-- 需要生成的目標class文件的編譯版本 -->
49                     <target>${jdk.version}</target>
50                     <!-- 字符集編碼設置為utf-8 -->
51                     <encoding>${project.build.sourceEncoding}</encoding>
52                 </configuration>
53             </plugin>
54         </plugins>
55     </build>
56 
57 </project>

本案例,使用的spring-boot-starter-parent就是官方給出的快速構建SpringBoot項目的公共父pom.xml配置文件支持。如果你的項目開發是基於eclipse開發工具的,修改完pom.xml配置文件之后,一定要更新項目(快捷鍵為Alt + F5)。

 

2、在項目中使用SpringBoot,往往會需要引入一個標准的父pom配置(spring-boot-starter-parent),利用這個父pom文件,可以方便地進行核心依賴庫的導入,並且由父pom統一管理所有的開發版本。但在實際的Maven項目開發中,往往會根據自己的需要來自定義屬於自己的父pom,這樣就會造成沖突。為了解決這樣的問題,在SpringBoot里面,用戶也可以直接以依賴管理的形式使用SpringBoot。

 

3、創建一個用於管理父pom的Maven項目springboot-base,如下所示:

注意:這里定義為pom類型的,如下所示:

修改springboot-base項目pom.xml配置文件,如下所示: 

 1 <project xmlns="http://maven.apache.org/POM/4.0.0"
 2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
 4     http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5 
 6     <modelVersion>4.0.0</modelVersion>
 7     <groupId>com.bie</groupId>
 8     <artifactId>springboot-base</artifactId>
 9     <version>0.0.1-SNAPSHOT</version>
10     <!-- 此父工程被定義為pom類型的 -->
11     <packaging>pom</packaging>
12     <name>springboot-base</name>
13     <url>http://maven.apache.org</url>
14 
15     <properties>
16         <jdk.version>1.8</jdk.version>
17         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18         <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
19         <spring-boot-dependencies.version>2.3.4.RELEASE</spring-boot-dependencies.version>
20     </properties>
21 
22     <!-- 在SpringBoot里面,用戶也可以直接以依賴管理的形式使用SpringBoot。 -->
23     <dependencyManagement>
24         <dependencies>
25             <dependency>
26                 <groupId>org.springframework.boot</groupId>
27                 <artifactId>spring-boot-dependencies</artifactId>
28                 <version>${spring-boot-dependencies.version}</version>
29                 <type>pom</type>
30                 <scope>import</scope>
31             </dependency>
32         </dependencies>
33     </dependencyManagement>
34 
35     <build>
36         <finalName>springboot-parent</finalName>
37         <plugins>
38             <!-- 配置編譯插件 -->
39             <plugin>
40                 <groupId>org.apache.maven.plugins</groupId>
41                 <artifactId>maven-compiler-plugin</artifactId>
42                 <configuration>
43                     <!-- 源代碼使用的開發版本 -->
44                     <source>${jdk.version}</source>
45                     <!-- 需要生成的目標class文件的編譯版本 -->
46                     <target>${jdk.version}</target>
47                     <!-- 字符集編碼設置為utf-8 -->
48                     <encoding>${project.build.sourceEncoding}</encoding>
49                 </configuration>
50             </plugin>
51         </plugins>
52     </build>
53 
54 </project>

在父項目springboot-base之中建立一個新的Maven模塊springboot-tentent,如下所示:

修改springboot-tentent項目的pom.xml配置文件,追加要引入的SpringBoot依賴配置,如下所示:

 1 <?xml version="1.0"?>
 2 <project
 3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
 4     http://maven.apache.org/xsd/maven-4.0.0.xsd"
 5     xmlns="http://maven.apache.org/POM/4.0.0"
 6     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 7     <modelVersion>4.0.0</modelVersion>
 8 
 9     <parent>
10         <groupId>com.bie</groupId>
11         <artifactId>springboot-base</artifactId>
12         <version>0.0.1-SNAPSHOT</version>
13     </parent>
14 
15     <!-- 父項目已經指定,這里可以省略 -->
16     <!-- <groupId>com.bie</groupId> -->
17     <artifactId>springboot-tentent</artifactId>
18     <!-- <version>0.0.1-SNAPSHOT</version> -->
19     <name>springboot-tentent</name>
20     <url>http://maven.apache.org</url>
21 
22     <properties>
23         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
24     </properties>
25 
26     <dependencies>
27         <dependency>
28             <groupId>org.springframework.boot</groupId>
29             <artifactId>spring-boot-starter-web</artifactId>
30         </dependency>
31     </dependencies>
32 
33 </project>

如果要修改application.properties配置文件,可以創建一個src/main/resources目錄,如下所示:

此時的Maven創建的springboot的父子工程的項目結構,如下所示:

 

4、SpringBoot程序開發完成之后,需要對程序的功能進行測試,這時需要啟動Spring容器。開發者可以直接利用SpringBoot提供的依賴包來實現控制層方法測試。

在子模塊springboot-tentent新增測試的依賴,如下所示:

 1 <?xml version="1.0"?>
 2 <project
 3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
 4     http://maven.apache.org/xsd/maven-4.0.0.xsd"
 5     xmlns="http://maven.apache.org/POM/4.0.0"
 6     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 7     <modelVersion>4.0.0</modelVersion>
 8 
 9     <parent>
10         <groupId>com.bie</groupId>
11         <artifactId>springboot-base</artifactId>
12         <version>0.0.1-SNAPSHOT</version>
13     </parent>
14 
15     <!-- 父項目已經指定,這里可以省略 -->
16     <!-- <groupId>com.bie</groupId> -->
17     <artifactId>springboot-tentent</artifactId>
18     <!-- <version>0.0.1-SNAPSHOT</version> -->
19     <name>springboot-tentent</name>
20     <url>http://maven.apache.org</url>
21 
22     <properties>
23         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
24     </properties>
25 
26     <dependencies>
27         <dependency>
28             <groupId>org.springframework.boot</groupId>
29             <artifactId>spring-boot-starter-web</artifactId>
30         </dependency>
31         <dependency>
32             <groupId>org.springframework.boot</groupId>
33             <artifactId>spring-boot-starter-test</artifactId>
34             <scope>test</scope>
35         </dependency>
36         <dependency>
37             <groupId>junit</groupId>
38             <artifactId>junit</artifactId>
39             <scope>test</scope>
40         </dependency>
41     </dependencies>
42 
43 </project>

編寫一個測試程序類,如下所示:

 1 package org.springboot.tentent.test;
 2 
 3 import org.junit.Test;
 4 import org.junit.runner.RunWith;
 5 import org.springboot.tentent.controller.SampleController;
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.boot.test.context.SpringBootTest;
 8 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 9 import org.springframework.test.context.web.WebAppConfiguration;
10 
11 @SpringBootTest(classes = SampleController.class) // 定義要測試的Springboot類
12 @RunWith(SpringJUnit4ClassRunner.class) // 使用Junit進行測試
13 @WebAppConfiguration // 進行web應用配置
14 public class SampleControllerTest {
15 
16     @Autowired
17     private SampleController sampleController;
18 
19     @Test // 使用Junit進行測試
20     public void testPrint() {
21         System.out.println(this.sampleController.hello());
22     }
23 
24 }

當右擊方法,run as -> Junit Test的時候,報如下所示的錯誤:

 1 java.lang.NoClassDefFoundError: org/junit/platform/launcher/core/LauncherFactory
 2     at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.<init>(JUnit5TestLoader.java:31)
 3     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
 4     at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
 5     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
 6     at java.lang.reflect.Constructor.newInstance(Unknown Source)
 7     at java.lang.Class.newInstance(Unknown Source)
 8     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.createRawTestLoader(RemoteTestRunner.java:367)
 9     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.createLoader(RemoteTestRunner.java:362)
10     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.defaultInit(RemoteTestRunner.java:306)
11     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.init(RemoteTestRunner.java:221)
12     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:205)
13 Caused by: java.lang.ClassNotFoundException: org.junit.platform.launcher.core.LauncherFactory
14     at java.net.URLClassLoader.findClass(Unknown Source)
15     at java.lang.ClassLoader.loadClass(Unknown Source)
16     at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
17     at java.lang.ClassLoader.loadClass(Unknown Source)
18     ... 11 more

解決方法,參考:https://blog.csdn.net/weixin_41287260/article/details/90578478

  為項目添加eclipse對應的Junit庫即可,具體步驟如下:在包資源管理器中右鍵單擊您的項目build path,configure build path,libraries-->add libraries-->junit-->添加就好了。

  注意:@EnableAutoConfiguration為整個SpringBoot的啟動注解配置,也就是說,這個注解應該隨着程序的主類一起進行定義。但是該注解有一個前提,就是只能夠掃描在同一程序類包中的配置程序,很明顯其功能是不足的。

  對於控制器程序類,由於在項目中有許多的控制器,那么最好將這些類統一保存在一個包中(如將所有的控制器程序類保存在org.springboot.tentent.controller中,這是org.springboot.tentent的子包),在這樣的環境下建議開發者使用@SpringBootApplication注解實現啟動配置。

  請嚴格遵守SpringBoot的自動配置約束,在SpringBoot開發過程中,為了簡化開發配置,往往會在SpringBoot啟動類下創建若干個子包,這樣子包中的注解就都可以自動掃描到(@EnableAutoConfiguration注解不支持此功能),並且可以實現依賴關系的自動配置。如果不在指定的子包中,程序啟動類就需要配置@ComponentScan注解設置掃描包。

JUnit Platform是提供了運行(測試框架)環境的平台,JUnit Jupiter 是新的Junit5(子項目提供了一個基於平台測試運行Jupiter的測試引擎),JUnit Vintage提供了Junit3/4的測試引擎(應該是向前兼容的意思)。 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM