springboot 打包插件

该段配置主要描述了使用Maven进行项目打包的过程,包括设置打包名称、处理资源文件、使用SpringBootMaven插件进行重新打包、配置jar插件添加类路径、依赖插件复制依赖库以及指定Java源目标版本等步骤。主要目的是确保程序能够正确运行并包含所有必要的依赖。
<build>
		<!-- 打包出来的名字 上面有name标签 -->
		<finalName>${name}</finalName>
		<!-- 把所有的xml文件,打包到相应位置 -->
		<resources>
			<resource>
				<directory>src/main/resources</directory>
				<includes>
					<include>**/*.*</include>
				</includes>
				<filtering>false</filtering>
				<!--这里是false,用true会报 数据库连接 错误 -->
			</resource>
		</resources>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<version>2.0.1.RELEASE</version>
				<configuration>
					<layout>ZIP</layout>
					<includes>
						<include>
							<groupId>non-exists</groupId>
							<artifactId>non-exists</artifactId>
						</include>
					</includes>
				</configuration>
				<executions>
					<execution>
						<goals>
							<goal>repackage</goal>
						</goals>
						<configuration>
							<classifier>classes</classifier>
							<attach>false</attach>
						</configuration>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<configuration>
					<archive>
						<manifest>
							<addClasspath>true</addClasspath>
							<classpathPrefix>lib/</classpathPrefix>
							<!-- main主入口 -->
							<mainClass>com.jmx.DicomServerApplication</mainClass>
							<!-- 打包不带时间戳 -->
							<useUniqueVersions>false</useUniqueVersions>
						</manifest>
					</archive>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-dependency-plugin</artifactId>
				<executions>
					<execution>
						<id>copy-dependencies</id>
						<phase>prepare-package</phase>
						<goals>
							<goal>copy-dependencies</goal>
						</goals>
						<configuration>
							<outputDirectory>${project.build.directory}/lib</outputDirectory>
							<overWriteReleases>false</overWriteReleases>
							<overWriteSnapshots>false</overWriteSnapshots>
							<overWriteIfNewer>true</overWriteIfNewer>
						</configuration>
					</execution>
				</executions>
			</plugin>

			<!-- 将项目下lib包中的jar打包 -->
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>UTF-8</encoding>
					<compilerArguments>
						<extdirs>${project.basedir}/lib</extdirs>
					</compilerArguments>
				</configuration>
			</plugin>
		</plugins>
	</build>
### Spring Boot 3.5 打包插件配置方法 Spring Boot 提供了强大的打包功能,允许开发人员通过 Maven 或 Gradle 插件将应用程序打包为可执行的 JAR 或 WAR 文件。以下是针对 Spring Boot 3.5 的打包插件配置教程和示例。 #### Maven 配置示例 在 Maven 项目中,`spring-boot-maven-plugin` 是用于打包的核心插件。以下是一个典型的 `pom.xml` 配置示例: ```xml <build> <plugins> <!-- Spring Boot Maven 插件 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>3.5.0</version> <!-- 确保版本与 Spring Boot 版本一致 --> <executions> <execution> <goals> <goal>repackage</goal> <!-- 使用 repackage 目标进行打包 --> </goals> </execution> </executions> </plugin> </plugins> </build> ``` 此配置确保生的 JAR 文件是可执行的,并且所有依赖项都包含在 `BOOT-INF/lib/` 目录下[^4]。 #### 打包为 WAR 文件 如果需要将 Spring Boot 应用程序部署到外部 Servlet 容器(如 Tomcat),可以将其打包为 WAR 文件。以下是相关配置: ```xml <packaging>war</packaging> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>3.5.0</version> </plugin> </plugins> </build> <!-- 确保主类继承 SpringBootServletInitializer --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> <!-- 将 Tomcat 作为 provided 依赖 --> </dependency> </dependencies> ``` 主类需要继承 `SpringBootServletInitializer` 并重写 `configure` 方法[^2]: ```java package com.weijie; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; @SpringBootApplication public class HelloWordApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(HelloWordApplication.class); } public static void main(String[] args) { SpringApplication.run(HelloWordApplication.class, args); } } ``` #### Gradle 配置示例 对于使用 Gradle 构建的项目,可以通过 `spring-boot-gradle-plugin` 实现类似功能。以下是一个 Gradle 配置示例: ```gradle plugins { id 'org.springframework.boot' version '3.5.0' // 确保版本匹配 id 'io.spring.dependency-management' version '1.1.0' id 'java' } group = 'com.weijie' version = '0.0.1-SNAPSHOT' sourceCompatibility = '17' repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat' // 如果需要打包为 WAR testImplementation 'org.springframework.boot:spring-boot-starter-test' } tasks.named('test') { useJUnitPlatform() } bootJar { mainClass = 'com.weijie.HelloWordApplication' // 指定主类 } ``` 此配置会生一个可执行的 JAR 文件,其中包含所有必要的依赖项[^3]。 #### MANIFEST.MF 文件内容 打包后,JAR 文件中的 `META-INF/MANIFEST.MF` 文件会包含以下关键信息: ``` Manifest-Version: 1.0 Implementation-Title: your-application-name Implementation-Version: 0.0.1-SNAPSHOT Built-By: your-username Main-Class: org.springframework.boot.loader.JarLauncher Start-Class: com.weijie.HelloWordApplication Spring-Boot-Classes: BOOT-INF/classes/ Spring-Boot-Lib: BOOT-INF/lib/ Created-By: Apache Maven 3.x.x Build-Jdk: 17 ``` 这些元数据确保了 JAR 文件的正确运行时行为[^3]。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值