首页 > 其他分享 >SpringBoot项目的POM文件分析

SpringBoot项目的POM文件分析

时间:2023-10-21 15:13:44浏览次数:38  
标签:文件 SpringBoot parent plugin spring boot jar maven POM

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 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.3.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.cunyu</groupId>
    <artifactId>springboot-03</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-03</name>
    <description>spring boot - 03</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.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>

主要有四个部分:

  • 项目元数据

即 Maven 项目的基本元素,包括 groupId、artifactId、version、name、description 等;

  • parent

继承 spring-boot-starter-parent 的依赖管理,控制版本与打包等等内容;

  • dependencies

项目具体依赖,默认包含 spring-boot-starter-web,用于实现HTTP接口(该依赖中包含了Spring MVC);spring-boot-starter-test用于编写单元测试的依赖包。后续开发中,主要就是在这里添加各种依赖。

  • build

构建配置部分,默认使用 spring-boot-maven-plugin,配合 spring-boot-starter-parent 可以把 Spring Boot 应用打包成 jar 来直接运行。

Spring Boot中parent标签的作用

在Spring Boot的官方示例中,都是让我们继承一个spring的 spring-boot-starter-parent作为parent标签。

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.6.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

这个parent标签有一个最常用的功能,就是定义子标签的依赖版本号,然而它的功能却不仅仅如此。

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>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.1.6.RELEASE</version>
    <relativePath>../../spring-boot-dependencies</relativePath>
  </parent>
  <artifactId>spring-boot-starter-parent</artifactId>
  <packaging>pom</packaging>
  <name>Spring Boot Starter Parent</name>
  <description>Parent pom providing dependency and plugin management for applications
		built with Maven</description>
  <url>https://projects.spring.io/spring-boot/#/spring-boot-starter-parent</url>
  <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>
  <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>
              <id>repackage</id>
              <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.1.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>
</project>

parent基本功能

从上面的源码中我们可以看到parent主要有如下功能:

  • 定义了一个父标签spring-boot-dependencies,这个里边定义了依赖的版本,也正是因为继承了这个依赖,所以我们在写依赖时才不需要写版本号
  • 使用默认编码格式为 UTF-8
  • 定义了 Java 编译版本为 1.8
  • 定义了针对 application.properties 和 application.yml 的资源过滤,包括通过 profile 定义的不同环境的配置文件,例如 application-dev.properties 和 application-dev.yml
  • 执行打包操作的配置
  • 自动化的资源过滤
  • 自动化的插件配置

使用自定义的parent

当我们需要使用自己定义的 parent ,我们的 Spring Boot 项目要继承自公司内部的 parent ,这个时候可以这么做。

一个简单的办法就是我们自行定义dependencyManagement 节点,然后在里边定义好版本号,再接下来在引用依赖时也就不用写版本号了,像下面这样:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.1.6.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

但这种方式只能解决依赖的版本号问题,如果我们仍然需要关于打包的插件、编译的 JDK 版本、文件的编码格式等等这些配置,在没有 parent 的时候,这些统统要自己去配置。

springboot maven 打包插件

springboot项目默认生成的可执行jar,为什么不能作为依赖导入其他项目?

Spring Boot 项目默认的插件是spring-boot-maven-plugin

<plugin>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

使用上面的插件会将项目A打包成一个可执行的jar 和一个.original文件如下:

jar图片

可以看到,可执行的jar包比另一个original文件大多了,分别解压这两个文件,original文件去掉后缀重命名可解压:可执行jar打开如下:

可执行jar解压后图片

original文件重命名后打开如下:

original文件

从这两张图片对比可以得知,可执行jar将项目的依赖都放进去了且目录和传统的jar包目录不同,多了一层BOOT-INF,因此这个jar包不能直接作为其他项目的依赖,不可执行jar没有把依赖打进去,和传统的jar包目录一致,因此去掉它的.original后缀可以作为其他项目的依赖。

有没有办法可以一次性获得可执行jar和依赖jar呢?

官方当然也准备了,使用下面的插件即可:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <classifier>exec</classifier>
    </configuration>
</plugin>

打包结果:

在这里插入图片描述

1)遇到的问题

将上述A项目的依赖jar引入另一个项目B时,启动项目依旧报找不到相关依赖?

2)原因

因为上述的依赖jar 并没有把A项目依赖的其他jar包给引进来,只是把A项目的代码给打成了jar包。

3)解决办法

将上述的A项目依赖jar安装到本地maven仓库或者公司的私服,然后在B项目通过maven引入,因为通过maven引入的方式,项目会自动去寻找依赖jar本身所需要引入的依赖。(推荐)将上述的A项目依赖jar直接放到B项目工程下,即springboot引入第三方jar的方式,这样引入后,还需要单独把A项目中用到的依赖,在B项目的pom文件单独再引入一下。采用下面的打包插件,在打A项目的依赖jar时,就把A项目需要的依赖全部一起打进去。

