Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spring Jdbc not available from command line #783

Closed
jvelliott opened this issue Jun 20, 2014 · 5 comments
Closed

Spring Jdbc not available from command line #783

jvelliott opened this issue Jun 20, 2014 · 5 comments

Comments

@jvelliott
Copy link

I have detailed this issue in a question on StackOverflow, but it appears to be a bug, instead of a configuration issue, as best as I can see.

In short, I have a jar file with two java migrations in the same package. One implements JdbcMigration, and one implements SpringJdbcMigration. The former is picked up by the command line migrate command, the latter is not.

When running with debug mode, it shows "Spring Jdbc available: false" despite it also showing the spring-jdbc jar being added to the classpath just a few lines above.

Below, see the output from debug, copied from my StackOverflow question, and with a few notes added:

[localhost ~/Documents/ws-src/flyway-3.0] ./flyway migrate -X
/usr/bin/tput
Flyway (Command-line Tool) v.3.0

DEBUG: Adding location to classpath: /Users/biggusjimmus/Documents/ws-src/flyway-3.0/bin/../jars/java-migration.jar
DEBUG: Adding location to classpath: /Users/biggusjimmus/Documents/ws-src/flyway-3.0/bin/../jars/mysql-connector-java-5.1.26-bin.jar
#
# NOTE: This library SHOULD enable Spring Jdbc, as far as I know.
#
DEBUG: Adding location to classpath: /Users/biggusjimmus/Documents/ws-src/flyway-3.0/bin/../jars/spring-jdbc-4.0.0.RELEASE.jar
Database: jdbc:mysql://localhost:3306/myproject (MySQL 5.6)
DEBUG: DDL Transactions Supported: false
DEBUG: Schema: myproject
#
#NOTE: spring-jdbc-4.0.0.RELEASE.jar was apparently added to the classpath above!
#
DEBUG: Spring Jdbc available: false
DEBUG: Spring Jdbc available: false
DEBUG: Validating migrations ...
DEBUG: Scanning for classpath resources at 'com/mycompany/myproject/db/migration' (Prefix: 'V', Suffix: '.sql')
DEBUG: Scanning URL: jar:file:/Users/biggusjimmus/Documents/ws-src/flyway-3.0/bin/../jars/java-migration.jar!/com/mycompany/myproject/db/migration
DEBUG: JBoss VFS v2 available: false
DEBUG: Filtering out resource: com/mycompany/myproject/db/migration/ (filename: )
DEBUG: Filtering out resource: com/mycompany/myproject/db/migration/V1_1_4_2__Java_Spring_Example.class (filename: V1_1_4_2__Java_Spring_Example.class)
DEBUG: Filtering out resource: com/mycompany/myproject/db/migration/V1_1_4_3__Java_NoSpring_Example.class (filename: V1_1_4_3__Java_NoSpring_Example.class)
DEBUG: Scanning for classes at 'com/mycompany/myproject/db/migration' (Implementing: 'org.flywaydb.core.api.migration.jdbc.JdbcMigration')
DEBUG: Scanning URL: jar:file:/Users/biggusjimmus/Documents/ws-src/flyway-3.0/bin/../jars/java-migration.jar!/com/mycompany/myproject/db/migration
DEBUG: JBoss VFS v2 available: false
DEBUG: Filtering out resource: com/mycompany/myproject/db/migration/ (filename: )
#
# NOTE: Why did it find the NoSpring example (which implements JdbcMigration), 
# but not the spring example (which implements SpringJdbcMigration), even though 
# it's in the same package? 
# Presumably because it doesn't think it has Spring Jdbc support
#
DEBUG: Found class: com.mycompany.myproject.db.migration.V1_1_4_3__Java_NoSpring_Example
DEBUG: Scanning for filesystem resources at 'sql' (Prefix: 'V', Suffix: '.sql')
DEBUG: Scanning for resources in path: sql (sql)
DEBUG: Filtering out resource: sql/.DS_Store (filename: .DS_Store)
DEBUG: Found filesystem resource: sql/V1_1_3__Base_version.sql
DEBUG: Found filesystem resource: sql/V1_1_4_1__SQL_Example.sql
Validated 3 migrations (execution time 00:00.052s)
DEBUG: Schema `myproject` already exists. Skipping schema creation.
DEBUG: Locking table `myproject`.`schema_version`...
DEBUG: Lock acquired for table `myproject`.`schema_version`
Current version of schema `myproject`: 1.1.3
Migrating schema `myproject` to version 1.1.4.1
DEBUG: Successfully completed and committed migration of schema `myproject` to version 1.1.4.1
DEBUG: MetaData table `myproject`.`schema_version` successfully updated to reflect changes
DEBUG: Locking table `myproject`.`schema_version`...
DEBUG: Lock acquired for table `myproject`.`schema_version`
Migrating schema `myproject` to version 1.1.4.3
DEBUG: Successfully completed and committed migration of schema `myproject` to version 1.1.4.3
DEBUG: MetaData table `myproject`.`schema_version` successfully updated to reflect changes
DEBUG: Locking table `myproject`.`schema_version`...
DEBUG: Lock acquired for table `myproject`.`schema_version`
Successfully applied 2 migrations to schema `myproject` (execution time 00:00.049s).

