Skip to content

Commit

Permalink
添加maven 多环境使用demo
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyueyi committed Apr 22, 2022
1 parent 2440349 commit 41442c7
Show file tree
Hide file tree
Showing 19 changed files with 290 additions and 8 deletions.
71 changes: 71 additions & 0 deletions spring-boot/001-properties-env-mvn/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-boot</artifactId>
<groupId>com.git.hui.boot</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>001-properties-env-mvn</artifactId>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>

<profiles>
<!-- 开发 -->
<profile>
<id>dev</id>
<properties>
<env>dev</env>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<!-- 测试 -->
<profile>
<id>test</id>
<properties>
<env>test</env>
</properties>
</profile>
<!-- 预发 -->
<profile>
<id>pre</id>
<properties>
<env>pre</env>
</properties>
</profile>
<!-- 生产 -->
<profile>
<id>prod</id>
<properties>
<env>prod</env>
</properties>
</profile>
</profiles>

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/resources-env/${env}</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>

</project>
17 changes: 17 additions & 0 deletions spring-boot/001-properties-env-mvn/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## 001-properites-env-mvn

### 项目说明

基于mvn的多环境配置选择,主要适用于每个环境下,都有很多配置文件的场景;

此时通过指定maven中profile,激活对应的环境,然后在打包的时候可以将路径下的配置文件都打在一起

```bash
# mvn 指定环境打包操作
mvn clean package -DskipTest=ture -P prod
```

### 博文说明

本项目对应的博文内容为

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.git.hui.boot.env;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
* @author yihui
* @date 2022/4/20
*/
@Controller
@EnableConfigurationProperties({DalConfig.class})
@SpringBootApplication
public class Application {
private DalConfig dalConfig;

public Application(DalConfig dalConfig, Environment environment) {
this.dalConfig = dalConfig;
System.out.println(dalConfig);
}

public static void main(String[] args) {
SpringApplication application = new SpringApplication(Application.class);
application.run(args);
}


@GetMapping(path = {"", "/", "/index"})
public ModelAndView index() {
Map<String, Object> data = new HashMap<>(2);
data.put("info", dalConfig);
data.put("now", LocalDateTime.now().toString());
return new ModelAndView("index", data);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.git.hui.boot.env;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
* @author yihui
* @date 2022/4/20
*/
@Data
@ConfigurationProperties(prefix = "spring.datasource")
public class DalConfig {
private String url;

private String username;

private String password;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/story?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password:
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
spring:
datasource:
url: jdbc:mysql://pre.hhui.top/story?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
username: pre_root
password:
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
spring:
datasource:
url: jdbc:mysql://prod.hhui.top/story?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
username: prod_root
password:
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
spring:
datasource:
url: jdbc:mysql://test.hhui.top/story?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
username: test_root
password:
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
spring:
profiles:
active: dal,web
thymeleaf:
mode: HTML
encoding: UTF-8
servlet:
content-type: text/html
cache: false

server:
port: 8080
tomcat:
accesslog:
enabled: true
directory: /logs/boot
file-date-format: .yyyyMMdd
pattern: '%h %l %u %t "%r" %s %b %Dms "%{Referer}i" "%{User-Agent}i" "%{X-Request-ID}i" "%{X-Forwarded-For}i"'
# basedir: /logs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.title {
color: #c00;
font-weight: normal;
font-size: 2em;
}

.content {
color: darkblue;
font-size: 1.2em;
}

.sign {
color: lightgray;
font-size: 0.8em;
font-style: italic;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="SpringBoot thymeleaf"/>
<meta name="author" content="YiHui"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>YiHui's SpringBoot Demo</title>
</head>
<body>

<div>
<div class="title">hello world!</div>
<br/>
<div class="content" th:text="'配置信息:' + ${info}">默认的内容</div>
<br/>
<div class="sign" th:text="'当前时间' + ${now}">默认的签名</div>
<br/>
</div>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
spring:
datasource:
url: jdbc:mysql://prod.hhui.top/story?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
username: prod_root
password:
19 changes: 19 additions & 0 deletions spring-boot/001-properties-env-mvn/target/classes/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
spring:
profiles:
active: dal,web
thymeleaf:
mode: HTML
encoding: UTF-8
servlet:
content-type: text/html
cache: false

server:
port: 8080
tomcat:
accesslog:
enabled: true
directory: /logs/boot
file-date-format: .yyyyMMdd
pattern: '%h %l %u %t "%r" %s %b %Dms "%{Referer}i" "%{User-Agent}i" "%{X-Request-ID}i" "%{X-Forwarded-For}i"'
# basedir: /logs
Binary file not shown.
Binary file not shown.
16 changes: 16 additions & 0 deletions spring-boot/001-properties-env-mvn/target/classes/static/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.title {
color: #c00;
font-weight: normal;
font-size: 2em;
}

.content {
color: darkblue;
font-size: 1.2em;
}

.sign {
color: lightgray;
font-size: 0.8em;
font-style: italic;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="SpringBoot thymeleaf"/>
<meta name="author" content="YiHui"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>YiHui's SpringBoot Demo</title>
</head>
<body>

<div>
<div class="title">hello world!</div>
<br/>
<div class="content" th:text="'配置信息:' + ${info}">默认的内容</div>
<br/>
<div class="sign" th:text="'当前时间' + ${now}">默认的签名</div>
<br/>
</div>
</body>
</html>
1 change: 1 addition & 0 deletions spring-boot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
<module>142-search-es</module>

<module>430-email</module>
<module>001-properties-env-mvn</module>

</modules>

Expand Down
8 changes: 0 additions & 8 deletions spring/500-json/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,12 @@
<version>6.8.8</version>
<scope>compile</scope>
</dependency>
<<<<<<< Updated upstream

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.1-jre</version>
<scope>compile</scope>
=======
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.0.1-jre</version>
>>>>>>> Stashed changes
</dependency>
</dependencies>
</project>

0 comments on commit 41442c7

Please sign in to comment.