Skip to content

Commit

Permalink
Use classpath*: for searching in SpringResourceAccessor
Browse files Browse the repository at this point in the history
#1595
LB-1060
  • Loading branch information
nvoxland committed Feb 15, 2021
1 parent b895e67 commit 4d2003a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
Expand Up @@ -102,10 +102,10 @@ protected String getResourcePath(Resource resource) {
if (url.contains("!")) {
return url.replaceFirst(".*!", "");
} else {
while (!resourceLoader.getResource("classpath:"+url).exists()) {
while (!resourceLoader.getResource("classpath:" + url).exists()) {
String newUrl = url.replaceFirst("^/?.*?/", "");
if (newUrl.equals(url)) {
throw new UnexpectedLiquibaseException("Could determine path for "+resource.getURL().toExternalForm());
throw new UnexpectedLiquibaseException("Could determine path for " + resource.getURL().toExternalForm());
}
url = newUrl;
}
Expand All @@ -115,7 +115,7 @@ protected String getResourcePath(Resource resource) {
} catch (IOException e) {
//the path gets stored in the databasechangelog table, so if it gets returned incorrectly it will cause future problems.
//so throw a breaking error now rather than wait for bigger problems down the line
throw new UnexpectedLiquibaseException("Cannot determine resource path for "+resource.getDescription());
throw new UnexpectedLiquibaseException("Cannot determine resource path for " + resource.getDescription());
}
}

Expand Down Expand Up @@ -163,12 +163,12 @@ protected boolean resourceIsFile(Resource resource) throws IOException {
* Default implementation adds "classpath:" and removes duplicated /'s and classpath:'s
*/
protected String finalizeSearchPath(String searchPath) {
searchPath = "classpath:"+searchPath;
searchPath = searchPath
.replaceAll("classpath\\*:", "classpath:")
.replace("\\", "/")
.replaceAll("//+", "/")
.replace("classpath:classpath:", "classpath:");
searchPath = searchPath.replace("\\", "/");
searchPath = searchPath.replaceAll("classpath\\*?:", "");
searchPath = "/" + searchPath;
searchPath = searchPath.replaceAll("//+", "/");

searchPath = "classpath*:" + searchPath;

return searchPath;
}
Expand Down
Expand Up @@ -2,6 +2,7 @@ package liquibase.integration.spring

import org.springframework.core.io.DefaultResourceLoader
import spock.lang.Specification
import spock.lang.Unroll

class SpringResourceAccessorTest extends Specification {

Expand Down Expand Up @@ -75,5 +76,23 @@ class SpringResourceAccessorTest extends Specification {
list.contains("MSSQLDatabaseTest.class,")
}

@Unroll
def finalizeSearchPath() {
expect:
new SpringResourceAccessor().finalizeSearchPath(input) == expected

where:
input | expected
"/path/to/file" | "classpath*:/path/to/file"
"//path////to/file" | "classpath*:/path/to/file"
"path/to/file" | "classpath*:/path/to/file"
"classpath:path/to/file" | "classpath*:/path/to/file"
"classpath:/path/to/file" | "classpath*:/path/to/file"
"classpath:classpath:/path/to/file" | "classpath*:/path/to/file"
"classpath*:/path/to/file" | "classpath*:/path/to/file"
"classpath*:path/to/file" | "classpath*:/path/to/file"

}


}

1 comment on commit 4d2003a

@dserik
Copy link

@dserik dserik commented on 4d2003a Oct 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in java 17 can not find files nested in folders.
For example:
ResourcePatternUtils.getResourcePatternResolver(resourceLoader).getResources("classpath:/core/changelog/load_data.xml") works fine.
ResourcePatternUtils.getResourcePatternResolver(resourceLoader).getResources("classpath*:/core/changelog/load_data.xml") File not found

Please sign in to comment.