我将flyway集成到一个新项目中,如果我执行该类抛出IDE,则迁移工作正常。但是我也希望从命令行开始从jar文件开始迁移。但是如果我使用maven构建项目并执行jar文件,我会得到一个例外:
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)
如果我使用其他线程中提到的SpringJdbcMigration,我会得到相同的异常。
的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 {}
}
答案 0 :(得分:4)
那是因为你的jar不包含依赖项。
您看到java.lang.NoClassDefFoundError: org/flywaydb/core/api/migration/jdbc/JdbcMigration
?
你的jar只包含你的.class类。