发布这个 self 回答,这样当我在几个月后再次打破它时,答案实际上会出现在谷歌中。
简单的 Java 项目,其中 Flyway API 用于迁移数据库架构。遵循 V1__init.sql、V2__updateCustomerTable.sql 约定的 sql 脚本资源目录用于检查 schema_version 元数据表并在需要时进行迁移。
一切正常,直到 jar 被混淆为止。 sql脚本肯定是打包到jar文件里了,但是找不到:
2017-04-10 17:17:13,612 [main] DEBUG (?:?) - Validating migrations ...
2017-04-10 17:17:13,884 [main] DEBUG (?:?) - Scanning for classpath resources at 'classpath:db/migration/postgres' (Prefix: '', Suffix: '.sql')
2017-04-10 17:17:13,885 [main] DEBUG (?:?) - Determining location urls for classpath:db/migration/postgres using ClassLoader sun.misc.Launcher$AppClassLoader@8b819f ...
2017-04-10 17:17:13,885 [main] WARN (?:?) - Unable to resolve location classpath:db/migration/postgres
2017-04-10 17:17:13,899 [main] DEBUG (?:?) - Scanning for classpath resources at 'classpath:db/migration/postgres' (Prefix: 'V', Suffix: '.sql')
2017-04-10 17:17:13,900 [main] DEBUG (?:?) - Scanning for classpath resources at 'classpath:db/migration/postgres' (Prefix: 'R', Suffix: '.sql')
未保护的 jar 会通过以下日志消息找到它们(请注意,它现在在 jar 文件中查找):
2017-04-10 17:07:16,612 [main] DEBUG (?:?) - Validating migrations ...
2017-04-10 17:07:16,613 [main] DEBUG (?:?) - Scanning for classpath resources at 'classpath:db/migration/postgres' (Prefix: 'V', Suffix: '.sql')
2017-04-10 17:07:16,614 [main] DEBUG (?:?) - Scanning URL: jar:file:/C:/Dev/Sanbox/myjar.jar!/db/migration/postgres
2017-04-10 17:07:16,614 [main] DEBUG (?:?) - JBoss VFS v2 available: false
2017-04-10 17:07:16,615 [main] DEBUG (?:?) - Filtering out resource: db/migration/postgres/ (filename: )
2017-04-10 17:07:16,615 [main] DEBUG (?:?) - Found resource: db/migration/postgres/V1__init.sql
2017-04-10 17:07:16,616 [main] DEBUG (?:?) - Found resource: db/migration/postgres/V2__updateCustomerTable.sql
最佳答案
罪魁祸首是你需要告诉 proguard 在你的配置文件中单独保留 db/migrations 目录(或者你放置迁移脚本的任何地方),否则 Flyway 将无法找到你的脚本,即使它们看起来是在 jar 里的正确位置。这是我在 proguard 配置中需要的行:
-keepdirectories db/migration/**
如果您更改了 sql 脚本的放置位置,则此配置也需要更新。
关于java - Proguard 破坏 Flyway 数据库迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43333826/