美文网首页
springboot多环境配置打jar包

springboot多环境配置打jar包

作者: 小猪x | 来源:发表于2021-10-09 12:20 被阅读0次
一、配置
  • maven profile的定义pom.wxl
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>${smartadmin.springboot.version}</version>
    <configuration>
        <mainClass>${main-class}</mainClass>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
        </execution>
    </executions>
</plugin>
 ............
 ............
<profiles>
    <!--开发环境 (dev,用于写代码)resources目录 建立对应dev文件配置-->
    <profile>
        <id>dev</id>
        <properties>
            <profiles.active>dev</profiles.active>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <!--测试环境 (sit,测试人员测试)resources目录 建立对应sit文件配置-->
    <profile>
        <id>sit</id>
        <properties>
            <profiles.active>sit</profiles.active>
        </properties>
    </profile>
    <!--预发布环境(pre, 真实的数据,最真实的生产环境)resources目录 建立对应pre文件配置-->
    <profile>
        <id>pre</id>
        <properties>
            <profiles.active>pre</profiles.active>
        </properties>
    </profile>
    <!--生产环境(prod, 生产环境)resources目录 建立对应prod文件配置-->
    <profile>
        <id>prod</id>
        <properties>
            <profiles.active>prod</profiles.active>
        </properties>
    </profile>
</profiles>
  • 配置文件目录 resources
    因为使用了maven profile,所以必须在resources目录下建立不同环境的配置文件夹,如下图。

  • 打包配置build
    maven profile的打包核心是对配置文件的过滤,如下

<resources>
    <resource>
        <directory>src/main/java</directory>
        <includes>
            <include>**/*.*</include>
        </includes>
    </resource>
    <resource>
        <filtering>false</filtering>
        <directory>src/main/resources</directory>
        <excludes>
            <exclude>dev/*</exclude>
            <exclude>sit/*</exclude>
            <exclude>pre/*</exclude>
            <exclude>prod/*</exclude>
        </excludes>
    </resource>
    <resource>
        <directory>src/main/resources/${profiles.active}</directory>
        <filtering>true</filtering>
        <includes>
            <include>*.properties</include>
        </includes>
    </resource>
    <resource>
        <directory>src/main/resources/${profiles.active}</directory>
        <filtering>false</filtering>
        <includes>
            <include>*.*</include>
        </includes>
    </resource>
</resources>
image.png
二、打包
(1)使用mvn命令打包
mvn clean package 清理并打包命令,默认是使用local配置文件。
mvn clean package -P dev 清理并指定配置文件打包命令,使用dev配置文件。
(2)手动打包
  1. 选择打包环境【dev】、【prod】等
  2. 运行clean
  3. 运行package

最终会在【tager】目录下生成对应的jar包


image.png
三、运行jar包

进入tabget目录执行以下命令

  1. 默认端口:java -jar smart-admin-api-1.0.0.jar
  2. 指定端口:java -jar smart-admin-api-1.0.0.jar --server.port=8888
image.png

运行jar包的方式有三种

  1. java -jar xxxxx.jar
    当前ssh窗口被锁定,CTRL + C打断程序运行,或直接关闭窗口,程序退出
  2. java -jar xxxxx.jar &
    当前ssh窗口不被锁定,但当窗口关闭时,程序中止运行
  3. nohup Java -jar xxxxxx.jar &
    不挂断运行命令,当账户退出或终端关闭时,程序仍然运行

第一二种ssh窗口开着才运行,第三种窗口关闭依然运行

四、关闭jar包运行

lsof -i:10086 #10086为你那jar包运行的端口
kill -9 6133 #杀掉编号为6133的进程(请根据实际情况输入)


image.png

相关文章

网友评论

      本文标题:springboot多环境配置打jar包

      本文链接:https://www.haomeiwen.com/subject/rrrqoltx.html