SpringBoot - BuildTools

SpringBoot提供了一个spring-boot-maven-plugin, 它可以打包成一个可执行的jar或war包,运行SpringBoot应用程序,产生构造信息,在执行集成测试之前启动SpringBoot应用程序

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

该插件提供了goal如下:

build-image: 将程序使用 buildpack 打包进容器镜像中。

build-info:生成项目的构建信息文件 build-info.properties。根据当前 MavenProject 的内容生成一个 - build-info.properties 文件

help:显示帮助信息。调用mvn spring-boot:help -Ddetail=true -Dgoal=以显示参数详细信息。

repackage:可生成可执行的jar包或war包。插件的核心goal。 默认的 goal,将普通 mvn package 打包成的 jar 重新打包成包含所有程序依赖项的可执行 jar/war 文件,并保留 mvn package 打包的 jar 为 .original 后缀

run:运行 Spring Boot 应用

start:在集成测试阶段,控制生命周期。通常用于集成测试方案中,在 mvn integration-test 阶段管理 Spring Boot 应用的生命周期。

stop:在集成测试阶段,控制生命周期。停止已通过 start 目标启动的应用程序。通常在 integration-- test 完成后调用。

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

使用repackage打包后,这里可以看到生成了两个jar相关文件,其中common.jar是spring-boot-maven-plugin插件重新打包后生成的可执行jar,即可以通过java -jar common.jar命令启动。common.jar.original这个则是mvn package打包的原始jar

通过指定groupId和artifactId排除某个特定的依赖

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                    </exclude>
                </excludes>
            </configuration>
        </plugin>
    </plugins>
</build>

spring-boot-maven-plugin插件配置classifier

在Spring Boot项目中,如果你需要配置spring-boot-maven-plugin插件的classifier属性,通常是为了包含额外的构建输出,例如包含应用程序的源码或者文档。classifier可以让你构建出具有不同分类的构建产物。

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.6.3</version>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                    <configuration>
                        <classifier>exec</classifier>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

在这个例子中,classifier设置为exec,这将导致Maven在构建过程中生成一个附加的包,它将包含可执行的jar,但是它的文件名将带有exec作为分类器。

指定Mainclass类

如果未指定,main将使用找到的第一个包含方法的编译类。

也就是说如果只有一个main方法,那不需要去指定,如果你这个模块存在两个主类,那么就需要在插件中指定具体哪个启动类了。

layout 属性用来指定打成 jar 还是war 文件,可用的值包括:ZIP 、JAR 、WAR、 NONE 。默认JAR

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <mainClass>com.can.mavenplugin.MavenPluginApplication</mainClass>
        <layout>ZIP</layout>
    </configuration>
</plugin>

repackage的参数说明:

类型

默认值

用户属性

源自版本

描述

outputDirectory

File

${project.build.directory}

1.0.0

打包输出目录.

attach

boolean

true

1.4.0

将重新打包的归档文件安装到本地 Maven 存储库或部署到远程存储库中. 当属性被设置为 false 时, 重新打包的文件将不会被安装或部署到存储库. 一般作用于 mvn deploy

classifier

String

1.0.0

添加到重新打包归档文件名中的分类器名称.

embeddedLaunchScript

File

1.3.0

指定嵌入式启动脚本, 默认使用 Spring Boot 提供的脚本.

embeddedLaunchScriptProperties

java.util.

Properties

1.3.0

需要应用在嵌入式启动脚本中的属性.

excludeDevtools

boolean

true

spring-boot.repackage.excludeDevtools

1.3.0

从重新打包的文件中排除 Spring Boot 开发工具包.

excludeGroupIds

String

spring-boot.excludeGroupIds

1.1.0

需要从重新打包的文件中排除的 groupId , 逗号间隔, 全匹配模式.

excludes

List

spring-boot.excludes

1.1.0

需要排除的依赖项集合. 需要明确指定 groupId 和 artifactId , 可选指定 classifier .

executable

boolean

false

1.3.0

通过在 jar 包中, 添加嵌入式启动脚本, 为*nux服务器制作完全可执行 jar 包. 目前有些机器不支持, 且启用了该选择, java -xf可能无法正常解压 jar 包. 建议在非启用不可的情况下启用此选项.

