From a1e279f736bb1bb50fa9a684414472dfa9925795 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 15 Jun 2021 22:00:11 +0100 Subject: [PATCH] Make Flyway and Liquibase auto-config back off without spring-jdbc The auto-config requires Spring JDBC for DataSource creation from spring.flyway.url or spring.liquibase.url, checking Flyway migrations exist, etc. Without it, the application fails to start due to missing spring-jdbc classes. This commit updates the auto-config so that it backs off in the absence of spring-jdbc. Closes gh-26849 --- .../autoconfigure/flyway/FlywayAutoConfiguration.java | 1 + .../liquibase/LiquibaseAutoConfiguration.java | 2 ++ .../flyway/FlywayAutoConfigurationTests.java | 8 ++++++++ .../liquibase/LiquibaseAutoConfigurationTests.java | 8 ++++++++ 4 files changed, 19 insertions(+) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java index 8a4b1dfc81..268f224def 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java @@ -115,6 +115,7 @@ public class FlywayAutoConfiguration { } @Configuration(proxyBeanMethods = false) + @ConditionalOnClass(JdbcUtils.class) @ConditionalOnMissingBean(Flyway.class) @EnableConfigurationProperties({ DataSourceProperties.class, FlywayProperties.class }) @Import({ FlywayMigrationInitializerEntityManagerFactoryDependsOnPostProcessor.class, diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java index 2a5a7b1c44..f52dd3977f 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java @@ -52,6 +52,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; +import org.springframework.jdbc.core.ConnectionCallback; import org.springframework.jdbc.core.JdbcOperations; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; import org.springframework.jdbc.datasource.SimpleDriverDataSource; @@ -92,6 +93,7 @@ public class LiquibaseAutoConfiguration { } @Configuration(proxyBeanMethods = false) + @ConditionalOnClass(ConnectionCallback.class) @ConditionalOnMissingBean(SpringLiquibase.class) @EnableConfigurationProperties({ DataSourceProperties.class, LiquibaseProperties.class }) public static class LiquibaseConfiguration { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java index 9e60ef80bf..05ef8eba1b 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java @@ -46,6 +46,7 @@ import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfigurati import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.boot.jdbc.SchemaManagement; import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; +import org.springframework.boot.test.context.FilteredClassLoader; import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.boot.test.system.CapturedOutput; import org.springframework.boot.test.system.OutputCaptureExtension; @@ -104,6 +105,13 @@ class FlywayAutoConfigurationTests { }); } + @Test + void backsOffWithFlywayUrlAndNoSpringJdbc() { + this.contextRunner.withPropertyValues("spring.flyway.url:jdbc:hsqldb:mem:" + UUID.randomUUID()) + .withClassLoader(new FilteredClassLoader("org.springframework.jdbc")) + .run((context) -> assertThat(context).doesNotHaveBean(Flyway.class)); + } + @Test void createDataSourceWithUrl() { this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfigurationTests.java index a21e055193..ad0d0a784e 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfigurationTests.java @@ -21,6 +21,7 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.Map; +import java.util.UUID; import java.util.function.Consumer; import javax.sql.DataSource; @@ -104,6 +105,13 @@ class LiquibaseAutoConfigurationTests { })); } + @Test + void backsOffWithLiquibaseUrlAndNoSpringJdbc() { + this.contextRunner.withPropertyValues("spring.liquibase.url:jdbc:hsqldb:mem:" + UUID.randomUUID()) + .withClassLoader(new FilteredClassLoader("org.springframework.jdbc")) + .run((context) -> assertThat(context).doesNotHaveBean(SpringLiquibase.class)); + } + @Test void createsDataSourceWhenSpringJdbcOnlyAvailableWithNoDataSourceBeanAndLiquibaseUrl() { this.contextRunner.withPropertyValues("spring.liquibase.url:jdbc:hsqldb:mem:liquibase")