专业的编程技术博客社区

网站首页 > 博客文章 正文

Springboot相关依赖引用方式及打包分离依赖包

baijin 2024-10-01 07:36:55 博客文章 12 ℃ 0 评论

环境:springboot2.3.10


常规建立springboot项目

依赖

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

通常建立springboot项目时pom.xml文件中的parent都是上面那样

如果项目是个老项目已经有了parent该如何使用呢?通过如下依赖管理。

依赖管理引用

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

接下来就可以根据需要引入springboot相关的包了

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
  </dependency>
</dependencies>

dependency中scope说明

  • compile

默认的依赖范围就是compile,没有scope或者scope为comile的依赖表示编译、运行、测试的时候都需要这个依赖的jar包。

  • provided

表示环境已经提供了,所以打包的时候就不会将scope为provided的依赖打到输出的jar包中。

  • runtime

如果一个依赖的scope为runtime,表示编译的时候不能引用这个依赖的jar包。
还有编译的时候不需要运行的时候需要的jar包?

  • test

scope为test的依赖表示,这个包只是在测试阶段使用,打包的时候这些依赖不会打在输出的jar包中。

  • system

system主要是为了引入一些没有通过maven发布的私有的三方jar包。

  • import

import只能在dependencyManagement的dependency,并且type为pom的依赖中使用。
import主要是为了将依赖管理和依赖定义分开。
例如SpringBoot的dependencyManagement中预定义很多依赖,所以就单独弄了一个spring-boot-dependencies来管理这些依赖。

  • optional

optional是用于控制依赖传递的。
optional设置为true,这个依赖不会被传递,假如在A项目中添加了这个依赖,B项目依赖A,但是B项目不会依赖spring-boot-devtools。

打包分离jar包

pom.xml

<build>
  <plugins>
    <!-- 打jar包的插件 -->
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <classpathPrefix>lib</classpathPrefix>
              <mainClass>com.pack.SpringBootFeignApplication</mainClass>
            </manifest>
            <manifestEntries>
              <Class-Path>./</Class-Path>
            </manifestEntries>
          </archive>
          <excludes>
            <exclude>config/**</exclude>
          </excludes>
        </configuration>
      </plugin>
      <!-- 配置将jar包和外部配置等文件整体打包 -->
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
          <!--jar包名称 -->
          <finalName>${project.artifactId}-${project.version}</finalName>
          <!--描述文件路径 -->
          <descriptors>
            <descriptor>src/main/resources/assembly/assembly.xml</descriptor>
          </descriptors>
        </configuration>
        <executions>
          <execution>
            <id>make-zip</id>
            <!-- 绑定到package生命周期阶段上 -->
            <phase>package</phase>
            <goals>
              <!-- 只运行一次 -->
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
	</build>

在src/main/resources新建assembly/assembly.xml文件

执行打包

mvn clean package -Dmaven.test.skip=true

target下生成了

压缩包,解压:

运行jar包是否正常

java -jar spring-boot-feign-1.0.0.jar

一切正常!!

完毕!!!!

给个关注呗谢谢

SpringBoot配置文件你了解多少?

SpringBoot邮件发送示例

SpringBoot多数据源配置详解

SpringBoot项目查看线上日志

Springboot整合openfeign使用详解

springboot mybatis jpa 实现读写分离

SpringBoot RabbitMQ消息可靠发送与接收

SpringBoot开发自己的Starter

Springboot整合MyBatis复杂查询应用

SpringBoot自定义注解属性支持占位符$「x」

SpringBoot开发案例之奇技淫巧

Springboot自定义消息转换器

Springboot项目中几种跨域的解决方法

Springboot配置文件yml各种复杂对象配置方法

SpringBoot项目中接口限流实现方案

SpringBoot读写分离组件开发详解

SpringBoot开发中几个实用小技巧

Springboot整合ELK日志收集详解步骤

SpringBoot这些常用注解你该知道

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表