title: How to use Flyway in a Grails 3 project with Spring Boot's autoconfig
body: <p>According to the <a href="https://flywaydb.org/documentation/plugins/grails" rel="nofollow noreferrer">Flyway documentation</a> it should be possible to use Flyway in a Grails 3 project out-of-the-box:</p>
  
  <blockquote>
    <p>Grails 3.x is based on Spring Boot comes with out-of-the-box integration for Flyway.<br>
    All you need to do is add flyway-core to your build.gradle:<br>
    <code>compile "org.flywaydb:flyway-core:4.1.1" </code><br>
    Spring Boot will then automatically autowire Flyway with its DataSource and invoke it on startup.</p>
  </blockquote>
  
  <p>This doesn't work for me. Flyway does not kick in at application startup. In the logs I see some suspicious lines:</p>
  
  <blockquote>
  <pre>
     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
  </pre>
  </blockquote>
  
  <p>And this is how Spring's FlywayAutoConfiguration class looks like:</p>
  
  <pre><code>@Configuration
  @ConditionalOnClass(Flyway.class)
  @ConditionalOnBean(DataSource.class)
  @ConditionalOnProperty(prefix = "flyway", name = "enabled", matchIfMissing = true)
  @AutoConfigureAfter({ DataSourceAutoConfiguration.class,
          HibernateJpaAutoConfiguration.class })
  public class FlywayAutoConfiguration {
  </code></pre>
  
  <p>So it looks to me that it is not working because <code>DataSourceAutoConfiguration</code> is excluded from auto-config.</p>
  
  <p>Is this analysis correct?</p>
  
  <p>Why and where is <code>DataSourceAutoConfiguration</code> excluded? Supposedly somewhere deep inside of Grails, because I am not aware of any place in my code that could cause this.</p>
  
  <p>How can I make the flyway integration into Grails work as promised by the Flyway docs? I know I can do it manually via resources.groovy (working code from my project, heavily inspired by <a href="https://github.com/saw303/grails-flyway/blob/master/src/main/groovy/ch/silviowangler/grails/flyway/GrailsFlywayGrailsPlugin.groovy" rel="nofollow noreferrer">Grails Flyway plugin code</a>):</p>
  
  <pre><code>if (application.config.flyway.enabled != false) {
      flyway(Flyway) { bean -&gt;
          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 &gt; 0) {
              dependsOnList.addAll(sessionFactoryBeanDef.dependsOn)
          }
          sessionFactoryBeanDef.dependsOn = dependsOnList as String[]
      }
  }
  </code></pre>
  
  <p>but if possible I'd prefer the auto-config approach because it supports many flyway properties out-of-the-box and I can keep my resources.groovy tidy.</p>