gpt4 book ai didi

java - 使用 Ant 时,Flyway 在类路径上找不到 Spring Java 迁移

转载 作者:行者123 更新时间:2023-11-30 04:36:03 25 4
gpt4 key购买 nike

我在项目中设置了 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 设置 Flyway 的工作示例(没有 Maven,没有 Ivy),我们将不胜感激。

<小时/>

更新

事实证明,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)"
);
}
}

以下是使用 SpringJdbcMigration 时的 Ant 调试输出:

[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] +-------------+------------------------+---------------------+---------+

最佳答案

必须首先编译Java 迁移。 (Flyway 查找 .class 文件)

我已更新the docs让这一点更清楚。

关于java - 使用 Ant 时,Flyway 在类路径上找不到 Spring Java 迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13504458/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com