maven打包springboot项目的三种场景

近期开发了一个微服务项目,打包部署的时候遇到过很多问题,现将遇到的问题总结,方便有需要的猿友们借鉴

1、有些公共代码,如缓存,数据源和公共实体类以及其它工具类的封装我们可以打包成工具包,方便其他服务开发时调用,其他服务在pom文件里添加依赖即可使用,这个时候pom.xml里的build如下

<build>
		<resources>
			<resource>
				<directory>src/main/java</directory>
				<includes>
					<include>**/*.properties</include>
					<include>**/*.xml</include>
				</includes>
				<filtering>true</filtering>
			</resource>

			<resource>
				<directory>src/main/resources</directory>
				<excludes>
					<exclude>**/*.properties</exclude>
					<exclude>**/*.xml</exclude>
				</excludes>
				<filtering>false</filtering>
			</resource>
		</resources>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<fork>true</fork>
					<skip>true</skip>
					<executable>
						C:\Program Files\Java\jdk1.8.0_161\bin\javac.exe
					</executable>
				</configuration>
			</plugin>
		</plugins>
	</build>


2、一般我们将服务打成jar包直接丢linux服务器运行即可,这个时候我们pom.xm里的build如下

<build>
		<resources>
			<resource>
				<directory>src/main/java</directory>
				<includes>
					<include>**/*.properties</include>
					<include>**/*.xml</include>
				</includes>
				<filtering>true</filtering>
			</resource>
			<resource>
				<directory>src/main/resources</directory>
				<includes>
					<include>**/*.properties</include>
					<include>**/*.xml</include>
				</includes>
				<filtering>true</filtering>
			</resource>
		</resources>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<fork>true</fork>
					<skip>true</skip>
					<executable>
						C:\Program Files\Java\jdk1.8.0_161\bin\javac.exe
					</executable>
				</configuration>

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


3、上面的打包方式有个缺陷,就是配置文件和第三方依赖包都是打到了jar包里面,如果需要修改配置文件或者替换第三方依赖包是需要重新构建部署的,如果服务较多,每个服务的jar包里都包含了第三方依赖包,根据我做的项目中,去除第三方依赖包的jar文件只有几十上百KB,而包含第三方依赖包的jar文件高达几十上百MB,这样是很不合理的,我们能不能把第三方依赖jar包和项目配置都提取出来统一管理呢,答案是可以的,只需要将pom.xml里的build配置成如下

<build>
		<resources>
			<resource>
				<directory>src/main/java</directory>
				<includes>
					<include>**/*.properties</include>
					<include>**/*.xml</include>
				</includes>
				<filtering>true</filtering>
			</resource>
			<resource>
				<directory>src/main/resources</directory>
				<excludes>
					<exclude>**/*.properties</exclude>
					<exclude>**/*.xml</exclude>
				</excludes>
				<filtering>false</filtering>
			</resource>
		</resources>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<fork>true</fork>
					<skip>true</skip>
					<executable>
						C:/Program Files/Java/jdk1.8.0_161/bin/javac.exe
					</executable>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-jar-plugin</artifactId>
				<configuration>
					<archive>
						<manifest>
							<addClasspath>true</addClasspath>
							<classpathPrefix>lib/</classpathPrefix>
							<useUniqueVersions>false</useUniqueVersions>
							<mainClass>com.xrq.demo.Application</mainClass>
						</manifest>
						<manifestEntries>
							<Class-Path>./</Class-Path>
						</manifestEntries>
					</archive>
					<excludes>
						<exclude>*.properties</exclude>
					</excludes>
					<excludes>
						<exclude>*.properties</exclude>
						<exclude>*.yml</exclude>
						<exclude>*.xml</exclude>
						<exclude>config/**</exclude>
						<exclude>static/**</exclude>
						<exclude>templates/**</exclude>
					</excludes>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-dependency-plugin</artifactId>
				<executions>
					<execution>
						<id>copy</id>
						<phase>package</phase>
						<goals>
							<goal>copy-dependencies</goal>
						</goals>
						<configuration>
							<outputDirectory>
								${project.build.directory}/lib
							</outputDirectory>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>

打包后target目录下的除了jar包还会有个lib目录,第三方依赖包都在这个目录下,配置文件根据自己需求自定义后,启动的时候指定该配置文件即可,命令为

java -jar -Dspring.config.location=/xxxxxx/xxxxxx/application.properties xxxx.jar

其他配置文件也可以实现jar包外统一管理,后面篇章会讲到

初次写博客,如有不对之处,请指正

阅读更多
个人分类: springboot java maven
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

maven打包springboot项目的三种场景

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