打出来的jar包找不到主类?
配置skip!
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<skip>false</skip>
</configuration>
</plugin>
多环境开发配置
spring:
profiles:
active: test
---
spring:
profiles: pro
server:
port: 80
---
spring:
profiles: dev
server:
port: 81
---
spring:
profiles: tett
server:
port: 82
读取yaml数据
- 例子: 这里的yaml数据是
database:
type: postgresql
host: 127.0.0.1
port: 5432
username: admin
password: secret
直接读取
@Value("${database.username}") private String u2;
- 新建一个pojo类
package com.itheima.domian;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "database")
public class DataBase {
private String type;
private String host;
private Integer port;
private String username;
private String password;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getPort() {
return port;
}
public void setPort(Integer port) {
this.port = port;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public String toString() {
return "DataBase{" +
"host='" + host + '\'' +
", type='" + type + '\'' +
", port=" + port +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
然后 自动装配 即可~
@Autowired
private DataBase dataBase;
多环境直接通过启动参数修改
java -jar springboot_01_quickstart-0.0.1-SNAPSHOT.jar
java -jar springboot_01_quickstart-0.0.1-SNAPSHOT.jar --spring.profiles.active=test
java -jar springboot_01_quickstart-0.0.1-SNAPSHOT.jar --spring.profiles.active=test --server.port=88
即使把参数顺序反过来,即 --server.port=88 在前,--spring.profiles.active=test 在后,应用仍然会以端口 88 启动。因为命令行参数的优先级总是高于配置文件中的设置。
yml和pom联调
在pom.xml中也可以定义不同开发环境, 如下方代码所示
但是想在yaml文件中读取到现在选择的配置, 还需要一些操作
<profiles>
<!--开发环境-->
<profile>
<id>dev</id>
<properties>
<profile.active>dev</profile.active>
</properties>
</profile>
<!--生产环境-->
<profile>
<id>pro</id>
<properties>
<profile.active>pro</profile.active>
</properties>
</profile>
<!--测试环境-->
<profile>
<id>test</id>
<properties>
<profile.active>test</profile.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
- 首先导入这个插件
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<encoding>UTF-8</encoding>
<useDefaultDelimiters>true</useDefaultDelimiters>
</configuration>
</plugin>
- 配置过滤器(在
标签中)
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.properties</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
异常拦截
使用 @RestControllerAdvice 注解和 @ExceptionHandler 注解
是 @ControllerAdvice 和 @ResponseBody 两个注解的组合。
@ExceptionHandler(Exception.class)用来指定处理哪种错误
既是响应体, 也是异常拦截
有了这个注解就不需要别的配置了
package com.itheima.controller;
import com.itheima.exception.BusinessException;
import com.itheima.exception.SystemException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
public class ProjectExceptionAdvice {
@ExceptionHandler(SystemException.class)
public Result doSystemException(SystemException e){
//记录日志
//发送消息给运维
//发送邮件给运维
return new Result(e.getCode(), null, "系统级错误1:" + e.getMessage());
}
@ExceptionHandler(BusinessException.class)
public Result doBusinessException(BusinessException e){
return new Result(e.getCode(), null, "业务级错误2:" + e.getMessage());
}
@ExceptionHandler(Exception.class)
public Result doException(Exception e){
//记录日志
//发送消息给运维
//发送邮件给运维
System.out.println("异常, 我草泥马");
return new Result(Code.SYSTEM_UNKNOW_ERR, null, "其他错误3系统繁忙, 请稍后再试");
}
}
Maven聚合/继承
- 父工程的结构如下图所示
parent-project
│
├── pom.xml
│
├── module-a
│ └── pom.xml
│
└── module-b
└── pom.xml
- 父项目
父项目是空的, 只需要填写pom文件, 设置打包方式为pom,标签设置需要管理的子项目
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>parent-project</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>module-a</module>
<module>module-b</module>
</modules>
</project>
- 子项目
子项目通过标签继承父项目
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>parent-project</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>module-a</artifactId>
<dependencies>
<!-- 在这里添加 module-a 的依赖 -->
</dependencies>
</project>
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>parent-project</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>module-b</artifactId>
<dependencies>
<!-- 在这里添加 module-b 的依赖 -->
</dependencies>
</project>
Maven配置属性
- 使用
标签配置可以复用的属性, ${}解析值
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>my-project</artifactId>
<version>1.0-SNAPSHOT</version>
<!--**我在这**-->
<properties>
<java.version>1.8</java.version>
<spring.version>5.3.10</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
properties文件加载maven配置
- 像Maven配置可服用属性一样写
- 在父工程中配置拦截 由于子项目继承了父工程, 拦截也会一并读取到
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>parent-project</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<!--**配置在这**-->
<properties>
<jdbc.url>jdbc:mysql://localhost:3306/mydb</jdbc.url>
<jdbc.username>root</jdbc.username>
<jdbc.password>password</jdbc.password>
</properties>
<!--**拦截在这**-->
<build>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<modules>
<module>child-module</module>
</modules>
</project>
Maven配置多环境开发
- 在父工程中设定多种
来写配置文件
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>parent-project</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<profiles>
<!-- 开发环境 -->
<profile>
<id>development</id>
<properties>
<env>development</env>
<db.url>jdbc:mysql://localhost:3306/dev_db</db.url>
<db.username>dev_user</db.username>
<db.password>dev_password</db.password>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<!-- 测试环境 -->
<profile>
<id>testing</id>
<properties>
<env>testing</env>
<db.url>jdbc:mysql://localhost:3306/test_db</db.url>
<db.username>test_user</db.username>
<db.password>test_password</db.password>
</properties>
</profile>
<!-- 生产环境 -->
<profile>
<id>production</id>
<properties>
<env>production</env>
<db.url>jdbc:mysql://localhost:3306/prod_db</db.url>
<db.username>prod_user</db.username>
<db.password>prod_password</db.password>
</properties>
</profile>
</profiles>
</project>
- 利用 -P 指令其实也可以指定某个环境
mvn clean install -P production<!--先删除target, 再用生产环境安装-->
跳过测试
- 点击小闪电标志(跳过全部测试)
- 配置插件
跳过全部测试
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version> <!-- 请根据需要选择合适的版本 -->
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
跳过某个测试类
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version> <!-- 请根据需要选择合适的版本 -->
<configuration>
<excludes>
<exclude>**/SkipThisTest*.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
跳过某个测试方法
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version> <!-- 请根据需要选择合适的版本 -->
<configuration>
<includes>
<include>**/MyTest*.java</include>
</includes>
<excludes>
<exclude>**/MyTest.java#testMethodToSkip</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
- 通过maven指令
mvn clean install -DskipTests
Maven私服
- 设置上传方式
<distributionManagement>
<repository>
<id>itheima-release</id>
<url>http://localhost:8081/repository/itheima-release/</url>
</repository>
<snapshotRepository>
<id>itheima-snapshot</id>
<url>http://localhost:8081/repository/itheima-snapshot/</url>
</snapshotRepository>
</distributionManagement>