wlnirvana
wlnirvana

Reputation: 2047

Why flyway's database driver dependency is not declared within the plugin itself in maven's pom.xml?

Most plugins relying on some other packages tend to declare the dependency in the plugin configuration. For example, spotbugs' doc does this

<plugin>
  <groupId>com.github.spotbugs</groupId>
  <artifactId>spotbugs-maven-plugin</artifactId>
  <version>4.2.0</version>
  <dependencies>
    <!-- overwrite dependency on spotbugs if you want to specify the version of spotbugs -->
    <dependency>
      <groupId>com.github.spotbugs</groupId>
      <artifactId>spotbugs</artifactId>
      <version>4.2.3</version>
    </dependency>
  </dependencies>
</plugin>

The version of spotbugs "core" is specified in plugin > dependencies > dependency.

However, flyway seems not working this way. For example, the following config where the database driver is in <dependencies> runs just fine.

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

    ...

    <build>
        <plugins>
            <plugin>
                <groupId>org.flywaydb</groupId>
                <artifactId>flyway-maven-plugin</artifactId>
                <version>${flyway.version}</version>
                <configuration>
                    <url>jdbc:mysql://localhost:3306/mydb</url>
                    <user>root</user>
                    <password>root</password>
                </configuration>
            </plugin>
        </plugins>
    </build>

Questions:

  1. Does this mean dependencies (at least compile and runtime scoped ones) are in the classpath of build-time goals as well?
  2. This might be subjective, but if there is indeed a best practice, it will obviously help beginners like me. So is it good/common coding style to write flyway's dependency in the "global" <dependencies> tag?

Upvotes: 2

Views: 650

Answers (2)

wlnirvana
wlnirvana

Reputation: 2047

Taking mvn flyway:migrate as an example.

The source mojo has requiresDependencyResolution = ResolutionScope.TEST configured in its annotation, which according to maven's doc, will enable access to pretty much all dependencies defined in <dependencies>.

Then in AbstractFlywayMojo.java (which is a super class for MigrateMojo.java), compile and runtime classpath elements are added to classloader. It is later processed by org.flywaydb.core.internal.scanner.Scanner to load the necessary class.

Upvotes: 0

Basil Bourque
Basil Bourque

Reputation: 338775

Drivers are bundled, for use without a Java project

For use on the command-line and scripting, Flyway comes bundled with JDBC drivers for several databases.

This bundling is done so that non-Java developers and sysadmins might use the tool as-is, without needing to establish a Java environment.

See the documentation for Supported Databases. Each page for each database product mentions whether the driver is included or not.

Flyway may not have bundled the very latest JDBC drivers. But that rarely matters as Flyway uses so very little of the JDBC API. All Flyway does little more than execute your SQL scripts, and record those executions. That work involves very few calls to the more basic features of JDBC, unlikely to be affected by minor driver updates.

Bring your own driver, for use within a Java project

If using Flyway from within a Java project, then you should already have your choice of JDBC driver installed for your particular database.

There are multiple versions of multiple kinds of multiple drivers from multiple vendors for various databases. Flyway cannot know what is appropriate to your situation. So it is not Flyway’s responsibility to install a JDBC driver into your Java project. That is your responsibility.

Note that dependency management & build configuration tools such as Maven have features for installing a dependency for use only in the IDE but not in deployment. In some scenarios, such as with some app servers like Apache Tomcat, you may need to install the JDBC driver separately rather than bundling within your JAR/WAR/EAR file.

Upvotes: 1

Related Questions