includeSystemScope

boolean

false

1.4.0

包含 system 范围(scope)的依赖项.

includes

List

spring-boot.includes

1.2.0

需要包含的依赖项集合. 与 excludes 格式相似, 功能相反.

layers

Layers

2.3.0

层配置选项, 包含禁用层创建, 排除层工具jar, 提供自定义层配置文件等.

layout

LayoutType

spring-boot.repackage.layout

1.0.0

归档的类型(对应于它内部依赖项的布局方式). 取值有JAR, WAR, ZIP(DIR), NONE. 默认由 pom.xml 中的 **jar

layoutFactory

LayoutFactory

1.5.0

如果没有显式设置布局工厂, 将使用创建可执行归档文件的布局工厂. 可以使用第三方提供的布局实现替代.

mainClass

String

1.0.0

主类的完整类名称. 如果未指定, 将使用找到的第一个包含主方法的编译类.

outputTimestamp

String

${project.build.outputTimestamp}

2.3.0

输出时间, 可用于指定重新打包文件的最后修改时间, 意义不大, 但关键时间还是很有作用, ISO 8601 (yyyy-MM-ddTHH:mm:ss.XXXZ) 格式或秒级时间戳, 例如: 2021-07-25T00:00:00.000Z 或 1627287708 .

requiresUnpack

java.util.List

1.1.0

必须从fat jar中解压缩才能运行的库的列表。它们将在运行时解包。

skip

boolean

false

spring-boot.repackage.skip

1.2.0

跳过本插件执行内容.

通过Maven运行应用程序

该插件包含一个 run 目标(goal), 可用于从命令行启动您的应用程序, 如下所示:

mvn spring-boot:run

可以使用 arguments 参数指定应用程序参数, 如下所示:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <arguments>
            <argument>property1</argument>
            <argument>property2=${my.value}</argument>
        </arguments>
    </configuration>
</plugin>

 

亦可以通过命令行形式, 通过用户属性指定应用程序参数:

mvn spring-boot:run -Dspring-boot.run.arguments="property1 'property2=Hello World'"

默认情况下, 应用程序会运行在fork的子进程中, 通过命令行的形式传入的参数, 对应用程序并不生效. 如果需要自定义应用程序中的部分 jvm 参数, 可以使用参数 jvmArguments 传递, 除此之外, 还可以配置 系统属性 和 系统环境变量 . 示例下如:

<plugins>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <jvmArguments>
                -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005
            </jvmArguments>
        </configuration>
    </plugin>
</plugins>

自定义 系统属性

<project>
    <build>
        <properties>
            <my.value>42</my.value>
        </properties>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <systemPropertyVariables>
                        <property1>test</property1>
                        <property2>${my.value}</property2>
                    </systemPropertyVariables>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

 自定义 系统环境变量

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <environmentVariables>
            <ENV1>5000</ENV1>
            <ENV2>Some Text</ENV2>
            <ENV3/>
            <ENV4></ENV4>
        </environmentVariables>
    </configuration>
</plugin>

其它参数:

类型

默认值

用户属性

源自版本

描述

classesDirectory

File

${project.build.outputDirectory}

1.0.0

包含源存档的目录, 默认为打包输出目录

addResources

boolean

FALSE

spring-boot.run.addResources

1.0.0

当启用 addResources 时, src/main/resources 内的所有资源都将会被添加到应用程序类目录(application classpath)中, 当类目录中出现多个相同资源时, target/classes 中的资源将会被删除(替换). 请考虑添加 spring-boot-devtools 到您的项目中, 因为它提供了此功能以及更多功能.

agents

File[]

spring-boot.run.agents

2.2.0

到agent jars的路径。注意:需要一个forked的进程来使用这个特性。

arguments

String[]

1.0.0

应传递给应用程序的参数

commandlineArguments

String

spring-boot.run.arguments

2.2.3

命令行中应传递给应用程序的参数. 并确保在引号之间包含多个值, 使用空格分隔多个参数. 指定时, 优先于 arguments .

directories

String[]

spring-boot.run.directories

1.0.0

除了应添加到类路径的类目录之外的其他目录

environmentVariables

Map

2.1.0

