赞
踩
目录
1、添加spring-boot-maven-plugin插件到pom.xml
1、项目配置文件中包含spring-boot-starter-parent
2、添加spring-boot-maven-plugin插件
在使用Spring Boot和Maven的项目中,你可以使用Maven来打包你的项目。Spring Boot项目通常使用Maven插件中的spring-boot-maven-plugin
来执行打包操作
spring-boot-maven-plugin
是Spring Boot项目中用于支持Maven构建的插件。它提供了一些功能,使得将Spring Boot应用程序打包成可执行的JAR或WAR文件变得更加简单。以下是一些常见的用法和配置示例:
spring-boot-maven-plugin
插件到pom.xml
在<build>
部分的<plugins>
中添加spring-boot-maven-plugin
插件:
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
通过配置mainClass
来指定Spring Boot应用程序的主类。
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- <configuration>
- <mainClass>com.example.YourApplicationClass</mainClass>
- </configuration>
- </plugin>
- </plugins>
- </build>
使用<finalName>
元素来指定生成的JAR文件的名称。
- <build>
- <finalName>custom-application-name</finalName>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
通过配置<resources>
元素来包含或排除特定的资源文件。
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- <configuration>
- <resources>
- <resource>
- <directory>src/main/resources</directory>
- <includes>
- <include>**/*.properties</include>
- </includes>
- </resource>
- </resources>
- </configuration>
- </plugin>
- </plugins>
- </build>
通过<dependencies>
元素指定额外的依赖项,这些依赖项会被包含到生成的JAR文件中
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- <configuration>
- <dependencies>
- <dependency>
- <groupId>com.example</groupId>
- <artifactId>extra-library</artifactId>
- <version>1.0.0</version>
- </dependency>
- </dependencies>
- </configuration>
- </plugin>
- </plugins>
- </build>
使用<arguments>
元素来配置运行时的JVM参数
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- <configuration>
- <arguments>
- <argument>--server.port=8081</argument>
- </arguments>
- </configuration>
- </plugin>
- </plugins>
- </build>
通过<executions>
元素来自定义插件执行阶段。
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- <executions>
- <execution>
- <goals>
- <goal>repackage</goal>
- </goals>
- <configuration>
- <!-- 自定义配置 -->
- </configuration>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
spring-boot-starter-parent
在你的pom.xml
文件中,确保你的项目是继承自spring-boot-starter-parent
。这样可以确保使用Spring Boot的合适版本和默认配置。
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.5.5</version> <!-- 使用你的Spring Boot版本 -->
- </parent>
spring-boot-maven-plugin
插件在<build>
部分添加spring-boot-maven-plugin
插件。
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
这个插件会自动将项目打包成可执行的JAR文件。
打开命令行,进入项目根目录,执行以下Maven命令:
mvn clean package
使用以下命令来运行打包后的JAR文件:
java -jar your-project.jar
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。