title: Run Flyway migrate with spring boot explicitly
body: <p>I have a spring boot based application that I want to run Flyway migrations on. The app needs to run the flyway migrate only under certain circumstances. For instance if the &quot;migrate&quot; argument is passed to Main().</p>
  <p>There also are 2 different Datasources that need to be migrated.</p>
  <pre><code>@SpringBootApplication
  public class ExampleMain implements CommandLineRunner {
  
      public static void main(String[] args) {
          new SpringApplicationBuilder(ExampleMain.class)
                                      .web(WebApplicationType.NONE)
                                      .run(args);
      }
  
      @Override
      public void run(String... args) throws Exception {
          if (args[0].equals(&quot;migrate&quot;))
              migrate();
          else
              System.out.println(&quot;The world is round&quot;);
      }
      
      private void migrate() {
          createFlywayForDb1().migrate();
          createFlywayForDb2().migrate();
      }
      
      private Flyway createFlywayForDb1() {
          return createFlyway(&quot;XXX&quot;, &quot;XXX&quot;, &quot;XXX&quot;, new String[] { &quot;classpath:com/example/migrations/db1&quot; } );
      }
      private Flyway createFlywayForDb2() {
          return createFlyway(&quot;ZZZ&quot;, &quot;ZZZ&quot;, &quot;ZZZ&quot;, new String[] { &quot;classpath:com/example/migrations/db2&quot; } );
      }
      
      private Flyway createFlyway(String url, String userName, String userPassword, String[] locations) {
          return Flyway.configure()
                  .dataSource(url, userName, userPassword)
                  .locations(locations)
                  .ignoreMissingMigrations(true)
                  .ignoreIgnoredMigrations(true)
                  .load();
      }
  }
  </code></pre>
  <p>The Flyway.migrate() won't work in this case because the Java migration classes need their spring boot beans injected.  Is there some way to Autoload a Flyway instance?</p>
  <p>I've read <a href="https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto.data-initialization.migration-tool.flyway" rel="nofollow noreferrer">this</a> but can't find a way to do the above. Any help would be greatly appreciated!</p>