Spring: Ant-like files pattern matching
Spring Batch job definition:
<bean>
<property name="resources" value="file:/${path}/some.*.pattern.zip"/>
...
</bean>
"${path}/some.*.pattern.zip" is right, I can guarantee that. Double-checked!
So why Spring Batch complaints "No resources to read" and does nothing?
Argh .. probably, has something to do with files pattern matching.
All right, I was curious about Ant-like files pattern matching in non-Ant environments for a long time already.
How one does that?
For "maven-copy-plugin" I’ve managed with "file-management" module:
<dependency>
<groupid>org.apache.maven.shared</groupid>
<artifactid>file-management</artifactid>
<version>1.2.1</version>
<scope>compile</scope>
</dependency>
import org.apache.maven.shared.model.fileset.FileSet; import org.apache.maven.shared.model.fileset.util.FileSetManager; FileSet fs = new FileSet(); fs.setDirectory( .. ); fs.setIncludes( .. ); fs.setExcludes( .. ); String[] files = new FileSetManager().getIncludedFiles( fs )
but that’s Maven. How is it done in Spring?
Well, debugging is always fun!
AbstractApplicationContext.getResources()PathMatchingResourcePatternResolver.getResources()PathMatchingResourcePatternResolver.findPathMatchingResources()PathMatchingResourcePatternResolver.doFindPathMatchingFileResources()PathMatchingResourcePatternResolver.retrieveMatchingFiles()PathMatchingResourcePatternResolver.doRetrieveMatchingFiles()
.. finally!AntPathMatcher.match()
.. how?AntPathMatcher.doMatch()– a FishEye link. As I expected, the matching process is quite involved ..
"org.springframework.util.AntPathMatcher". Doesn’t surprise me, "org.springframework.util" package was always full of nice utilities. One more to a toolbox!
P.S.
"No resources to read" ?
"file:/" was not required:
<bean>
<property name="resources" value="${path}/some.*.pattern.zip"/>
...
</bean>
Previous version was converted to something like "file:c:\file:/", but this one worked just fine.
I guess this version would also work, according to PathMatchingResourcePatternResolver Javadoc:
<bean>
<property name="resources" value="file:${path}/some.*.pattern.zip"/>
...
</bean>
One more example where a single "/" is of crucial importance. I love this job!
Apache Ant Javadoc, Online JFrog: To Build or Not to Be


