springboot打包jar和lib分离 指定文件夹的配置文件打包
java哥
阅读:674
2021-04-01 11:04:08
评论:0
有些时候jar中不想携带所有的配置文件,比如自己的生产环境配置不想交付到客户,jar包与lib分离方便频繁部署
1.指定配置文件夹
如图打包只想在jar中保留dev下配置文件,不要prop下的防止泄露账户密码
2.pom
<!-- 定义开发运行时的环境变量:开发、测试、生产环境 -->
<profiles>
<profile> <!-- 开发环境 -->
<id>dev</id>
<properties>
<profiles.env>dev</profiles.env>
<log4j.rootlevel>debug</log4j.rootlevel>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation> <!-- Eclipse中运行时是否启用该节点 -->
</profile>
<profile> <!-- 测试环境 -->
<id>test</id>
<properties>
<profiles.env>test</profiles.env>
<log4j.rootlevel>info</log4j.rootlevel>
</properties>
</profile>
<profile> <!-- 生产环境 -->
<id>prod</id>
<properties>
<profiles.env>prod</profiles.env>
<log4j.rootlevel>info</log4j.rootlevel>
</properties>
</profile>
</profiles>
<build>
<finalName>${project.artifactId}</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>*</include>
<include>/static/**</include>
<include>/mapper/**</include>
</includes>
<excludes>
<exclude>generatorConfig.xml</exclude>
</excludes> <!-- 打包时不打入指定配置文件(同时影响开发运行时) -->
<filtering>true</filtering> <!-- 是否自动替换${xxx}的内容(涉及log4j2.xml) -->
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources/${profiles.env}</directory>
<includes>
<include>*</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
<!-- 打包方法1:程序包和依赖包合并成一个包 -->
<!-- <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId>
<configuration> <layout>ZIP</layout> <executable>true</executable> </configuration>
</plugin> -->
<!-- 打包方法2:程序包和依赖包分开打包 -->
<!-- plugin1=程序包,plugin2=依赖包 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix> <!-- MANIFEST.MF 中 Class-Path 加入前缀 -->
<useUniqueVersions>false</useUniqueVersions> <!-- 防止在 MANIFEST.MF 中自动为引用的snapshot添加时间戳 -->
<mainClass>cn.zk.multimodule.controller.ControllerApplication</mainClass>
</manifest>
<manifestEntries>
<!-- <Class-Path>config/</Class-Path> --> <!--MANIFEST.MF 中 Class-Path 加入资源文件目录 -->
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeScope>runtime</includeScope>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
3.本地开发 放到哪个id 下就是启动对应环境配置文件
<activation>
<activeByDefault>true</activeByDefault>
</activation>
4.plugins将lib依赖包和jar包分离开来
编译打包
clean package -Pdev
5.jar包下只有一个配置文件,将其他环境的剔除
6.发布项目 java -jar controller.jar
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。