应用在使用fork进程运行的应用程序中的环境变量列表. 注意: 使用此功能需要确保 fork = true, 反之无效.

excludeGroupIds

String

spring-boot.excludeGroupIds

1.1.0

要排除的 groupId 名称, 逗号分隔列表(完全匹配)

excludes

List

spring-boot.excludes

1.1.0

需要排除的依赖项集合. 需要明确指定 groupId 和 artifactId , 可选指定 classifier .

fork

boolean

TRUE

spring-boot.run.fork

1.2.0

启动fork进程运行应用程序. 注意: 禁用该功能, jvmArguments, systemPropertyVariables, environmentVariables 和 agents 等选项都将被忽略.

includes

List

spring-boot.includes

1.2.0

需要包含的依赖项集合. 与 excludes 格式相似, 功能相反.

jvmArguments

String

spring-boot.run.jvmArguments

1.1.0

JVM 参数

mainClass

String

spring-boot.run.main-class

1.0.0

主类的完整类名称. 如果未指定, 将使用找到的第一个包含主方法的编译类.

noverify

boolean

spring-boot.run.noverify

1.0.0

表示代理需要-noverify的标志。

optimizedLaunch

boolean

TRUE

spring-boot.run.optimizedLaunch

2.2.0

优化 JVM 的启动

profiles

String[]

spring-boot.run.profiles

1.3.0

需要激活的配置文件. 指定 spring.profiles.active 参数的便捷快捷方式. 在命令行上使用逗号分隔, 指定多个配置文件.

skip

boolean

FALSE

spring-boot.run.skip

1.3.2

跳过本插件执行内容.

systemPropertyVariables

Map

2.1.0

要传递给应用程序JVM进程的系统属性变量. 注意: 使用此功能需要确保 fork = true, 反之无效.

useTestClasspath

Boolean

FALSE

spring-boot.run.useTestClasspath

1.3.0

标记以在运行时包含测试类路径

workingDirectory

File

spring-boot.run.workingDirectory

1.5.0

用于应用程序执行的当前工作目录. 如果未指定, 则将使用 basedir 目录. 注意: 使用此功能需要确保 fork = true, 反之无效.

npm error code 1 npm error path D:\springboot-vue-example-master\server_frontend\node_modules\node-sass npm error command failed npm error command C:\WINDOWS\system32\cmd.exe /d /s /c node scripts/build.js npm error Building: D:\node\node.exe D:\springboot-vue-example-master\server_frontend\node_modules\node-gyp\bin\node-gyp.js rebuild --verbose --libsass_ext= --libsass_cflags= --libsass_ldflags= --libsass_library= npm error gyp info it worked if it ends with ok npm error gyp verb cli [ npm error gyp verb cli 'D:\\node\\node.exe', npm error gyp verb cli 'D:\\springboot-vue-example-master\\server_frontend\\node_modules\\node-gyp\\bin\\node-gyp.js', npm error gyp verb cli 'rebuild', npm error gyp verb cli '--verbose', npm error gyp verb cli '--libsass_ext=', npm error gyp verb cli '--libsass_cflags=', npm error gyp verb cli '--libsass_ldflags=', npm error gyp verb cli '--libsass_library=' npm error gyp verb cli ] npm error gyp info using node-gyp@3.8.0 npm error gyp info using node@22.14.0 | win32 | x64 npm error gyp verb command rebuild [] npm error gyp verb command clean [] npm error gyp verb clean removing "build" directory npm error gyp verb command configure [] npm error gyp verb check python checking for Python executable "python2" in the PATH npm error gyp verb `which` failed Error: not found: python2 npm error gyp verb `which` failed at getNotFoundError (D:\springboot-vue-example-master\server_frontend\node_modules\which\which.js:13:12) npm error gyp verb `which` failed at F (D:\springboot-vue-example-master\server_frontend\node_modules\which\which.js:68:19) npm error gyp verb `which` failed at E (D:\springboot-vue-example-master\server_frontend\node_modules\which\which.js:80:29) npm error gyp verb `which` failed at D:\springboot-vue-example-master\server_frontend\node_modules\which\which.js:89:16 npm error gyp verb `which` failed at D:\springboot-vue-example-master\server_frontend\node_modules\isexe\index.js:42:5 npm error
03-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值