使用Maven创建Springboot的父子工程

发布时间:2023-05-16 09:33:58

1、在eclipse开发工具中创建一个新的Maven项目,项目类型为quickstart,如下所示:

使用Maven创建Springboot的父子工程_apache

然后项目类型为quickstart,如下所示:

使用Maven创建Springboot的父子工程_spring_02

使用Maven创建Springboot的父子工程_maven_03

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

使用Maven创建Springboot的父子工程_spring_04

修改pom.添加SpringBoot依赖配置及相关插件的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     <modelVersion>4.0.0</modelVersion> 6  7     <!-- 引入Springboot的支持 --> 8     <parent> 9         <!-- 引入Springboot的支持 --> 8     <parent> 9         <!-- spring-boot-starter-parent是官方给出的快速构建SpringBoot项目的公共父pomm.支持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项目的公共父pomm.支持xml配置文件。如果您的项目开发是基于eclipse开发工具,请修改pomm.xml配置文件后,必须更新项目(快捷键是alt + F5)。

2、在项目中使用SpringBoot通常需要引入标准的父pom配置(spring-boot-starter-parent),使用此父pom文件,可以方便地导入核心依赖库,并由父pom统一管理所有开发版本。但在实际的Maven项目开发中,往往会根据自己的需要来定义自己的父pom,从而造成冲突。为了解决这样的问题,用户也可以直接以依赖管理的形式使用Springbot。

3、为管理父pom创建Maven项目springbot-base

注:这里定义为pom类型,如下所示:

使用Maven创建Springboot的父子工程_spring_06

修改springboot-basepome项目.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     <!-- 在Springbot中,用户也可以直接以依赖管理的形式使用Springbot。 -->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>

在父项目springbot-base中建立一个新的maven模块springbot-tentent,如下所示:

使用Maven创建Springboot的父子工程_spring_07

使用Maven创建Springboot的父子工程_maven_08

使用Maven创建Springboot的父子工程_spring_09

修改springbot-tentent项目的pomm.SpringBoot依赖于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的父子工程_apache_10

此时Maven创建的springboot父子工程的项目结构如下:

使用Maven创建Springboot的父子工程_maven_11

4、SpringBoot程序开发完成后,需要测试程序的功能,然后启动Spring容器。开发人员可以直接使用SpringBoot提供的依赖包来测试控制层。

依赖子模块springbot-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     @Autowired17     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.newinstance(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.LauncherFactory14     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

解决方案,参考:javascript:void(0)

  将eclipse对应的Junit库添加到项目中,具体步骤如下:右键单击包资源管理器中的项目build path,configure build path,libraries-->add libraries-->junit-->只需添加。

@Enableautoconfiguration为整个SpringBoot的启动注释配置,即该注释应与程序的主要类别一起定义。但是,注释的前提是只能扫描同一程序包中的配置程序,显然其功能不足。

@实现SpringBotaplication注释的启动配置。

@ComponentScan注释设置扫描包。

使用Maven创建Springboot的父子工程_spring_12

JUnit Platform是一个提供运行(测试框架)环境的平台,JUnit Jupiter 是新的Junit5(子项目提供了基于平台测试运行Jupiter的测试引擎),JUnit Vintage提供了Junit3/4的测试引擎(应该意味着向前兼容)。

上一篇 java printf、println和print区别 java中print与println区别
下一篇 Springboot的控制层结合@PathVariable的使用

文章素材均来源于网络,如有侵权,请联系管理员删除。

标签: Java教程Java基础Java编程技巧面试题Java面试题