Skip to content

Commit

Permalink
ci: 👷 调整项目打包结构,分离依赖、配置文件
Browse files Browse the repository at this point in the history
1.调整 Spring Boot 配置文件到 config 目录
2.移除 Maven Profiles 配置
3.调整项目打包结构,分离依赖、配置文件。如无依赖调整,部署时仅需拷贝程序包,且更方便进行配置修改
4.调整后的项目打包结构,更贴合部署安装程序结构,例如:Tomcat 安装包、Maven 安装包
5.建议在 bin 目录上一级执行程序,以使日志文件能正确生成在 logs 目录下,参考 Dockerfile
  • Loading branch information
Charles7c committed Sep 6, 2023
1 parent 5c9e663 commit e679abf
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 83 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ jobs:
cache: 'maven'
# 3、打包
- name: Build
run: mvn -B package -P prod --file pom.xml
# 4、拷贝 jar 包到服务器
- name: Copy Jar
run: mvn -B package --file pom.xml
# 4、拷贝到服务器
- name: Copy
uses: garygrossgarten/github-action-scp@release
with:
host: ${{ secrets.SERVER_HOST }}
port: ${{ secrets.SERVER_PORT }}
username: ${{ secrets.SERVER_USERNAME }}
password: ${{ secrets.SERVER_PASSWORD }}
local: continew-admin-webapi/target/continew-admin.jar
remote: /docker/continew-admin/server/continew-admin.jar
local: ./continew-admin-webapi/target/app
remote: /docker/continew-admin
# 5、启动后端服务
- name: Start
uses: appleboy/ssh-action@master
Expand Down Expand Up @@ -75,7 +75,7 @@ jobs:
- name: Build
run: pnpm build
working-directory: ./continew-admin-ui
# 6、拷贝文件到服务器
# 6、拷贝到服务器
- name: Copy
uses: garygrossgarten/github-action-scp@release
with:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ git clone https://github.com/Charles7c/continew-admin.git
# 5.部署
# 5.1 Docker 部署
# 5.1.1 服务器安装好 docker 及 docker-compose(参考:https://blog.charles7c.top/categories/fragments/2022/10/31/CentOS%E5%AE%89%E8%A3%85Docker)
# 5.1.2 执行 mvn package -P prod 进行项目打包,将 target 目录下的 continew-admin.jar 放到 /docker/continew-admin/server 目录下
# 5.1.2 执行 mvn package 进行项目打包,将 target/app 目录下的所有内容放到 /docker/continew-admin 目录下
# 5.1.3 将 docker 目录上传到服务器 / 目录下,并授权(chmod -R 777 /docker)
# 5.1.4 修改 docker-compose.yml 中的 MariaDB 配置、Redis 配置、continew-admin-server 配置、Nginx 配置
# 5.1.5 执行 docker-compose up -d 创建并后台运行所有容器
Expand Down
93 changes: 74 additions & 19 deletions continew-admin-webapi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ limitations under the License.
<name>${project.artifactId}</name>
<description>API 模块(存放 Controller 层代码,打包部署的模块)</description>

<properties>
<!-- ### 打包配置相关 ### -->
<!-- 启动类 -->
<main-class>top.charles7c.cnadmin.ContiNewAdminApplication</main-class>
<!-- 程序 jar 输出目录 -->
<bin-path>bin</bin-path>
<!-- 配置文件输出目录 -->
<config-path>config</config-path>
<!-- 依赖 jar 输出目录 -->
<lib-path>lib</lib-path>
</properties>

<dependencies>
<!-- Liquibase(用于管理数据库版本,跟踪、管理和应用数据库变化) -->
<dependency>
Expand Down Expand Up @@ -66,34 +78,77 @@ limitations under the License.
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Dfile.encoding=UTF-8</argLine>
<!-- 根据启用环境执行对应 @Tag 的测试方法 -->
<groups>${profiles.active}</groups>
<!-- 排除标签 -->
<excludedGroups>exclude</excludedGroups>
<!-- 跳过单元测试 -->
<skip>true</skip>
</configuration>
</plugin>
<!-- Spring Boot 打包插件(将 Spring Boot Maven 应用打包为可执行的 jar 包) -->
<!-- Maven 打包插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<!-- 排除配置文件 -->
<excludes>
<exclude>${config-path}/</exclude>
</excludes>
<archive>
<manifest>
<mainClass>${main-class}</mainClass>
<!-- 为 MANIFEST.MF 中的 Class-Path 加入依赖 jar 目录前缀 -->
<classpathPrefix>../${lib-path}/</classpathPrefix>
<addClasspath>true</addClasspath>
<!-- jar 包不包含唯一版本标识 -->
<useUniqueVersions>false</useUniqueVersions>
</manifest>
<manifestEntries>
<!--为 MANIFEST.MF 中的 Class-Path 加入配置文件目录前缀 -->
<Class-Path>../${config-path}/</Class-Path>
</manifestEntries>
</archive>
<outputDirectory>${project.build.directory}/app/${bin-path}</outputDirectory>
</configuration>
</plugin>
<!-- 拷贝依赖 jar -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>repackage</goal>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/app/${lib-path}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!-- 拷贝配置文件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>${config-path}/</include>
</includes>
</resource>
</resources>
<outputDirectory>${project.build.directory}/app</outputDirectory>
</configuration>
</execution>
</executions>
<configuration>
<!-- 在新的 JVM 进程中运行打包任务(另注:在使用 devtools 时,如果没有该项配置,热部署可能会失效) -->
<fork>true</fork>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ rsa:
# 私钥
privateKey: MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAznV2Bi0zIX61NC3zSx8U6lJXbtru325pRV4Wt0aJXGxy6LMTsfxIye1ip+f2WnxrkYfk/X8YZ6FWNQPaAX/iRwIDAQABAkEAk/VcAusrpIqA5Ac2P5Tj0VX3cOuXmyouaVcXonr7f+6y2YTjLQuAnkcfKKocQI/juIRQBFQIqqW/m1nmz1wGeQIhAO8XaA/KxzOIgU0l/4lm0A2Wne6RokJ9HLs1YpOzIUmVAiEA3Q9DQrpAlIuiT1yWAGSxA9RxcjUM/1kdVLTkv0avXWsCIE0X8woEjK7lOSwzMG6RpEx9YHdopjViOj1zPVH61KTxAiBmv/dlhqkJ4rV46fIXELZur0pj6WC3N7a4brR8a+CLLQIhAMQyerWl2cPNVtE/8tkziHKbwW3ZUiBXU24wFxedT9iV