将项目中所有依赖都打进依赖jar的打包插件:

<plugin>
     <artifactId>maven-assembly-plugin</artifactId>
     <configuration>
         <appendAssemblyId>false</appendAssemblyId>
         <descriptorRefs>
             <descriptorRef>jar-with-dependencies</descriptorRef>
         </descriptorRefs>
         <archive>
             <manifest>
                 <!-- 此处指定 main 方法入口的class -->
                 <mainClass>com.libii.sso.LibiiSpringBootStartApplication</mainClass>
             </manifest>
         </archive>
     </configuration>
     <executions>
         <execution>
             <id>make-assembly</id>
             <phase>package</phase>
             <goals>
                 <goal>single</goal>
             </goals>
         </execution>
     </executions>
</plugin>

打包结果:

包含所有依赖的jar图片

标签:文件,SpringBoot,parent,plugin,spring,boot,jar,maven,POM
From: https://www.cnblogs.com/xfeiyun/p/17778875.html

相关文章

  • 深入理解linux文件
    一、linux的文件存储假如有一个用户在linux系统中编辑了一个文件,编辑完内容后,关闭编辑器时会问用户改如何命名这个文件,设置完名称之后会选择一个目录将该文件保存到指定目录下,在这个例子中包含了linux系统中与文件相关的三个组成部分数据:就是文件的内容,保存在一个叫data(数据块)......
  • js 大文件切片,中止上传,上传进度,断点续传
    大文件切片上传背景介绍:当涉及大文件上传时,一种有效的方法是将大文件分割成小切片并逐个上传。这种技术不仅可以减轻服务器的负担,还可以避免上传过程中的中断和内存问题。本文将介绍如何使用JavaScript实现大文件切片上传,并解释如何处理断点续传、并发控制以及上传取消等问题,用到......
  • 文件描述符fd
    目录在tcp的服务器端,有两类文件描述符监听的文件描述符只需要有一个不负责和客户端通信,负责检测客户端的连接请求,检测到之后调用accept就可以建立新的连接通信的文件描述符负责和建立连接的客户端通信如果有N个客户端和服务器建立了新的连接,通信的文件描述符就......
  • springboot 配置文件元数据
    springboot配置文件元数据1.spring-configuration-metadata.json{"groups":[{"name":"management.appoptics.metrics.export","type":"org.springframework.boot.actuate.autoconfigure.metrics.export.app......
  • docker cp 命令 - 宿主机与容器互相拷贝文件
    一、从容器拷贝文件到宿主机命令格式:$dockercp<containder-id>:/path/host/path例子:$dockercpc9b7f17d43e9:/opt/hello.txt/home/hello.txt二、从宿主机拷贝文件到容器命令格式:$dockercp/host/path<containder-id>:/path例子:$dockercp/home/hel......
  • SpringBoot中启用虚拟线程
    1.什么是虚拟线程虚拟线程是JDK21版本正式发布的一个新特性。虚拟线程和平台线程主要区别在于,虚拟线程在运行周期内不依赖操作系统线程:它们与硬件脱钩,因此被称为“虚拟”。这种解耦是由JVM提供的抽象层赋予的。虚拟线程的运行成本远低于平台线程。它们消耗的内存要少得多。这就......
  • 详解vue大文件视频切片上传的处理方法
    前端上传大文件、视频的时候会出现超时、过大、很慢等情况,为了解决这一问题,跟后端配合做了一个切片的功能,接下来就详细的给大家介绍一下vue大文件视频切片上传的处理方法,需要的朋友可以参考下 前端上传大文件、视频的时候会出现超时、过大、很慢等情况,为了解决这一问题,跟......
  • makefile学习记录 :一个工程里有多个makefile 如何make根目录下的makefile 调用子目录
    注:本文个人学习记录目的:一个工程里有多个makefile如何make根目录下的makefile调用子目录下的makefile,编译所有.c文件如图所示目录结构,根目录server:makefile;子目录so:makefile  根目录makefile:GCC=gccAPP=server ALL_C=$(wildcard./*.c)C_OBJ=$(notdir$......
  • java基础漏洞学习----文件操作漏洞
    java基础漏洞学习----文件操作漏洞前置基础知识https://www.cnblogs.com/thebeastofwar/p/17760812.html文件上传漏洞文件上传的方式1.通过文件流index.jsp<%@pagelanguage="java"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%><!DOC......
  • SpringBoot Vue3打造企业级一体化SaaS系统[最新版完结]
    点击下载:SpringBoot+Vue3打造企业级一体化SaaS系统     提取码:3ixbSpringBoot和Vue3是目前十分盛行的JavaWeb开发技术栈。SpringBoot能够快速构建Web应用程序,并提供许多有用的功用,如自动配置、快速开发、高效性能、易于部署等。Vue3是一种盛行的前端框架,它能够协助开发......