将springboot项目,依赖jar,配置文件分离,外置
pom配置如下:
之前发布boot项目的时候,改动一点东西,就需要将整个项目重新打包部署,十分不便,故把依赖lib从项目分离出来,每次部署只需要发布代码即可。
步骤1:不使用spring-boot-maven-plugin
1.不使用spring-boot-maven-plugin,采用maven-jar-plugin
原因:spring-boot-maven会将项目所依赖的jar包都打入BOOT-INF/lib下
<plugins>
<!--
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib</classpathPrefix>
<mainClass>com.xxx.sample.SampleApplication</mainClass>
</manifest>
</archive>
<excludes>
<exclude>*.sh</exclude>
<exclude>*.bat</exclude>
<exclude>application.properties</exclude>
</excludes>
</configuration>
</plugin>
上述xml文件中:
<addClasspath>表示需要将所需依赖加入到类构建路径<classpathPrefix>指定生成的MANIFEST.MF文件中Class-Path依赖前面都加上路径lib,构建出lib/xx.jar,如果不加,在启动项目时无法找到所依赖的jar,会导致启动失败。- 在打包时将
application.properties文件排除在jar包之外,jar包中不必包含配置文件,然后在jar包同级目录下新建一个文件夹config。将配置文件放到config中。
这是因为SpringBoot读取配置有一个优先级,放在jar包外面config目录优先级最高,主要是便于从外部修改配置,而不是改jar包中的application.yml文件。优先级如下:
1.当前目录的config目录下
2.当前目录
3.classpath的config目录下
4.classpath的根目录
这样就实现了配置文件外置,可随时修改项目配置
步骤2:配置maven-dependency-plugin插件
2.将所依赖的jar在打包时拷贝到项目jar外的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>
<configuration>
<outputDirectory>target/lib</outputDirectory>
<excludeTransitive>false</excludeTransitive>
<stripVersion>false</stripVersion>
<includeScope>runtime</includeScope>
</configuration>
</execution>
</executions>
</plugin>
maven-dependency-plugin插件将所有的依赖都放入到target/lib文件夹下
步骤3:配置maven-assembly-plugin插件
3.配置assembly文件,配置具体的需要打包的资源
在pom.xml中配置maven-assembly-plugin插件,在assembly插件中配置assembly.xml文件的位置
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<!-- The configuration of the plugin -->
<configuration>
<descriptors>
<descriptor>src/main/resources/config/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<descriptor>标签指定了assembly.xml文件的位置
assembly.xml文件的配置如下:
<id>package</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<!-- 把项目相关的说明文件,打包进zip文件的根目录 -->
<fileSet>
<directory>${project.basedir}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<!--<include>*.sql</include>-->
<include>*.bat</include>
<include>*.md</include>
<!--<include>README*</include>-->
<!--<include>LICENSE*</include>-->
<!--<include>NOTICE*</include>-->
<!--<include>build.info</include>-->
</includes>
</fileSet>
<fileSet>
<directory>${project.build.directory}/lib</directory>
<outputDirectory>/lib</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
<fileSet>
<directory>${project.basedir}/src/main/resources</directory>
<outputDirectory>/config</outputDirectory>
<includes>
<include>application.properties</include>
<include>logback.xml</include>
<include>*.sql</include>
</includes>
</fileSet>
<!-- 把项目自己编译出来的jar文件,打包进zip文件的根目录 -->
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
</fileSets>
上述配置会将application.properties和logback文件放置在jar同级目录下
打成包后的目录层级:
--sam
--config
--application.properties
--logback.xml
--log
--lib
--cameraIndexCode
--sam.jar
在项目运行时会自动在sam.jar的同级目录下生成一个log文件夹(这个是在logback.xml中配置的)
logback.xml的配置如下:
<property name="LOG_HOME" value="log"/>
<property name="component" value="sam"/>
<property name="segment" value="hello"/>
<property name="samSqlLevel" value="INFO"/>
<property name="defaultPattern"
value="%d{yyyy-MM-dd'T'HH:mm:ss.SSSXXX} %level ${component} [%thread] [%logger{50}:%line] %msg%n"/>
<contextName>${component}</contextName>
<!--控制台输出日志-->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<charset>UTF-8</charset>
<pattern>${defaultPattern}</pattern>
</encoder>
</appender>
<!-- 文件输出日志 (文件大小策略进行文件输出,超过指定大小对文件备份)-->
<appender name="FILE-debug" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>TRACE</level>
</filter>
<File>${LOG_HOME}/${component}.debug.log</File>
<!--压缩的策略-->
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<FileNamePattern>${LOG_HOME}/${component}.debug.log.%d{yyyy-MM-dd}.%i.zip</FileNamePattern>
<maxHistory>7</maxHistory>
<MaxFileSize>100MB</MaxFileSize>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<charset>UTF-8</charset>
<pattern>${defaultPattern}</pattern>
</encoder>
</appender>
<appender name="FILE-error" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
<File>${LOG_HOME}/${component}.error.log</File>
<!--压缩的策略-->
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<FileNamePattern>${LOG_HOME}/${component}.error.log.%d{yyyy-MM-dd}.%i.zip</FileNamePattern>
<maxHistory>7</maxHistory>
<MaxFileSize>100MB</MaxFileSize>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<charset>UTF-8</charset>
<pattern>${defaultPattern}</pattern>
</encoder>
</appender>
<appender name="FILE-sql" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>TRACE</level>
</filter>
<File>${LOG_HOME}/${component}.sql.log</File>
<!--压缩的策略-->
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<FileNamePattern>${LOG_HOME}/${component}.sql.log.%d{yyyy-MM-dd}.%i.zip</FileNamePattern>
<maxHistory>7</maxHistory>
<MaxFileSize>100MB</MaxFileSize>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<charset>UTF-8</charset>
<pattern>${defaultPattern}</pattern>
</encoder>
</appender>
<logger name="org.springframework" level="ERROR" addtivity="false">
<appender-ref ref="FILE-error"/>
</logger>
<logger name="com.xxx.sample" level="INFO" addtivity="false">
<appender-ref ref="FILE-debug"/>
</logger>
<logger name="dao" level="${helloSqlLevel}" addtivity="false">
<appender-ref ref="FILE-sql"/>
</logger>
<root level="WARN">
<appender-ref ref="STDOUT"/>
</root>
上述配置的文件输出目录为${LOG_HOME}/ ${component}也就是log/sam.xx.log,这个log文件夹是会生成在jar的同级目录下.
需要在application.properties文件中设置日志配置文件所在位置
logging.config=logback.xml
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)