多环境打包部署

参考资料:

(180条消息) maven-assembly-plugin插件_dependencysets_Doctor舒畅的博客-CSDN博客

虽然前面的工程已经可以提供接口了,但是还不算完整的具备基础工程能力。

工程包含CICD,能够适配多环境配置和打出可用的二进制包才算完整

  • 配置文件
  • pom多profile配置
  • 其他打包配置
  • 常见启动命令
  • 手工发包部署(本质上自动化程序只不过是手工上的封装而已)

config目录下增加配置文件

  • application.properties
  • application-sit.properties

也有一些喜欢使用yml,但是经过自动化发现,并不太好用。

  • yml 层次分明,但是格式要求严格
  • properties没有缩进问题,但是不太好归类

application.properties

spring.profiles.active=@active.profile@
server.servlet.context-path=/test
server.port=10004
server.servlet.encoding.charset=utf-8

application-sit.properties暂时空白

启动:

配置已经生效了

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.8.RELEASE)

2023-05-03 19:54:00.553  INFO 12596 --- [           main] com.wht.test.TestAppStart                : Starting TestAppStart on DESKTOP-TVKIOH9 with PID 12596 (D:\workspace\java\SpringBootWebTest\target\classes started by 13355 in D:\workspace\java\SpringBootWebTest)
2023-05-03 19:54:00.553  INFO 12596 --- [           main] com.wht.test.TestAppStart                : No active profile set, falling back to default profiles: default
2023-05-03 19:54:01.708  INFO 12596 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 10004 (http)
2023-05-03 19:54:01.724  INFO 12596 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-05-03 19:54:01.724  INFO 12596 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.41]
2023-05-03 19:54:01.823  INFO 12596 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/test]   : Initializing Spring embedded WebApplicationContext
2023-05-03 19:54:01.823  INFO 12596 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1222 ms
2023-05-03 19:54:01.978  INFO 12596 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2023-05-03 19:54:02.137  INFO 12596 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 10004 (http) with context path '/test'
2023-05-03 19:54:02.153  INFO 12596 --- [           main] com.wht.test.TestAppStart                : Started TestAppStart in 2.027 seconds (JVM running for 2.509)

pom中体现多环境

这样就支持多环境了

    <profiles>
        <profile>
            <id>sit</id>
            <properties>
                <profies.active>sit</profies.active>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
    </profiles>

打包支持多环境

主要build这里有大量的插件,后续补充基础基础知识

    <build>
        <finalName>test</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <excludes>
                    <exclude>**/*.xlsx</exclude>
                    <exclude>application.properties</exclude>
                    <exclude>application-sit.properties</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>false</filtering>
                <includes>
                    <include>**/*.xlsx</include>
                </includes>

            </resource>
            <resource>
                <directory>config</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*.xlsx</include>
                    <include>application.properties</include>
                    <include>application-sit.properties</include>
                </includes>
                <targetPath>${project.build.directory}/config</targetPath>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptors>
                        <descriptor>/src/main/assembly/descriptor.xml</descriptor>
                    </descriptors>
                    <encoding>UTF-8</encoding>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>

                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <type>jar</type>
                            <includeTypes>jar</includeTypes>
                            <outputDirectory>${project.build.directory}/libs</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

descriptor.xml配置

为了支持打出jar包和tar.gz包

<assembly>
    <id>descriptor</id>
    <formats>
        <format>tar.gz</format>
    </formats>
    <fileSets>
        <fileSet>
            <directory>./src/main/shell</directory>
            <outputDirectory>/libs</outputDirectory>
            <fileMode>755</fileMode>
            <includes>
                <include>*.sh</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>./target</directory>
            <outputDirectory>/libs</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>./target/config</directory>
            <outputDirectory>/libs/config</outputDirectory>
            <includes>
                <include>*.properties</include>
                <include>*.ds</include>
            </includes>
            <directoryMode>0600</directoryMode>
            <fileMode>0600</fileMode>
        </fileSet>
    </fileSets>
</assembly>

启动脚本control.sh

每个包中放一个启动脚本是否方便OS类部署

对于自动化部署可以参考

#!/bin/bash
JAR_NAME=test.jar
CURPATH=$(cd "$(dirname "$0")";pwd)

cd $CURPATH

pid=`ps -ef|grep ${JAR_NAME}|grep -V grep|grep -v kill |awk '{print $2}'`
if [ ${pid} ]; then
  echo "strop ${JAR_NAME}....................."
  kill -9 $pid
else
  echo "${JAR_NAME} is not running.................."
fi
sleep 2

pid=`ps -ef|grep ${JAR_NAME}|grep -V grep|grep -v kill |awk '{print $2}'`
if [ ${pid} ]; then
  echo "${JAR_NAME} still running....................."
else
  echo "${JAR_NAME} is not running.................."
fi
sleep 2

nohup java -jar -Dserver.port=1005 ${JAR_NAME} --spring.profiles.active=$1 >test.log 2>&1 &
echo "start ${JAR_NAME} ..............."
sleep 3
tail -1000f test.log

linux手工部署

访问验证

问题记录及处理

1、打包失败 lombak无法编译

mvn clean package -Psit
  • lombak内部写死了很多虚拟机基础工具,他的编译依赖javac真正的版本
  • mvn说白了立即窗口可以运行是因为他是外部软件,依赖windows path中指定的java版本
  • windows中有个默认的oracle下的jre,可能是17版本,要从系统环境变量配置path中删除
  • 配置自己的java版本的环境变量
  • 再次运行 mv命令

2、配置文件变量没替换

3、idea中缺少springboot启动配置

posted @ 2023-05-03 22:45  红尘过客2022  阅读(28)  评论(0编辑  收藏  举报