我在我的项目中设置了 Flyway,具有以下目录布局(不包括不相关的目录):
$ tree database
database
├── lib
│ ├── flyway-ant-2.0.1.jar
│ ├── flyway-core-2.0.1.jar
│ ├── migrate.jar
│ ├── spring-beans-3.1.2.RELEASE.jar
│ ├── spring-core-3.1.2.RELEASE.jar
│ ├── spring-dao-2.0.8.jar
│ └── spring-jdbc-3.1.2.RELEASE.jar
└── updates
├── java
│ └── database
│ └── migrate
│ └── V1_0__create_posts.java
└── sql
└── V1_1__create_users.sql
以及 build.xml 的相关部分(flyway.url 和 flyway.driver 设置在此处未包含的属性文件中---Flyway 与数据库对话没有问题):
<target name="path">
<path id="migrate.build.path">
<fileset dir="database/lib">
<include name="flyway-*.jar"/>
</fileset>
<fileset dir="database/lib">
<include name="spring-*.jar"/>
</fileset>
</path>
<path id="flyway.lib.path">
<fileset dir="database/lib">
<include name="flyway-*.jar"/>
</fileset>
</path>
<path id="flyway.classpath">
<fileset dir="lib" includes="h2-*.jar"/>
<fileset dir="database/lib" includes="migrate.jar"/>
</path>
</target>
<target name="compile" depends="path">
<mkdir dir="build"/>
<javac srcdir="database/updates/java" destdir="build" includeantruntime="false" classpathref="migrate.build.path"/>
<copy todir="build">
<fileset dir="database/updates" excludes="**/*.java"/>
</copy>
<jar destfile="database/lib/migrate.jar" basedir="build" includes="sql/**,database/migrate/**"/>
</target>
<target name="init" depends="compile,path">
<!-- imported tasks -->
<taskdef uri="antlib:com.googlecode.flyway.ant" resource="com/googlecode/flyway/ant/antlib.xml" classpathref="flyway.lib.path"/>
<property name="flyway.locations" value="sql,database.migrate"/>
</target>
<!-- flyway tasks -->
<target name="db:clean" depends="init">
<flyway:clean/>
</target>
<target name="db:init" depends="init">
<flyway:init/>
</target>
<target name="db:migrate" depends="init">
<flyway:migrate/>
</target>
<target name="db:validate" depends="init">
<flyway:validate/>
</target>
<target name="db:info" depends="init">
<flyway:info/>
</target>
<target name="db:status" depends="init,db:info"/>
<target name="db:repair" depends="init">
<flyway:repair/>
</target>
这是在编译目标中创建的迁移 JAR 的布局:
$ unzip -l database/lib/migrate.jar
Archive: database/lib/migrate.jar
Length Date Time Name
-------- ---- ---- ----
0 01-14-13 16:27 META-INF/
103 01-14-13 16:27 META-INF/MANIFEST.MF
0 01-14-13 16:21 database/
0 01-14-13 16:27 database/migrate/
0 01-14-13 16:27 sql/
538 01-14-13 16:27 database/migrate/V1_0__create_posts.class
0 01-14-13 16:27 sql/V1_1__create_users.sql
-------- -------
641 7 files
这是来自的输出flyway:info
:
[flyway:info] +-------------+------------------------+---------------------+---------+
[flyway:info] | Version | Description | Installed on | State |
[flyway:info] +-------------+------------------------+---------------------+---------+
[flyway:info] | 0 | << Flyway Init >> | 2013-01-14 14:14:09 | Missing |
[flyway:info] | 1.1 | create users | | Pending |
[flyway:info] +-------------+------------------------+---------------------+---------+
Flyway 没有发现 .class 文件。我在这里做错了什么?
编辑:如果有人知道使用 Ant(没有 Maven,没有 Ivy)设置 Flyway 的工作示例,将不胜感激。
更新
事实证明 Flyway 会找到JdbcMigration
课程,但不会SpringJdbcMigration
。
此迁移已成功加载:
/* database/updates/java/database/migrate/V1_0__create_posts.java */
import com.googlecode.flyway.core.api.migration.jdbc.JdbcMigration;
import java.sql.Connection;
public class V1_0__create_posts implements JdbcMigration {
public void migrate(Connection conn) throws Exception {
conn.createStatement().execute(
"create table posts (id integer primary key, title varchar(255), body text)"
);
}
}
但是这个被跳过了:
/* database/updates/java/database/migrate/V1_0__create_posts.java */
import org.springframework.jdbc.core.JdbcTemplate;
import com.googlecode.flyway.core.api.migration.spring.SpringJdbcMigration;
public class V1_0__create_posts implements SpringJdbcMigration {
public void migrate(JdbcTemplate template) throws Exception {
template.execute(
"create table posts (id integer primary key, title varchar(255), body text)"
);
}
}
这是使用 a 时的 Ant 调试输出SpringJdbcMigration
:
[antlib:com.googlecode.flyway.ant] Could not load definitions from resource com/googlecode/flyway/ant/antlib.xml. It could not be found. [4/873]
[flyway:info] Database: H2 1.2
[flyway:info] DDL Transactions Supported: false
[flyway:info] Schema: PUBLIC
[flyway:info] No metadata table upgrade necessary
[flyway:info] Spring Jdbc available: false
[flyway:info] Scanning for resources at 'database/migrate' (Prefix: 'V', Suffix: '.sql')
[flyway:info] Scanning URL: jar:file:.../flyway-sample/database/lib/migrate.jar!/database/migrate
[flyway:info] JBoss VFS v2 available: false
[flyway:info] Filtering out resource: database/migrate/ (filename: )
[flyway:info] Filtering out resource: database/migrate/V1_0__create_posts.class (filename: V1_0__create_posts.class)
[flyway:info] Scanning for classes at 'database/migrate' (Implementing: 'com.googlecode.flyway.core.api.migration.jdbc.JdbcMigration')
[flyway:info] Scanning URL: jar:file:.../flyway-sample/database/lib/migrate.jar!/database/migrate
[flyway:info] Filtering out resource: database/migrate/ (filename: )
[flyway:info] Scanning for resources at 'db/migration' (Prefix: 'V', Suffix: '.sql')
[flyway:info] Unable to determine URL for classpath location: db/migration (ClassLoader: AntClassLoader[.../flyway-sample/lib/h2-1.2.135.jar:.../flyway-sample/database/lib/migrate.jar])
[flyway:info] Scanning for classes at 'db/migration' (Implementing: 'com.googlecode.flyway.core.api.migration.jdbc.JdbcMigration')
[flyway:info] Unable to determine URL for classpath location: db/migration (ClassLoader: AntClassLoader[.../flyway-sample/lib/h2-1.2.135.jar:.../flyway-sample/database/lib/migrate.jar])
[flyway:info] Scanning for resources at 'sql' (Prefix: 'V', Suffix: '.sql')
[flyway:info] Scanning URL: jar:file:.../flyway-sample/database/lib/migrate.jar!/sql
[flyway:info] Filtering out resource: sql/ (filename: )
[flyway:info] Found resource: sql/V1_1__create_users.sql
[flyway:info] Scanning for classes at 'sql' (Implementing: 'com.googlecode.flyway.core.api.migration.jdbc.JdbcMigration')
[flyway:info] Scanning URL: jar:file:.../flyway-sample/database/lib/migrate.jar!/sql
[flyway:info] Filtering out resource: sql/ (filename: )
[flyway:info] Filtering out resource: sql/V1_1__create_users.sql (filename: V1_1__create_users.sql)
[flyway:info] +-------------+------------------------+---------------------+---------+
[flyway:info] | Version | Description | Installed on | State |
[flyway:info] +-------------+------------------------+---------------------+---------+
[flyway:info] | 0 | << Flyway Init >> | 2013-01-14 14:14:09 | Missing |
[flyway:info] | 1.1 | create users | | Pending |
[flyway:info] +-------------+------------------------+---------------------+---------+