Created
July 3, 2011 22:29
-
-
Save vorburger/1062678 to your computer and use it in GitHub Desktop.
Copy resources from Classpath into a temporary directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.IOException; | |
import java.net.URL; | |
import org.apache.commons.io.FileUtils; | |
import org.springframework.core.io.Resource; | |
import org.springframework.core.io.support.PathMatchingResourcePatternResolver; | |
import org.springframework.core.io.support.ResourcePatternResolver; | |
File directory = new File(System.getProperty("java.io.tmpdir"), "MifosBirtFilesExtractedFromClasspath"); | |
directory.mkdirs(); | |
FileUtils.cleanDirectory(directory); | |
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); | |
Resource[] resources = resolver.getResources(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + BIRT_RESOURCES_PATTERN + "**"); | |
for (Resource resource : resources) { | |
if (resource.exists() & resource.isReadable() && resource.contentLength() > 0) { | |
URL url = resource.getURL(); | |
String urlString = url.toExternalForm(); | |
String targetName = urlString.substring(urlString.indexOf(directoryToScan)); | |
File destination = new File(rootDirectory, targetName); | |
FileUtils.copyURLToFile(url, destination); | |
LOGGER.info("Copied " + url + " to " + destination.getAbsolutePath()); | |
} else { | |
LOGGER.debug("Did not copy, seems to be directory: " + resource.getDescription()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment