spring-boot-starter-parent作用及测试
spring-boot-starter-parent,Spring Boot应用的父级依赖;
一.简介:
提供了springboot统一的依赖管理和插件管理;
主要的依赖其实是继承了spring-boot-dependencies(通过标签dependencyManagement管理依赖声明),本质是继承了它然后扩展了插件配置;
spring-boot-starter-parent具体作用:
1.引入父pom里面的依赖时无须指定版本;
2.java版本,项目编码格式,资源引用描述符已经设置好
3.插件管理
a.封装了配置文件的过滤规则
b.封装了打可执行jar、war的配置
c.封装了插件的版本信息
d.封装了日期格式
e.引入了eclipse和IDEA相关依赖简化了配置,达到开箱即用等
。。。
这些从pom里面可以看到:(2.0.6)
<modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>2.0.6.RELEASE</version><relativePath>../../spring-boot-dependencies</relativePath></parent><artifactId>spring-boot-starter-parent</artifactId><packaging>pom</packaging>-- java版本,项目编码格式,资源引用描述符<properties><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><resource.delimiter>@</resource.delimiter><maven.compiler.source>${java.version}</maven.compiler.source><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.target>${java.version}</maven.compiler.target></properties>--插件管理1.封装了配置文件的过滤规则2.封装了打可执行jar、war的配置3.封装了插件的版本信息4.引入了eclipse和IDEA相关依赖简化了配置,达到开箱即用等
<build><resources><resource><filtering>true</filtering><directory>${basedir}/src/main/resources</directory><includes><include>**/application*.yml</include><include>**/application*.yaml</include><include>**/application*.properties</include></includes></resource><resource><directory>${basedir}/src/main/resources</directory><excludes><exclude>**/application*.yml</exclude><exclude>**/application*.yaml</exclude><exclude>**/application*.properties</exclude></excludes></resource></resources><pluginManagement><plugins><plugin><groupId>org.jetbrains.kotlin</groupId><artifactId>kotlin-maven-plugin</artifactId><version>${kotlin.version}</version><executions><execution><id>compile</id><phase>compile</phase><goals><goal>compile</goal></goals></execution><execution><id>test-compile</id><phase>test-compile</phase><goals><goal>test-compile</goal></goals></execution></executions><configuration><jvmTarget>${java.version}</jvmTarget><javaParameters>true</javaParameters></configuration></plugin><plugin><artifactId>maven-compiler-plugin</artifactId><configuration><parameters>true</parameters></configuration></plugin><plugin><artifactId>maven-failsafe-plugin</artifactId><executions><execution><goals><goal>integration-test</goal><goal>verify</goal></goals></execution></executions><configuration><classesDirectory>${project.build.outputDirectory}</classesDirectory></configuration></plugin><plugin><artifactId>maven-jar-plugin</artifactId><configuration><archive><manifest><mainClass>${start-class}</mainClass><addDefaultImplementationEntries>true</addDefaultImplementationEntries></manifest></archive></configuration></plugin><plugin><artifactId>maven-war-plugin</artifactId><configuration><archive><manifest><mainClass>${start-class}</mainClass><addDefaultImplementationEntries>true</addDefaultImplementationEntries></manifest></archive></configuration></plugin><plugin><groupId>org.codehaus.mojo</groupId><artifactId>exec-maven-plugin</artifactId><configuration><mainClass>${start-class}</mainClass></configuration></plugin><plugin><artifactId>maven-resources-plugin</artifactId><configuration><delimiters><delimiter>${resource.delimiter}</delimiter></delimiters><useDefaultDelimiters>false</useDefaultDelimiters></configuration></plugin><plugin><groupId>pl.project13.maven</groupId><artifactId>git-commit-id-plugin</artifactId><executions><execution><goals><goal>revision</goal></goals></execution></executions><configuration><verbose>true</verbose><dateFormat>yyyy-MM-dd'T'HH:mm:ssZ</dateFormat><generateGitPropertiesFile>true</generateGitPropertiesFile><generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename></configuration></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><executions><execution><goals><goal>repackage</goal></goals></execution></executions><configuration><mainClass>${start-class}</mainClass></configuration></plugin><plugin><artifactId>maven-shade-plugin</artifactId><executions><execution><phase>package</phase><goals><goal>shade</goal></goals><configuration><transformers><transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"><resource>META-INF/spring.handlers</resource></transformer><transformer implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer"><resource>META-INF/spring.factories</resource></transformer><transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"><resource>META-INF/spring.schemas</resource></transformer><transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/><transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"><mainClass>${start-class}</mainClass></transformer></transformers></configuration></execution></executions><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>2.0.6.RELEASE</version></dependency></dependencies><configuration><keepDependenciesWithProvidedScope>true</keepDependenciesWithProvidedScope><createDependencyReducedPom>true</createDependencyReducedPom><filters><filter><artifact>*:*</artifact><excludes><exclude>META-INF/*.SF</exclude><exclude>META-INF/*.DSA</exclude><exclude>META-INF/*.RSA</exclude></excludes></filter></filters></configuration></plugin><plugin><groupId>org.eclipse.m2e</groupId><artifactId>lifecycle-mapping</artifactId><version>1.0.0</version><configuration><lifecycleMappingMetadata><pluginExecutions><pluginExecution><pluginExecutionFilter><groupId>org.codehaus.mojo</groupId><artifactId>flatten-maven-plugin</artifactId><versionRange>[1.0.0,)</versionRange><goals><goal>flatten</goal></goals></pluginExecutionFilter><action><ignore/></action></pluginExecution><pluginExecution><pluginExecutionFilter><groupId>org.apache.maven.plugins</groupId><artifactId>maven-checkstyle-plugin</artifactId><versionRange>[3.0.0,)</versionRange><goals><goal>check</goal></goals></pluginExecutionFilter><action><ignore/></action></pluginExecution></pluginExecutions></lifecycleMappingMetadata></configuration></plugin></plugins></pluginManagement></build>
二.怎么体现上面的特点? 失去他你才懂得。。。
那么这里分2种情况:
2.1:不继承spring-boot-starter-parent,所有的依赖自已写;
<?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.ysy</groupId><artifactId>spring-boot-01</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.0.6.RELEASE</version></dependency></dependencies><!-- 使用插件将应用打包成可执行的jar--><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>2.0.6.RELEASE</version></plugin></plugins></build>
</project>
虽然你本地可以正常起来跑起来;(依赖都有,当然可以跑起来)
但是打包会报错;使用mvn clean package指令或者Idea操作;
情况1.打war
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war) on project spring-boot-01: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode) -> [Help 1]
[ERROR]
在上面的pom中,我并没有引入maven-war-plugin依赖;那它怎么来的呢?
看执行日志:
[INFO] -----------------------< com.ysy:spring-boot-01 >-----------------------
[INFO] Building spring-boot-01 1.0-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-war-plugin/2.2/maven-war-plugin-2.2.pom
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-war-plugin/2.2/maven-war-plugin-2.2.pom (6.5 kB at 1.7 kB/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-war-plugin/2.2/maven-war-plugin-2.2.jar
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-war-plugin/2.2/maven-war-plugin-2.2.jar (79 kB at 53 kB/s)
[INFO]
它会自动从远程仓库下载默认依赖;
默认的war版本2.2是必须要求web应用存在web.xml的,于是报错;
在2.0.6的spring-boot-dependencies中可以知道;里面的maven-war-plugin是3.1.0;
spring-boot-maven-plugin是2.0.6;如下
<maven-war-plugin.version>3.1.0</maven-war-plugin.version><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>2.0.6.RELEASE</version></plugin>
为了解决上面的报错;
那我在pom里面指定maven-war-plugin版本吧;
<plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-war-plugin</artifactId><version>3.1.0</version></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>2.0.6.RELEASE</version></plugin></plugins>
然后再执行,嗯,打包成功了;
开始跑跑:
可以看到,启动失败了,没有找到启动类;
原来:还需要对spring-boot-maven-plugin配置一下:
<plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>2.0.6.RELEASE</version><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin>
再次打包成功启动;
而这些配置,在spring-boot-starter-parent里面全部都帮你配好了,让你远离这些坑。。
情况2.打jar,同理,也需要配置repackage才能变成可执行jar
<plugins><!--<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-war-plugin</artifactId><version>3.1.0</version></plugin>--><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>2.0.6.RELEASE</version><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins>
2.2:不继承spring-boot-starter-parent,但是继承spring-boot-dependencies
这个操作看似可以的,但是你只是继承了spring-boot-dependencies的依赖声明,但是插件管理还是需要你自已写的,如你打包时还是需要在pom声明repackage才能运行的;
而spring-boot-starter-parent已经帮你写好了插件管理及其他功能;
所以你要自已考虑清楚,自已是否需要扩展重新写自已的parent;
其中,如果你只想使用spring-boot-dependencies的依赖,可以用
dependencyManagement引入如下; 也可以你自已写重写一套依赖;
<dependencyManagement><dependencies><dependency><!-- Import dependency management from Spring Boot --><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>2.2.4.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies>
</dependencyManagement>
补充知识:父pom在被继承的时候,
dependencies与dependencyManagement的区别
1.dependencies即使在子项目中不写该依赖项,那么子项目仍然会从父项目中继承该依赖项(全部继承) ,管理的是具体的依赖关系;
2.dependencyManagement里只是声明依赖,并不实现引入,因此子项目需要显示的声明需要用的依赖。如果不在子项目中声明依赖,是不会从父项目中继承下来的;只有在子项目中写了该依赖项,并且没有指定具体版本,才会从父项目中继承该项,并且version和scope都读取自父pom;另外如果子项目中指定了版本号,那么会使用子项目中指定的jar版本。
,管理的是依赖的声明;
spring-boot-starter-parent作用及测试相关推荐
- Spring Boot中parent标签的作用
在Spring Boot的官方示例中,都是让我们继承一个spring的 spring-boot-starter-parent作为parent标签 <parent><groupId&g ...
- 手把手教你定制标准 Spring Boot starter
点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 写在前面 我们每次构建一个 Spring 应用程序时,我 ...
- 自定义 Spring Boot Starter
一.引言 什么是Spring Boot Starter呢?我们直接来看看官网是怎么介绍的吧. Starters are a set of convenient dependency descripto ...
- 自定义依赖注解无效_最详细的自定义Spring Boot Starter开发教程
1.前言 随着Spring的日渐臃肿,为了简化配置.开箱即用.快速集成,Spring Boot 横空出世.目前已经成为 Java 目前最火热的框架了.平常我们用Spring Boot开发web应用.S ...
- 一个项目有两个pom_实现一个Spring Boot Starter超简单,读 Starter 源码也不在话下...
Spring Boot 对比 Spring MVC 最大的优点就是使用简单,约定大于配置.不会像之前用 Spring MVC 的时候,时不时被 xml 配置文件搞的晕头转向,冷不防还因为 xml 配置 ...
- 一个简易上手的短信服务Spring Boot Starter,连傻瓜都会!
作 者:jackieonway 来 源:jianshu.com/u/36510c75d37c 短信服务在用户注册.登录.找回密码等相关操作中,可以让用户使用更加便捷,越来越多的公司都采用短信验证的方式 ...
- 快速开发一个自定义 Spring Boot Starter ,希望你也会
来源:http://t.cn/Ai9li9fC 众所周知,Spring Boot由众多Starter组成,随着版本的推移Starter家族成员也与日俱增.在传统Maven项目中通常将一些层.组件拆分为 ...
- Spring boot starter
1:Spring boot starter及项目中的类似运用 1:Spring boot starter的两种方式 引入pom文件,自动管理jar版本 根据spring.factories配置文件,加 ...
- Spring Boot微服务的黑匣子测试是如此简单
当我需要进行原型设计,概念验证或在空闲时间使用一些新技术时,开始新项目对于Maven来说总是有点烦人. 不得不说,设置Maven项目并不难,您可以使用Maven原型. 但是原型通常是过时的. 谁想玩旧 ...
- 《SpringCloud超级入门》Spring Boot Starter的介绍及使用《七》
目录 Spring Boot Starter项目创建 自动创建客户端 使用 Starter 使用注解开启 Starter 自动构建 使用配置开启 Starter 自动构建 配置 Starter 内容提 ...
最新文章
- 《当程序员的那些狗日日子》(五)工作中,工作外
- ActiveMQ 入门
- HTML之垂直居中问题
- Hadoop—如何查看HDFS默认的ns命名空间和所有命名空间列表
- Java面向对象之构造方法模拟捕鱼达人游戏【附源码】
- Java并发(二)——ThreadLocal
- php数据类型之自动转换和强制转换
- 运动重构(SFM)学习笔记一
- 射击类游戏html代码,超简单射击游戏
- 【数学建模】微分方程求解 | dsolve函数 | ode45函数
- 跟熊浩学沟通30讲读后感_跟熊浩学沟通
- 漫谈程序员系列 群星闪耀的黄金时代
- 戴尔游匣7559更换C面和D面以及升级内存硬盘教程
- 联想笔记本修复计算机还原系统失败,联想电脑重置电脑失败怎么办
- 【Java中实现微信公众号模板消息推送】
- C语言---集合---合取-析取-异或-条件语句-双条件语句
- SQL-Server 零基础入门教程[上]
- API 低代码开发:接口大师,一套开发、管理和提供接口的产品框架
- 关联自己的应用程序到系统右键菜单
- 《信息安全保障》一3.2 信息安全管理方法与实施
热门文章
- web buuctf [SUCTF 2019]Pythonginx1
- 浅析:S2B2C模式
- VC++在局域网基于winpcap实现QQ号码IP嗅探
- re:正则表达式,字符串处理的杀手锏
- 精心分享7个办公软件,每一款都好用到爆
- 一只能看懂表格图片的数据助手
- OpenCV4-C++读图、显示图
- 微信小程序网络请求异常怎么办_微信小程序打开提示“网络异常,请检查网络状态”的解决方法...
- MFC CTreeCtrl节点重命名
- 多WAN口宽带路由器到底几个口才算合理(转)