根据Flyway documentation,应该可以在开箱即用的Grails 3项目中使用Flyway:
Grails 3.x基于Spring Boot,为Flyway提供开箱即用的集成 您需要做的就是将flyway-core添加到build.gradle中:
compile "org.flywaydb:flyway-core:4.1.1"
然后,Spring Boot将自动使用其DataSource自动装配Flyway,并在启动时调用它。
这对我不起作用。 Flyway在应用程序启动时没有启动。在日志中,我看到一些可疑的行:
FlywayAutoConfiguration did not match - @ConditionalOnClass found required class 'org.flywaydb.core.Flyway' (OnClassCondition) - @ConditionalOnProperty (flyway.enabled) matched (OnPropertyCondition) - @ConditionalOnBean (types: javax.sql.DataSource; SearchStrategy: all) did not find any beans (OnBeanCondition) ... Exclusions: org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
这就是Spring的FlywayAutoConfiguration类的样子:
@Configuration
@ConditionalOnClass(Flyway.class)
@ConditionalOnBean(DataSource.class)
@ConditionalOnProperty(prefix = "flyway", name = "enabled", matchIfMissing = true)
@AutoConfigureAfter({ DataSourceAutoConfiguration.class,
HibernateJpaAutoConfiguration.class })
public class FlywayAutoConfiguration {
因此我认为它无效,因为DataSourceAutoConfiguration
被排除在自动配置之外。
这种分析是否正确?
排除DataSourceAutoConfiguration
的原因和位置?据说在Grails内部的某个地方,因为我不知道我的代码中有任何可能导致此问题的地方。
如何按照Flyway文档的承诺,将flyway集成到Grails中?我知道我可以通过resources.groovy手动完成(我的项目中的工作代码,受Grails Flyway plugin code的启发):
if (application.config.flyway.enabled != false) {
flyway(Flyway) { bean ->
bean.initMethod = 'migrate'
dataSource = ref('dataSource')
baselineOnMigrate = application.config.flyway.baselineOnMigrate
}
BeanDefinition sessionFactoryBeanDef = getBeanDefinition('sessionFactory')
if (sessionFactoryBeanDef) {
def dependsOnList = ['flyway'] as Set
if (sessionFactoryBeanDef.dependsOn?.length > 0) {
dependsOnList.addAll(sessionFactoryBeanDef.dependsOn)
}
sessionFactoryBeanDef.dependsOn = dependsOnList as String[]
}
}
但是如果可能的话,我更喜欢自动配置方法,因为它支持许多开箱即用的flyway属性,我可以保持我的资源。整洁。