Rostyslav Rava - Personal Website

How to Avoid Maven Metadata Retrieval for Flyway Migrations

When running Flyway migrations with Maven by default, the build output shows that Maven fetches repository metadata on each run. If migrations are run frequently, it soon becomes distracting and annoying.

+ mvn flyway:migrate
[INFO] Scanning for projects...
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-metadata.xml
Downloading from central: https://repo.maven.apache.org/maven2/org/codehaus/mojo/maven-metadata.xml
Downloading from central: https://repo.maven.apache.org/maven2/org/flywaydb/maven-metadata.xml
Progress (1): 4.6 kB
Progress (2): 4.6 kB | 3.1 kB
Progress (2): 4.6 kB | 8.0 kB
Progress (2): 4.6 kB | 8.0 kB
Progress (2): 4.6 kB | 13 kB 
Progress (3): 4.6 kB | 13 kB | 234 B
Progress (3): 10 kB | 13 kB | 234 B 
Progress (3): 14 kB | 13 kB | 234 B
Progress (3): 14 kB | 17 kB | 234 B
Progress (3): 14 kB | 20 kB | 234 B
Progress (3): 14 kB | 21 kB | 234 B
                                   
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-metadata.xml (14 kB at 19 kB/s)
Downloaded from central: https://repo.maven.apache.org/maven2/org/flywaydb/maven-metadata.xml (234 B at 311 B/s)
Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/mojo/maven-metadata.xml (21 kB at 27 kB/s)
[INFO] 
[INFO] -----------------------< ...

This happens because Maven does not know where to find the Flyway short name and has to look up the plugin in metadata. As a result, it initiates plugin metadata retrieval from Maven Central and slows down migration execution.

It is possible to specify the full namespace to run Flyway migrations, as shown below, but the command is long and unwieldy.

mvn org.flywaydb:flyway-maven-plugin:migrate

As a solution to keep the command simple and avoid plugin metadata retrieval so migrations run faster, add the following configuration to the Maven settings file ~/.m2/settings.xml.

<settings>
  <pluginGroups>
    <pluginGroup>org.flywaydb</pluginGroup>
  </pluginGroups>
</settings>

After applying this configuration, migrations launched by Maven will run without creating additional traffic or retrieving plugin metadata.

+ mvn flyway:migrate
[INFO] Scanning for projects...
[INFO] 
[INFO] -----------------------< ...

An additional benefit is a slight reduction in migration time.

Before the configuration:

[INFO] Total time:  4.012 s

After the configuration, total time drops to around 3 seconds - a reduction of nearly one second:

[INFO] Total time:  3.032 s

This post has described the process of Maven configuration to avoid unnecessary plugin metadata retrieval each time the Flyway plugin is used, resulting in faster execution and cleaner migration output.

Tags: