java spring启动应用程序[profile]。属性不起作用
我已经安装了SpringBootWeb项目。当我只有一个应用程序时,它工作得很好。包含所有数据库连接详细信息的属性文件。我正在使用mysql数据库。这是我的申请表。属性文件
logging.file=myfilename.log
logging.path=/var/log/mydir
# db1
spring.db1.url=jdbc:mysql://[my_host_ip]/db1
spring.db1.username=my_host_username
spring.db1.password=my_host_password
spring.db1.driver-class-name=com.mysql.jdbc.Driver
# db2
spring.db2.url=jdbc:mysql://[my_host_ip]/db2
spring.db2.username=my_host_username
spring.db2.password=my_host_password
spring.db2.driver-class-name=com.mysql.jdbc.Driver
我想设置不同的环境,例如生产、开发、测试、登台、本地
根据文件Profile-specific propertie
我已经创建了5个特定于配置文件的属性文件
i) application-production.properties
ii) application-dev.properties
iii) application-test.properties
iv) application-staging.properties
v) application-local.properties
我已从默认应用程序中删除数据库连接属性。属性文件
我在GradleBuild中添加了这一功能,以允许传递活动配置文件
bootRun {
systemProperties = System.properties
}
当我使用gradle启动项目时
./gradlew clean bootRun -Dspring.profiles.active=test
它工作,它连接到测试数据库
但在理想的生产场景中,我想用“test”配置文件构建jar文件,这样它就可以运行所有测试,并在所有测试都通过时创建jar
例如
./gradlew clean build -Dspring.profiles.active=test
然后将jar部署到不同的环境(登台、开发、生产等)并运行
关于开发
java -jar myapp.jar -Dspring.profiles.active=dev
登台
java -jar myapp.jar -Dspring.profiles.active=staging
但是构建失败了,只有一个例外
Caused by: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: The url cannot be null
Caused by: java.sql.SQLException: The url cannot be null
另一个问题是,
如果一开始没有任何测试,是否可以只构建jar而不传递任何概要文件选项
./gradlew clean build
它因这个例外而失败
com.st.ComputeApplicationTests > contextLoads FAILED
java.lang.IllegalStateException
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException
Caused by: org.springframework.beans.factory.BeanCreationException
Caused by: org.springframework.beans.BeanInstantiationException
Caused by: org.springframework.beans.factory.BeanCreationException
Caused by: org.springframework.beans.BeanInstantiationException
Caused by: org.springframework.jdbc.CannotGetJdbcConnectionException
Caused by: java.sql.SQLException
更新:
正如@Alex的评论所建议的
“ComputeApplicationTests用@SpringBootTest注释,未指定任何ActiveProfiles。它在默认配置文件中找不到合适的配置,因此失败”
mport org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ComputeApplicationTests {
@Test
public void contextLoads() {
}
}
删除测试使生成暂时成功
共 (0) 个答案