首页 > 编程语言 > 使用springBoot项目配置文件位置调整到打包外
2022
02-21

使用springBoot项目配置文件位置调整到打包外

项目目录

问题痛点:

当我们打包一个项目的时候,springboot打包的jar都是把resouce下的配置文件打进去了,这样就没发修改配置文件

解决方案

  • 1.打包的时候指定打包路径
  • 2.将配置文件从resouce下面移出来

这两种方案其实都涉及到一个maven打包配置问题

首先来谈谈将配置文件从resouce下面移出来怎么解决

1在项目src同级别目录建

config目录

2.将resouce下的

application.yaml 文件移到config目录下方便打包后可以配置application文件中相关配置

pom.xml中的打包配置

 <build>
        <resources>
            <resource>
                <directory>config</directory>
                <includes>
                    <include>*.yaml</include>
                    <include>*.xml</include>
                    <include>*.conf</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
<!--过滤掉的 -->
                <!--                <excludes>-->
                <!--                    <exclude>**/*.properties</exclude>-->
                <!--                    <exclude>**/*.xml</exclude>-->
                <!--                    <exclude>**/*.yml</exclude>-->
                <!--                </excludes>-->
                <filtering>false</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions></executions>
                <configuration>
                    <!-- 生成可执行的jar的名字:xxx-exec.jar -->
                    <!-- 不固定,写成abcd都可以 -->
                    <classifier>exec</classifier>
                    <mainClass>com.cloudwise.douc.zabbix.api.DoucZabbixApplication</mainClass>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptors>
                        <descriptor>src/main/assembly/assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

assembly.xml配置

<assembly
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
        xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
    <id>bin</id>
    <formats>
        <format>tar.gz</format>
    </formats>
    <includeBaseDirectory>true</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>${project.basedir}</directory>
            <outputDirectory>./</outputDirectory>
            <includes>
                <include>README*</include>
                <include>LICENSE*</include>
                <include>NOTICE*</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>config</directory>
            <outputDirectory>config</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>bin</directory>
            <outputDirectory>bin</outputDirectory>
            <fileMode>777</fileMode>
        </fileSet>
        <fileSet>
            <directory>target</directory>
            <outputDirectory>lib</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
    </fileSets>
    <dependencySets>
        <dependencySet>
            <!--是否把本项目添加到依赖文件夹下-->
            <useProjectArtifact>true</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>
            <!--将scope为runtime的依赖包打包-->
            <!--<scope>runtime</scope>-->
        </dependencySet>
    </dependencySets>
</assembly>

sh启动类shell脚本

#!/bin/bash
PWDPATH=`dirname $0`
COMM_HOME=`cd $PWDPATH && cd .. && pwd`
cd $COMM_HOME
start () {
    JVM_OPTS="
     -server
     -Xms1g
     -Xmx1g
     -XX:+AlwaysPreTouch
     -XX:+UseG1GC
     -XX:MaxGCPauseMillis=2000
     -XX:GCTimeRatio=4
     -XX:InitiatingHeapOccupancyPercent=30
     -XX:G1HeapRegionSize=8M
     -XX:ConcGCThreads=2
     -XX:G1HeapWastePercent=10
     -XX:+UseTLAB
     -XX:+ScavengeBeforeFullGC
     -XX:+DisableExplicitGC
     -XX:+PrintGCDetails
     -XX:-UseGCOverheadLimit
     -XX:+PrintGCDateStamps
     -Xloggc:logs/gc.log
     -Dlog4j2.configurationFile=config/log4j2.xml
    "
    export CLASSPATH=$JAVA_HOME/jre/lib/*:$JAVA_HOME/lib/*:$COMM_HOME/lib/*
#    启动类路径
    export MAINCLASS="com.gug.api.AdimApplication"
    case $1 in
    -b )
        nohup java $JVM_OPTS -cp $CLASSPATH $MAINCLASS 1>/dev/null 2>&1 &
    ;;
    -d )
        java $JVM_OPTS -classpath $CLASSPATH $MAINCLASS
    ;;
    esac
    echo -e '\r'
}
case $1 in
restart )
    echo stop
    PID=`ps avx|grep $COMM_HOME|grep -v 'grep'|awk '{print $1}'`
    if  [ ! -n "$PID" ] ;then
       echo "The current process does not exist."
    else
       kill $PID
       echo "The process has been successfully stopped."
    fi
    echo start
    if [ ! -n "$2" ] ;then
	echo "After start, you must add parameters -d or -b. See help for details."
    else
        start $2 -b
    fi
;;
start )
    echo start
    if [ ! -n "$2" ] ;then
	echo "After start, you must add parameters -d or -b. See help for details."
    else
        start $2
    fi
;;
stop )
    echo stop
    PID=`ps avx|grep $COMM_HOME|grep -v 'grep'|awk '{print $1}'`
    if  [ ! -n "$PID" ] ;then
       echo "The current process does not exist."
    else
       kill $PID
       echo "The process has been successfully stopped."
    fi
;;
pid )
    PID=`ps avx|grep $COMM_HOME|grep -v 'grep'|awk '{print $1}'`
    if  [ ! -n "$PID" ] ;then
       echo "The current process does not exist."
    else
       echo "pid : "${PID}
    fi
;;
status )
    PID=`ps avx|grep $COMM_HOME|grep -v 'grep'|awk '{print $1}'`
    if  [ ! -n "$PID" ] ;then
       echo "dead"
    else
       echo "running"
    fi
;;
help )
    echo 'start    -d or -b     Start the service DEBUG mode or background mode.'
    echo 'stop                  Stop the service running in the background.'
    echo 'pid                   Gets the current process id information.'
    echo 'status                Gets the current service status information.'
;;
* )
    echo Command error, please enter help
;;
esac

总结:

当打包过程中出现各种问题后,及时的去查找问题,一般注意pom中的配置打包是否没有把某些包打进去还有就是启动sell脚本通过 ./Aplication.sh start -d 显示启动日志

到此这篇使用springBoot项目配置文件位置调整到打包外文章就介绍到这了,更多相关springBoot项目配置文件位置调整到打包外的内容请搜索自学编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持自学编程网!

编程技巧