外置application.properties和logback.xml配置文件

自定义打包输出

使用maven-assembly-plugin自定义打包输出,其在pom文件中的配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<finalName>4a-insight-clickhouse</finalName>
<descriptors>
<descriptor>src/main/assembly/package.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<!-- 绑定到package生命周期阶段上 -->
<phase>package</phase>
<goals>
<!-- 只运行一次 -->
<goal>single</goal>
</goals>
</execution>
</executions>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>logback-spring.xml</exclude>
</excludes>
</resource>
</resources>
</plugin>

<descriptors>描述自定义文件的位置,<phase>maven打包的哪个生命周期生效。由于有了外置的logback-spring.xml文件,所以要排除掉jar包内的logback-spring.xml文件,要不然会出现错误。

package.xml配置文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>full</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<!-- for bin -->
<fileSet>
<directory>src/main/bin</directory>
<includes>
<include>*.*</include>
</includes>
<directoryMode>775</directoryMode>
<outputDirectory>/bin</outputDirectory>
</fileSet>
<!-- for spring boot jar -->
<fileSet>
<directory>target/</directory>
<includes>
<include>9n-4a-insight-0.0.1-SNAPSHOT.jar</include>
</includes>
<outputDirectory>/lib</outputDirectory>
</fileSet>

<fileSet>
<!--打包时把配置文件放在config目录下,并且config目录与jar包同属于一个目录-->
<directory>target/classes/</directory>
<includes>
<include>application.properties</include>
<include>logback-spring.xml</include>
</includes>
<outputDirectory>/lib/config</outputDirectory>
<fileMode>0755</fileMode>
</fileSet>
</fileSets>
</assembly>

jar包放到lib目录下,application.propertieslogback-spring.xml放到lib/config目录下,启动和停止脚本放到bin目录下。关于application.properties配置文件生效优先级参考:https://www.jianshu.com/p/86a40ce2eb7a,logback扩展配置参考:https://juejin.im/post/5bbdb64fe51d450e5e0cb1c4

应用启动命令配置

根据命令执行的当前路径找到对应的配置文件

1
2
#!/usr/bin/env bash
java -jar -Dspring.config.location=../lib/config/ -Dlogback.configurationFile=../lib/config/logback-spring.xml ../lib/9n-4a-insight-0.0.1-SNAPSHOT.jar &