背景
分布式项目、微服务项目一般都会引用许多公共依赖,每次maven打出来的jar包上百M,不利于运维工作、可以在打包时隔离项目本身的jar和依赖的公用jar包,这样项目本身包可以做到很精简。
本文主要把项目依赖的包和项目的配置文件在打包时导出到本身springboot 的jar包 外。
本文项目中导入依赖用到了两种方式,一是直接通过maven库,二是通过导入本地第三方jar
配置
全部配置
我在系统根目录下创建了一个boot的文件夹,mvn打包之后会在boot下创建名称为lib、boot-demo的两个文件夹,lib用来存放所有依赖的jar,boot-demo用来存放springboot的jar和application.properties配置文件?
用到了
maven-compiler-plugin:负责编译源码
spring-boot-maven-plugin :负责编译springbootjar
maven-jar-plugin:负责把项目依赖的jar写到MANIFEST.MF文件
maven-dependency-plugin:负责导出项目依赖的jar
maven-resources-plugin:负责导出项目配置文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.luck</groupId>
<artifactId>boot-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<name>boot-demo</name>
<properties>
<global-jar-output>/boot</global-jar-output>
<!-- 注意点 . 和 空格 -->
<system-jar-classpath>. ../lib/mytest-1.0.jar</system-jar-classpath>
<java.home>${env.JAVA_HOME}</java.home>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.3.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 直接引入SDK,对应上面配置的properties -->
<dependency>
<groupId>com.luck</groupId>
<artifactId>mytest</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/lib/otherjar.jar</systemPath>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
<!-- 忽略配置文件 -->
<excludes>
<exclude>*.properties</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/${package.environment}</directory>
<includes>
<include>**/*.*</include>
</includes>
<excludes>
<exclude>*.properties</exclude>
</excludes>
<targetPath>${project.basedir}/target/classes</targetPath>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<!-- 编译源码 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF8</encoding>
<compilerArguments>
<bootclasspath>${java.home}\jre\lib\rt.jar;${java.home}\jre\lib\jce.jar</bootclasspath>
</compilerArguments>
</configuration>
</plugin>
<!-- 导出springboot的jar -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 启动类 -->
<mainClass>com.luck.MainApplication</mainClass>
<!-- 打包方式,zip说明jar以PropertiesLauncher启动 -->
<layout>ZIP</layout>
<!-- 忽略所有jar,在springboot包中不包含任何jar依赖 -->
<includes>
<include>
<groupId>nojar</groupId>
<artifactId>nojar</artifactId>
</include>
</includes>
<!-- springboot jar导出目录 -->
<outputDirectory>${global-jar-output}/${project.artifactId}</outputDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<!-- META-INF/MANIFEST.MF中添加Class-Path: {项目依赖的所有jar名称}-->
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>../lib/</classpathPrefix>
<useUniqueVersions>false</useUniqueVersions>
</manifest>
<!-- META-INF/MANIFEST.MF中添加Class-Path: {第三方jar名称,上方properties的配置}-->
<manifestEntries>
<Class-Path>${system-jar-classpath}</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
<!-- 导出项目依赖的jar到/boot/lib目录下 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-lib</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
</execution>
</executions>
<configuration>
<outputDirectory>${global-jar-output}/lib</outputDirectory>
<excludeTransitive>false</excludeTransitive>
<stripVersion>false</stripVersion>
<silent>false</silent>
</configuration>
</plugin>
<!-- 导出项目配置文件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-config</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>*.properties</include>
</includes>
</resource>
</resources>
<outputDirectory>${global-jar-output}/${project.artifactId}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>test</id>
<properties>
<package.environment>res-test</package.environment>
</properties>
</profile>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<package.environment>resources</package.environment>
</properties>
</profile>
</profiles>
</project>
生成?
?
?
?
?
?
?
Java中读取application.properties文件
java读取外部配置文件需要用到
org.springframework.core.io.ClassPathResource.ClassPathResource这个类
InputStream in = new ClassPathResource("application.properties").getInputStream()
参考文章
SpringBoot 部署 Jar 文件,瘦身优化指南 ! - 知乎
|