我的 Spring Boot 应用程序遇到问题。当我通过单击播放按钮直接从 IntelliJ IDEA 启动它时,它运行得非常好。但是,当我尝试使用
mvn clean install
构建项目时,遇到错误,指示 URL 必须以“jdbc”开头。此错误出现在 Surefire 报告中。
这是我的应用程序的主类:
package com.ampliamasnotas.usuarios_api;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class UsuariosApiApplication {
public static void main(String[] args) {
// Imprimir variables de entorno
String dbUrl = System.getenv("DB_URL");
String dbUser = System.getenv("DB_USER");
String dbPw = System.getenv("DB_PW");
System.out.println("DB_URL: " + dbUrl);
System.out.println("DB_USER: " + dbUser);
System.out.println("DB_PW: " + dbPw);
SpringApplication.run(UsuariosApiApplication.class, args);
}
}
这是应用程序.属性:
spring.application.name=usuarios_api
spring.datasource.url=${DB_URL}
spring.datasource.username=${DB_USER}
spring.datasource.password=${DB_PW}
spring.jpa.hibernate.ddl-auto=update
server.port=8080
这些是我在 IntelliJ IDEA 配置中设置的环境变量:
DB_URL=jdbc:mysql://localhost:3307/user?createIfNotExists=true&serverTimezone=UTC;DB_USER=root;DB_PW=123456
我在 Spring Initializr 中包含的依赖项是:
Spring Web、Spring Data JPA、MYSQL 驱动程序。
可能导致此问题的原因是什么?如何确保在
mvn clean install
期间正确设置环境变量?
如有任何建议,我们将不胜感激!
我已经读到,环境变量值周围不应有空格或引号,并且我已确保情况确实如此。该应用程序直接从 IntelliJ IDEA 运行良好,但似乎在 Maven 构建过程中没有选择环境变量。
运行 mvn clean install 时会出现问题:变量没有传递到执行测试的 JVM。您有两种选择来解决此问题:
mvn clean install
。pom.xml
以包含 Maven Surefire 插件所需的系统属性:...
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<systemPropertyVariables>
<DB_URL>jdbc:mysql://localhost:3307/user?createIfNotExists=true&serverTimezone=UTC</DB_URL>
<DB_USER>root</DB_USER>
<DB_PW>123456</DB_PW>
</systemPropertyVariables>
</configuration>
</plugin>
...
</plugins>
</build>
...