This same file is parsed fine when running Flyway within my application, but our process depends on running it externally, which likely means command line.

If this is a configuration issue on my end, or a bug, please let me know.

@jvelliott
Copy link
Author

I've also tried adding the spring-core, spring-tx jars into jars with no luck

@jvelliott
Copy link
Author

So, I found a way to make it work, but it requires modification of the classpath in the "flyway" shell script, which seems dubious, and also requires the inclusion of FIVE dependencies, without which I either get the behavior described in the issue, or a ClassNotFoundException.

Specifically, I need to add the following libraries:
commons-logging
spring-beans:
spring-core
spring-jdbc
spring-tx

And change https://github.com/flyway/flyway/blob/flyway-3.0/flyway-commandline/src/main/assembly/flyway#L52 to

"$JAVA_CMD" -cp ./bin/flyway-commandline-${project.version}.jar:./bin/flyway-core-${project.version}.jar:./jars/* org.flywaydb.commandline.Main $@ -consoleWidth=$CONSOLE_WIDTH

or in 4.0, https://github.com/flyway/flyway/blob/master/flyway-commandline/src/main/assembly/flyway#L57 to

CP="./bin/flyway-commandline-${project.version}.jar:./bin/flyway-core-${project.version}.jar:./jars/*"

Is there a smarter way to do this? Is this documented anywhere?

@axelfontaine
Copy link
Contributor

Thanks for the report. Flyway will automatically load all jar files in the /jars directory.

Did you try simply placing all 5 jar files there?

@jvelliott
Copy link
Author

It looks like it's trying to load all the jars, but I get a NoClassDefFoundError when I don't modify the classpath in the script:

Without modifying classpath:

[localhost ~/Documents/ws-src/flyway-3.0] sh -x flyway migrate -X
++ pwd
+ OLDDIR=/Users/jelliott/Documents/ws-src/flyway-3.0
+ PRG=flyway
+ '[' -h flyway ']'
++ dirname flyway
+ INSTALLDIR=.
+ cd .
+ '[' -z '' ']'
+ JAVA_CMD=java
+ command -v tput
/usr/bin/tput
++ tput cols
+ CONSOLE_WIDTH=116
+ java -cp ./bin/flyway-commandline-3.0.jar:./bin/flyway-core-3.0.jar org.flywaydb.commandline.Main migrate -X -consoleWidth=116
Flyway (Command-line Tool) v.3.0

DEBUG: Adding location to classpath: /Users/jelliott/Documents/ws-src/flyway-3.0/bin/../jars/commons-logging-1.1.1.jar
DEBUG: Adding location to classpath: /Users/jelliott/Documents/ws-src/flyway-3.0/bin/../jars/java-migration.jar
DEBUG: Adding location to classpath: /Users/jelliott/Documents/ws-src/flyway-3.0/bin/../jars/mysql-connector-java-5.1.26-bin.jar
DEBUG: Adding location to classpath: /Users/jelliott/Documents/ws-src/flyway-3.0/bin/../jars/spring-beans-4.0.0.RELEASE.jar
DEBUG: Adding location to classpath: /Users/jelliott/Documents/ws-src/flyway-3.0/bin/../jars/spring-core-4.0.0.RELEASE.jar
DEBUG: Adding location to classpath: /Users/jelliott/Documents/ws-src/flyway-3.0/bin/../jars/spring-jdbc-4.0.0.RELEASE.jar
DEBUG: Adding location to classpath: /Users/jelliott/Documents/ws-src/flyway-3.0/bin/../jars/spring-tx-3.1.2.RELEASE.jar
Database: jdbc:mysql://localhost:3306/myproject (MySQL 5.6)
DEBUG: DDL Transactions Supported: false
DEBUG: Schema: myproject
DEBUG: Spring Jdbc available: true
DEBUG: Spring Jdbc available: true
DEBUG: Validating migrations ...
DEBUG: Scanning for classpath resources at 'com/mycompany/myproject/db/migration' (Prefix: 'V', Suffix: '.sql')
DEBUG: Scanning URL: jar:file:/Users/jelliott/Documents/ws-src/flyway-3.0/bin/../jars/java-migration.jar!/com/mycompany/myproject/db/migration
DEBUG: JBoss VFS v2 available: false
DEBUG: Filtering out resource: com/mycompany/myproject/db/migration/ (filename: )
DEBUG: Filtering out resource: com/mycompany/myproject/db/migration/V1_1_4_2__Java_Spring_Example.class (filename: V1_1_4_2__Java_Spring_Example.class)
DEBUG: Filtering out resource: com/mycompany/myproject/db/migration/V1_1_4_3__Java_NoSpring_Example.class (filename: V1_1_4_3__Java_NoSpring_Example.class)
DEBUG: Scanning for classes at 'com/mycompany/myproject/db/migration' (Implementing: 'org.flywaydb.core.api.migration.jdbc.JdbcMigration')
DEBUG: Scanning URL: jar:file:/Users/jelliott/Documents/ws-src/flyway-3.0/bin/../jars/java-migration.jar!/com/mycompany/myproject/db/migration
DEBUG: JBoss VFS v2 available: false
DEBUG: Filtering out resource: com/mycompany/myproject/db/migration/ (filename: )
DEBUG: Found class: com.mycompany.myproject.db.migration.V1_1_4_3__Java_NoSpring_Example
DEBUG: Scanning for classes at 'com/mycompany/myproject/db/migration' (Implementing: 'org.flywaydb.core.api.migration.spring.SpringJdbcMigration')
DEBUG: Scanning URL: jar:file:/Users/jelliott/Documents/ws-src/flyway-3.0/bin/../jars/java-migration.jar!/com/mycompany/myproject/db/migration
DEBUG: JBoss VFS v2 available: false
DEBUG: Filtering out resource: com/mycompany/myproject/db/migration/ (filename: )
DEBUG: Found class: com.mycompany.myproject.db.migration.V1_1_4_2__Java_Spring_Example
DEBUG: Scanning for filesystem resources at 'sql' (Prefix: 'V', Suffix: '.sql')
DEBUG: Scanning for resources in path: sql (sql)
DEBUG: Filtering out resource: sql/.DS_Store (filename: .DS_Store)
DEBUG: Found filesystem resource: sql/V1_1_3__Base_version.sql
DEBUG: Found filesystem resource: sql/V1_1_4_1__SQL_Example.sql
Validated 4 migrations (execution time 00:00.056s)
DEBUG: Schema `myproject` already exists. Skipping schema creation.
DEBUG: Locking table `myproject`.`schema_version`...
DEBUG: Lock acquired for table `myproject`.`schema_version`
Current version of schema `myproject`: 1.1.3
Migrating schema `myproject` to version 1.1.4.1
DEBUG: Successfully completed and committed migration of schema `myproject` to version 1.1.4.1
DEBUG: MetaData table `myproject`.`schema_version` successfully updated to reflect changes
DEBUG: Locking table `myproject`.`schema_version`...
DEBUG: Lock acquired for table `myproject`.`schema_version`
Migrating schema `myproject` to version 1.1.4.2
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/jdbc/core/JdbcTemplate
    at org.flywaydb.core.internal.resolver.spring.SpringJdbcMigrationExecutor.execute(SpringJdbcMigrationExecutor.java:46)
    at org.flywaydb.core.internal.command.DbMigrate$5.doInTransaction(DbMigrate.java:287)
    at org.flywaydb.core.internal.command.DbMigrate$5.doInTransaction(DbMigrate.java:285)
    at org.flywaydb.core.internal.util.jdbc.TransactionTemplate.execute(TransactionTemplate.java:72)
    at org.flywaydb.core.internal.command.DbMigrate.applyMigration(DbMigrate.java:285)
    at org.flywaydb.core.internal.command.DbMigrate.access$800(DbMigrate.java:46)
    at org.flywaydb.core.internal.command.DbMigrate$2.doInTransaction(DbMigrate.java:207)
    at org.flywaydb.core.internal.command.DbMigrate$2.doInTransaction(DbMigrate.java:156)
    at org.flywaydb.core.internal.util.jdbc.TransactionTemplate.execute(TransactionTemplate.java:72)
    at org.flywaydb.core.internal.command.DbMigrate.migrate(DbMigrate.java:156)
    at org.flywaydb.core.Flyway$1.execute(Flyway.java:864)
    at org.flywaydb.core.Flyway$1.execute(Flyway.java:811)
    at org.flywaydb.core.Flyway.execute(Flyway.java:1171)
    at org.flywaydb.core.Flyway.migrate(Flyway.java:811)
    at org.flywaydb.commandline.Main.executeOperation(Main.java:120)
    at org.flywaydb.commandline.Main.main(Main.java:88)
Caused by: java.lang.ClassNotFoundException: org.springframework.jdbc.core.JdbcTemplate
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 16 more
+ JAVA_EXIT_CODE=1
+ cd /Users/jelliott/Documents/ws-src/flyway-3.0
+ exit 1

After modifying the classpath:

[localhost ~/Documents/ws-src/flyway-3.0] sh -x flyway migrate -X
++ pwd
+ OLDDIR=/Users/jelliott/Documents/ws-src/flyway-3.0
+ PRG=flyway
+ '[' -h flyway ']'
++ dirname flyway
+ INSTALLDIR=.
+ cd .
+ '[' -z '' ']'
+ JAVA_CMD=java
+ command -v tput
/usr/bin/tput
++ tput cols
+ CONSOLE_WIDTH=116
+ java -cp './bin/flyway-commandline-3.0.jar:./bin/flyway-core-3.0.jar:./jars/*' org.flywaydb.commandline.Main migrate -X -consoleWidth=116
Flyway (Command-line Tool) v.3.0

DEBUG: Adding location to classpath: /Users/jelliott/Documents/ws-src/flyway-3.0/bin/../jars/commons-logging-1.1.1.jar
DEBUG: Adding location to classpath: /Users/jelliott/Documents/ws-src/flyway-3.0/bin/../jars/java-migration.jar
DEBUG: Adding location to classpath: /Users/jelliott/Documents/ws-src/flyway-3.0/bin/../jars/mysql-connector-java-5.1.26-bin.jar
DEBUG: Adding location to classpath: /Users/jelliott/Documents/ws-src/flyway-3.0/bin/../jars/spring-beans-4.0.0.RELEASE.jar
DEBUG: Adding location to classpath: /Users/jelliott/Documents/ws-src/flyway-3.0/bin/../jars/spring-core-4.0.0.RELEASE.jar
DEBUG: Adding location to classpath: /Users/jelliott/Documents/ws-src/flyway-3.0/bin/../jars/spring-jdbc-4.0.0.RELEASE.jar
DEBUG: Adding location to classpath: /Users/jelliott/Documents/ws-src/flyway-3.0/bin/../jars/spring-tx-3.1.2.RELEASE.jar
Database: jdbc:mysql://localhost:3306/myproject (MySQL 5.6)
DEBUG: DDL Transactions Supported: false
DEBUG: Schema: myproject
DEBUG: Spring Jdbc available: true
DEBUG: Spring Jdbc available: true
DEBUG: Validating migrations ...
DEBUG: Scanning for classpath resources at 'com/mycompany/myproject/db/migration' (Prefix: 'V', Suffix: '.sql')
DEBUG: Scanning URL: jar:file:/Users/jelliott/Documents/ws-src/flyway-3.0/jars/java-migration.jar!/com/mycompany/myproject/db/migration
DEBUG: JBoss VFS v2 available: false
DEBUG: Scanning URL: jar:file:/Users/jelliott/Documents/ws-src/flyway-3.0/bin/../jars/java-migration.jar!/com/mycompany/myproject/db/migration
DEBUG: JBoss VFS v2 available: false
DEBUG: Filtering out resource: com/mycompany/myproject/db/migration/ (filename: )
DEBUG: Filtering out resource: com/mycompany/myproject/db/migration/V1_1_4_2__Java_Spring_Example.class (filename: V1_1_4_2__Java_Spring_Example.class)
DEBUG: Filtering out resource: com/mycompany/myproject/db/migration/V1_1_4_3__Java_NoSpring_Example.class (filename: V1_1_4_3__Java_NoSpring_Example.class)
DEBUG: Scanning for classes at 'com/mycompany/myproject/db/migration' (Implementing: 'org.flywaydb.core.api.migration.jdbc.JdbcMigration')
DEBUG: Scanning URL: jar:file:/Users/jelliott/Documents/ws-src/flyway-3.0/jars/java-migration.jar!/com/mycompany/myproject/db/migration
DEBUG: JBoss VFS v2 available: false
DEBUG: Scanning URL: jar:file:/Users/jelliott/Documents/ws-src/flyway-3.0/bin/../jars/java-migration.jar!/com/mycompany/myproject/db/migration
DEBUG: JBoss VFS v2 available: false
DEBUG: Filtering out resource: com/mycompany/myproject/db/migration/ (filename: )
DEBUG: Found class: com.mycompany.myproject.db.migration.V1_1_4_3__Java_NoSpring_Example
DEBUG: Scanning for classes at 'com/mycompany/myproject/db/migration' (Implementing: 'org.flywaydb.core.api.migration.spring.SpringJdbcMigration')
DEBUG: Scanning URL: jar:file:/Users/jelliott/Documents/ws-src/flyway-3.0/jars/java-migration.jar!/com/mycompany/myproject/db/migration
DEBUG: JBoss VFS v2 available: false
DEBUG: Scanning URL: jar:file:/Users/jelliott/Documents/ws-src/flyway-3.0/bin/../jars/java-migration.jar!/com/mycompany/myproject/db/migration
DEBUG: JBoss VFS v2 available: false
DEBUG: Filtering out resource: com/mycompany/myproject/db/migration/ (filename: )
DEBUG: Found class: com.mycompany.myproject.db.migration.V1_1_4_2__Java_Spring_Example
DEBUG: Scanning for filesystem resources at 'sql' (Prefix: 'V', Suffix: '.sql')
DEBUG: Scanning for resources in path: sql (sql)
DEBUG: Filtering out resource: sql/.DS_Store (filename: .DS_Store)
DEBUG: Found filesystem resource: sql/V1_1_3__Base_version.sql
DEBUG: Found filesystem resource: sql/V1_1_4_1__SQL_Example.sql
Validated 4 migrations (execution time 00:00.061s)
DEBUG: Schema `myproject` already exists. Skipping schema creation.
DEBUG: Locking table `myproject`.`schema_version`...
DEBUG: Lock acquired for table `myproject`.`schema_version`
Current version of schema `myproject`: 1.1.3
Migrating schema `myproject` to version 1.1.4.1
DEBUG: Successfully completed and committed migration of schema `myproject` to version 1.1.4.1
DEBUG: MetaData table `myproject`.`schema_version` successfully updated to reflect changes
DEBUG: Locking table `myproject`.`schema_version`...
DEBUG: Lock acquired for table `myproject`.`schema_version`
Migrating schema `myproject` to version 1.1.4.2
DEBUG: Successfully completed and committed migration of schema `myproject` to version 1.1.4.2
DEBUG: MetaData table `myproject`.`schema_version` successfully updated to reflect changes
DEBUG: Locking table `myproject`.`schema_version`...
DEBUG: Lock acquired for table `myproject`.`schema_version`
Migrating schema `myproject` to version 1.1.4.3
DEBUG: Successfully completed and committed migration of schema `myproject` to version 1.1.4.3
DEBUG: MetaData table `myproject`.`schema_version` successfully updated to reflect changes
DEBUG: Locking table `myproject`.`schema_version`...
DEBUG: Lock acquired for table `myproject`.`schema_version`
Successfully applied 3 migrations to schema `myproject` (execution time 00:00.134s).
+ JAVA_EXIT_CODE=0
+ cd /Users/jelliott/Documents/ws-src/flyway-3.0
+ exit 0

EDIT: Added shell script debug output.

@axelfontaine axelfontaine added this to the Flyway 3.1 milestone Oct 7, 2014
@Lucas3oo
Copy link

This issue has resurfaced in at least version 5.2.4.
I.e. Java migrations that depends on third party jars like Spring-jdbc are not loaded on the class path when the migration is executed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants