由于开发过程与生产环境发布过程的服务器环境配置不同,所以在项目搭建过程中,需要对项目的服务环境配置文件进行区分,以方便打包发版
搭建Maven多模块项目
1.1 首先搭建Maven工程的父类模块
IDE选择`file`--->选择`Create Product`
1.2 搭建子模块工程
选择父类工程,右击选择new
—>Module
选择Maven ,然后不选择
Create from archetype
创建子模块
创建子模块完成后,在pom.xml文件中添加对应的依赖包
添加启动类
application类
@SpringBootApplication(scanBasePackages = "com.xxx")
@EnableSwagger2
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
添加yml配置文件,并且对配置文件进行环境划分
1.将原配置文件 application.yml 修改为 application-dev.yml并复制出一份 application-release.yml,如下图所示
2.修改applicaion.yml
内容如下:
spring:
profiles:
active: @profileActive@
2.1其他的各个环境的配置文件只配置各自环境的相关配置即可
server:
#端口号
port: 8880
servlet:
contextPath: /xxx
spring:
#应用名
application:
name: xxxx-xxx-web
#上传文件大小
servlet:
multipart:
max-file-size: 100Mb
max-request-size: 100Mb
#数据源
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://110.110.110.110:3306/xxxxxxx?useUnicode=true&characterEncoding=utf8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai
username: xxxx
password: xxxx
type: com.alibaba.druid.pool.DruidDataSource
druid:
initial-size: 5
min-idle: 5
max-active: 20
max-wait: 60000
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
validation-query: select 1 from dual
test-while-idle: true
test-on-borrow: false
test-on-return: false
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
#redis
redis:
host: 110.110.110.110
port: 6379
password:
timeout: 6000 # 连接超时时长(毫秒)
jedis:
pool:
max-active: 1000 # 连接池最大连接数(使用负值表示没有限制)
max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)
max-idle: 10 # 连接池中的最大空闲连接
min-idle: 5 # 连接池中的最小空闲连接
#日志
logging:
file: ../logs
level:
com.xxx: debug
#mybatis-plus
mybatis-plus:
mapper-locations: classpath*:mybatisplus/*.xml
typeAliasesPackage: com.xyj.**.model
configuration:
mapUnderscoreToCamelCase: true
file:
#文件仓库,保存上传文件
repository-path: /usr/local/nginx/html
2.2修改maven profiles
2.2.1 修改parent工程的pom.xml文件
进入项目的pom.xml,在<build>
标签下添加以下配置
<resources>
<resource>
<directory>src/main/resources</directory>
<!-- 处理文件时替换文件中的变量 -->
<filtering>true</filtering>
<excludes>
<!-- 打包时排除文件,可自行添加test.yml -->
<exclude>application.yml</exclude>
<exclude>application-dev.yml</exclude>
<exclude>application-prod.yml</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<!-- 打包时所包含得文件 -->
<includes>
<include>application.yml</include>
<include>application-${profileActive}.yml</include>
</includes>
</resource>
</resources>
2.2.2 在<project>
标签下添加profiles配置
继续编辑pom.xml,在标签下添加以下配置
<profiles>
<!-- dev开发环境配置,prod为生产环境配置 -->
<profile>
<id>dev</id>
<properties>
<profileActive>dev</profileActive>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>prod</id>
<properties>
<profileActive>prod</profileActive>
</properties>
</profile>
</profiles>
3.添加完毕后,可以在IDE中的maven
中的Profiles
中可以看到不同环境
的打包选项,选择不同环境进行打包即可
4.项目打包
通过勾选不同的环境配置进行打包,则会根据不同环境的配置进行打包配置文件
以上就是搭建Maven 多模块项目,并且配置多个环境yml文件切换
5.linux服务器git拉取项目以及打包
如果在pom文件中设置了默认dev环境的配置,在linux服务器中进行拉取项目时,如果没有指定配置环境,则会默认打包默认dev环境的配置文件,如果需要指定环境配置文件,可以通过如下命令进行打包指定配置文件
mvn package -P prod
---->maven打包成生产环境的配置文件
mvn package -P dev
---->maven打包成开发环境的配置文件
或
mvn clean install -P dev
---->maven打包成开发环境的配置文件
mvn clean install -P dev
---->maven打包成开发环境的配置文件