Dubstef
Dubstef

Reputation: 103

Flyway migration execution from jar file

I integrated flyway into a new project and if I execute the class throw the IDE, the migration works fine. But also I want to let the migration start over the jar file from command line. But if I build the project with maven and execute the jar file, I get an exception:

Exception in thread "main" java.lang.NoClassDefFoundError: org/flywaydb/core/api/migration/jdbc/JdbcMigration
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at de.toom.controller.ContentMigrationApplication.main(ContentMigrationApplication.java:5)
Caused by: java.lang.ClassNotFoundException: org.flywaydb.core.api.migration.jdbc.JdbcMigration
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

If I use the SpringJdbcMigration as mentioned in other threads, I get the same exception.

pom.xml:

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/*.sql</include>
            </includes>
        </resource>
    </resources>

    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.5.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>java</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <mainClass>com.test.controller.ContentMigrationApplication</mainClass>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.0.1</version>
            <configuration>
              <archive>
                <manifest>
                  <addClasspath>true</addClasspath>
                  <mainClass>com.test.controller.ContentMigrationApplication</mainClass>
                </manifest>
              </archive>
            </configuration>
      </plugin>
    </plugins>
</build>

<dependencies>
    <!-- ====================================================================== -->
    <!-- External dependencies -->
    <!-- ====================================================================== -->
    <dependency>
        <groupId>org.flywaydb</groupId>
        <artifactId>flyway-core</artifactId>
        <version>4.0.3</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.39</version>
    </dependency>
</dependencies>

ContentMigrationApplication.java:

public class ContentMigrationApplication {
    public static void main(String[] args) {
        ContentMysqlMigration contentMysqlMigration = new ContentMysqlMigration();

        contentMysqlMigration.migrateDatabase(args);
    }
}

ContentMysqlMigration.java

public class ContentMysqlMigration implements JdbcMigration {
    private static final String DATABASE_DRIVER_CONNECTION = "jdbc:mysql://";
    private static final String SERVER_ADDRESS = "localhost";
    private static final String SERVER_ADDRESS_PORT = ":3306";
    private static final String SERVER_DATABASE_NAME = "/test";
    private static final String SERVER_USERNAME = "test";
    private static final String SERVER_PASSWORD = "test";

    public ContentMysqlMigration() {}

    public void migrateDatabase(String[] args) {
        Flyway flyway = new Flyway();

        flyway.setBaselineOnMigrate(true);

        flyway.setDataSource(DATABASE_DRIVER_CONNECTION + SERVER_ADDRESS + SERVER_ADDRESS_PORT + SERVER_DATABASE_NAME,
                SERVER_USERNAME, SERVER_PASSWORD);

        flyway.migrate();
    }

    public void migrate(Connection connection) throws Exception {}
}

Upvotes: 2

Views: 3372

Answers (1)

michael_bitard
michael_bitard

Reputation: 4212

That's because your jar doesn't contains the dependencies.

You see java.lang.NoClassDefFoundError: org/flywaydb/core/api/migration/jdbc/JdbcMigration?

Your jar contains only the .class of your sources.

Upvotes: 4

Related Questions