println it

Software blog about tools, builds and making it all work

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!

What a journey. But we have the answer, it’s "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!

, , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>