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

 更新时间:2021年08月10日 17:27:47   作者:langzilige  
这篇文章主要介绍了使用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项目配置文件位置调整到打包外的内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • SpringBoot中集成日志的四种方式

    SpringBoot中集成日志的四种方式

    在开发中,日志记录是保障应用程序健壮性、可维护性的重要手段,通过日志,我们可以记录系统的运行状态、捕获异常并进行调试,Spring Boot 默认使用的是 Logback,但你也可以根据需求选择其他框架,以下是几种常用的日志集成方法,需要的朋友可以参考下
    2024-10-10
  • Mybatis结果集自动映射的实例代码

    Mybatis结果集自动映射的实例代码

    在使用Mybatis时,有的时候我们可以不用定义resultMap,而是直接在<select>语句上指定resultType。这个时候其实就用到了Mybatis的结果集自动映射,下面通过本文给大家分享Mybatis结果集自动映射的实例代码,一起看看吧
    2017-02-02
  • 2020最新版SSM框架整合教程

    2020最新版SSM框架整合教程

    这篇文章主要介绍了2020最新版SSM框架整合教程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • JavaFX如何获取ListView(列表视图)的选项

    JavaFX如何获取ListView(列表视图)的选项

    这篇文章主要介绍了JavaFX如何获取ListView(列表视图)的选项,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-01-01
  • Java 如何遍历JsonObject对象

    Java 如何遍历JsonObject对象

    这篇文章主要介绍了Java 如何遍历JsonObject对象?今天小编就为大家分享一篇Java遍历JsonObject对象案例,希望对大家有所帮助吧
    2021-01-01
  • Java 集合实现分页的方法(业务代码实现分页)

    Java 集合实现分页的方法(业务代码实现分页)

    在Java开发中,有些场景比较复杂,受限制,不好在sql查询层面实现分页,需要在查询的list结果后,将list分页返回,如何实现呢,带着这个问题一起通过本文学习吧
    2025-02-02
  • Eclipse不自动编译java文件的终极解决方法

    Eclipse不自动编译java文件的终极解决方法

    这篇文章主要介绍了Eclipse不自动编译java文件的终极解决方法,需要的朋友可以参考下
    2015-12-12
  • 解析Java并发Exchanger的使用

    解析Java并发Exchanger的使用

    Exchanger是java 5引入的并发类,Exchanger顾名思义就是用来做交换的。这里主要是两个线程之间交换持有的对象。当Exchanger在一个线程中调用exchange方法之后,会等待另外的线程调用同样的exchange方法。两个线程都调用exchange方法之后,传入的参数就会交换。
    2021-06-06
  • springboot整合quartz实现定时任务示例

    springboot整合quartz实现定时任务示例

    spring支持多种定时任务的实现。我们来介绍下使用spring的定时器和使用quartz定时器,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2017-04-04
  • 小议Java的源文件的声明规则以及编程风格

    小议Java的源文件的声明规则以及编程风格

    这篇文章主要介绍了小议Java的源文件的声明规则以及编程风格,仅给Java初学者作一个简单的示范,需要的朋友可以参考下
    2015-09-09

最新评论