--- ### 日志配置
logging:
level:
top.charles7c: DEBUG
file:
path: ./logs

--- ### 接口文档配置
springdoc:
swagger-ui:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ rsa:
# 私钥
privateKey: MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAznV2Bi0zIX61NC3zSx8U6lJXbtru325pRV4Wt0aJXGxy6LMTsfxIye1ip+f2WnxrkYfk/X8YZ6FWNQPaAX/iRwIDAQABAkEAk/VcAusrpIqA5Ac2P5Tj0VX3cOuXmyouaVcXonr7f+6y2YTjLQuAnkcfKKocQI/juIRQBFQIqqW/m1nmz1wGeQIhAO8XaA/KxzOIgU0l/4lm0A2Wne6RokJ9HLs1YpOzIUmVAiEA3Q9DQrpAlIuiT1yWAGSxA9RxcjUM/1kdVLTkv0avXWsCIE0X8woEjK7lOSwzMG6RpEx9YHdopjViOj1zPVH61KTxAiBmv/dlhqkJ4rV46fIXELZur0pj6WC3N7a4brR8a+CLLQIhAMQyerWl2cPNVtE/8tkziHKbwW3ZUiBXU24wFxedT9iV

--- ### 日志配置
logging:
level:
top.charles7c: INFO
file:
path: ./logs

--- ### 接口文档配置
springdoc:
swagger-ui:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ project:

--- ### 日志配置(重叠部分,优先级高于 logback-spring.xml 中的配置)
logging:
level:
top.charles7c: @logging.level@
file:
path: @logging.file.path@
config: classpath:logback-spring.xml
## 系统日志配置
system:
Expand Down Expand Up @@ -186,8 +182,7 @@ spring:
## 环境配置
profiles:
# 启用的环境
# 配合 Maven Profile 选择不同配置文件进行启动,在 IntelliJ IDEA 右侧 Maven 工具窗口可以快速切换环境
active: @profiles.active@
active: dev
main:
# 允许定义重名的 bean 对象覆盖原有的 bean
allow-bean-definition-overriding: true
Expand Down
8 changes: 5 additions & 3 deletions docker/continew-admin/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ FROM java:8

MAINTAINER Charles7c [email protected]

ARG JAR_FILE=./server/*.jar
COPY ${JAR_FILE} app.jar
ARG JAR_FILE=./bin/*.jar
COPY ${JAR_FILE} /app/bin/app.jar
WORKDIR /app

ENTRYPOINT ["java", \
"-jar", \
"-Djava.security.egd=file:/dev/./urandom", \
"app.jar"]
"-Dspring.profiles.active=prod", \
"./bin/app.jar"]
8 changes: 5 additions & 3 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ services:
REDIS_PWD: 你的 Redis 密码
REDIS_DB: 你的 Redis 数据库索引
volumes:
- /docker/continew-admin/data/file:/data/file
- /docker/continew-admin/data/avatar:/data/avatar
- /docker/continew-admin/logs:/logs
- /docker/continew-admin/config:/app/config
- /docker/continew-admin/data/file:/app/data/file
- /docker/continew-admin/data/avatar:/app/data/avatar
- /docker/continew-admin/logs:/app/logs
- /docker/continew-admin/lib:/app/lib
depends_on:
- redis
- mariadb
Expand Down
45 changes: 0 additions & 45 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,33 +68,6 @@ limitations under the License.
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<profiles>
<profile>
<id>dev</id>
<!-- 自定义属性配置 -->
<properties>
<!-- Spring Boot 启用环境 -->
<profiles.active>dev</profiles.active>
<!-- 日志级别 -->
<logging.level>DEBUG</logging.level>
<!-- 日志存储位置 -->
<logging.file.path>./logs</logging.file.path>
</properties>
<activation>
<!-- 默认启用 -->
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>prod</id>
<properties>
<profiles.active>prod</profiles.active>
<logging.level>INFO</logging.level>
<logging.file.path>./logs</logging.file.path>
</properties>
</profile>
</profiles>

<!-- 全局依赖版本管理 -->
<dependencyManagement>
<dependencies>
Expand Down Expand Up @@ -287,24 +260,6 @@ limitations under the License.
</configuration>
</plugin>
</plugins>

<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.yml</include>
</includes>
<!-- 启用过滤,即替换对应资源中的变量 -->
<filtering>true</filtering>
</resource>
<!-- 除 YAML 配置文件外,其他配置文件不需要进行变量替换 -->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/**</include>
</includes>
</resource>
</resources>
</build>

<!-- 远程仓库配置:阿里云 Maven 中央仓库(公共代理仓库,Central 仓和 JCenter 仓的聚合仓,帮助研发人员提高研发生产效率,使用阿里云 Maven 中央仓库作为下载源,速度更快更稳定) -->
Expand Down

0 comments on commit e679abf

Please sign in to comment.