分布式项目、微服务项目一般都会引用许多公共依赖,每次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.0modelVersion>
-
- <groupId>com.luckgroupId>
- <artifactId>boot-demoartifactId>
- <version>1.0-SNAPSHOTversion>
-
- <name>boot-demoname>
-
- <properties>
- <global-jar-output>/bootglobal-jar-output>
-
- <system-jar-classpath>. ../lib/mytest-1.0.jarsystem-jar-classpath>
- <java.home>${env.JAVA_HOME}java.home>
- <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
- <java.version>1.8java.version>
- properties>
-
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-dependenciesartifactId>
- <version>2.3.0.RELEASEversion>
- <type>pomtype>
- <scope>importscope>
- dependency>
- dependencies>
- dependencyManagement>
-
- <dependencies>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
-
- <dependency>
- <groupId>com.luckgroupId>
- <artifactId>mytestartifactId>
- <version>1.0version>
- <scope>systemscope>
- <systemPath>${basedir}/lib/otherjar.jarsystemPath>
- dependency>
- dependencies>
- <build>
- <resources>
- <resource>
- <directory>src/main/javadirectory>
- <includes>
- <include>**/*.xmlinclude>
- includes>
- resource>
- <resource>
- <directory>src/main/resourcesdirectory>
- <includes>
- <include>**/*.*include>
- includes>
-
- <excludes>
- <exclude>*.propertiesexclude>
- excludes>
- resource>
- <resource>
- <directory>src/main/${package.environment}directory>
- <includes>
- <include>**/*.*include>
- includes>
- <excludes>
- <exclude>*.propertiesexclude>
- excludes>
- <targetPath>${project.basedir}/target/classestargetPath>
- <filtering>truefiltering>
- resource>
- resources>
- <plugins>
-
- <plugin>
- <groupId>org.apache.maven.pluginsgroupId>
- <artifactId>maven-compiler-pluginartifactId>
- <version>3.1version>
- <configuration>
- <source>1.8source>
- <target>1.8target>
- <encoding>UTF8encoding>
- <compilerArguments>
- <bootclasspath>${java.home}\jre\lib\rt.jar;${java.home}\jre\lib\jce.jarbootclasspath>
- compilerArguments>
- configuration>
- plugin>
-
- <plugin>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-maven-pluginartifactId>
- <configuration>
-
- <mainClass>com.luck.MainApplicationmainClass>
-
- <layout>ZIPlayout>
-
- <includes>
- <include>
- <groupId>nojargroupId>
- <artifactId>nojarartifactId>
- include>
- includes>
-
- <outputDirectory>${global-jar-output}/${project.artifactId}outputDirectory>
- configuration>
- <executions>
- <execution>
- <goals>
- <goal>repackagegoal>
- goals>
- execution>
- executions>
- plugin>
- <plugin>
- <groupId>org.apache.maven.pluginsgroupId>
- <artifactId>maven-jar-pluginartifactId>
- <configuration>
- <archive>
-
- <manifest>
- <addClasspath>trueaddClasspath>
- <classpathPrefix>../lib/classpathPrefix>
- <useUniqueVersions>falseuseUniqueVersions>
- manifest>
-
- <manifestEntries>
- <Class-Path>${system-jar-classpath}Class-Path>
- manifestEntries>
- archive>
- configuration>
- plugin>
-
- <plugin>
- <groupId>org.apache.maven.pluginsgroupId>
- <artifactId>maven-dependency-pluginartifactId>
- <executions>
- <execution>
- <id>copy-libid>
- <phase>packagephase>
- <goals>
- <goal>copy-dependenciesgoal>
- goals>
- execution>
- executions>
- <configuration>
- <outputDirectory>${global-jar-output}/liboutputDirectory>
- <excludeTransitive>falseexcludeTransitive>
- <stripVersion>falsestripVersion>
- <silent>falsesilent>
- configuration>
- plugin>
-
- <plugin>
- <groupId>org.apache.maven.pluginsgroupId>
- <artifactId>maven-resources-pluginartifactId>
- <executions>
- <execution>
- <id>copy-configid>
- <phase>packagephase>
- <goals>
- <goal>copy-resourcesgoal>
- goals>
- <configuration>
- <resources>
- <resource>
- <directory>src/main/resourcesdirectory>
- <includes>
- <include>*.propertiesinclude>
- includes>
- resource>
- resources>
- <outputDirectory>${global-jar-output}/${project.artifactId}outputDirectory>
- configuration>
- execution>
- executions>
- plugin>
- plugins>
- build>
-
- <profiles>
- <profile>
- <id>testid>
- <properties>
- <package.environment>res-testpackage.environment>
- properties>
- profile>
- <profile>
- <id>devid>
- <activation>
- <activeByDefault>trueactiveByDefault>
- activation>
- <properties>
- <package.environment>resourcespackage.environment>
- properties>
- profile>
- profiles>
- project>


java读取外部配置文件需要用到
org.springframework.core.io.ClassPathResource.ClassPathResource这个类
InputStream in = new ClassPathResource("application.properties").getInputStream()
参考文章