eu.lindenbaum.maven.util
Class FileUtils

java.lang.Object
  extended by eu.lindenbaum.maven.util.FileUtils

public final class FileUtils
extends Object

Containing utilities related to file handling.

Author:
Tobias Schlager

Field Summary
static FileFilter APP_FILTER
          Filename filter to filter app files (.app & .appup).
static FileFilter BEAM_FILTER
          Filename filter to filter compiled beam files (.beam).
static Predicate<File> FILE_PRED
          a Predicate that checks a file for null and existance
static FileFilter NULL_FILTER
          a FileFilter accepting all input
static FileFilter REL_FILTER
          Filename filter to filter rel files (.rel).
static FileFilter SOURCE_FILTER
          Filename filter to filter source files (.erl & .hrl).
static FileFilter SRC_FILTER
          Filename filter to filter .src files.
 
Constructor Summary
FileUtils()
           
 
Method Summary
static int copyDirectory(File from, File to, FileFilter filter)
          Copies the contents of the source directory recursively into the destination directory.
static int copyDirectory(File from, File to, FileFilter filter, Map<String,String> replacements)
          Copies the contents of the source directory recursively into the destination directory.
static void copyFile(File from, File to, Map<String,String> replacements)
          Copies the content of a file into another file (previous content will be overwritten).
static void copyFiles(File destination, File... files)
          Copies the given files to a specific destination directory.
static void ensureDirectories(File... dirs)
          Ensures that the directories denoted by the given files exist and are directories.
static void extractFileFromClassPath(Class<?> clazz, String path, String name, File dest)
          Writes a file that is part of the classpath to a specific destination file.
static List<File> getDirectories(File root, Collection<String> excludes)
          Returns a list of sub directories of a specific directory not matching a list of given excludes.
static List<File> getDirectoriesRecursive(File root, String suffix)
          Returns the list of sub directories containing files with the specified file suffix.
static List<File> getFilesAndDirectoriesRecursive(File root, FileFilter filter)
          Returns a list of all found filterend (sub) files and directories.
static List<File> getFilesRecursive(File root, String suffix)
          Get a List of files matching the given file extension (excluding directories).
static FileFilter getSuffixFilter(String[] suffixes)
          Returns a FileFilter which only accepts files ending with one of the given suffixes.
static String readFileFromClassPath(Class<?> clazz, String path, String name)
          Reads a file that is part of the classpath and returns its content as a String object.
static void removeDirectory(File directory)
          Removes a directory recursively.
static void removeEmptyDirectory(File directory)
          Removes an empty directory.
static void removeFileNameSuffix(String suffix, File... files)
          Renames a list of files by replacing the given string suffix from the current file name.
static int removeFiles(File... files)
          Removes the specific files from the file system.
static int removeFilesRecursive(File root, String suffix)
          Removes all files ending with a specific suffix recursively from a directory.
static void touch(File file)
          Touches a file UNIX style.
static void writeFile(File destination, String data)
          Writes data to a destination file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SOURCE_FILTER

public static final FileFilter SOURCE_FILTER
Filename filter to filter source files (.erl & .hrl). Directories are always accepted.


BEAM_FILTER

public static final FileFilter BEAM_FILTER
Filename filter to filter compiled beam files (.beam). Directories are always accepted.


APP_FILTER

public static final FileFilter APP_FILTER
Filename filter to filter app files (.app & .appup). Directories are always accepted.


REL_FILTER

public static final FileFilter REL_FILTER
Filename filter to filter rel files (.rel). Directories are always accepted.


SRC_FILTER

public static final FileFilter SRC_FILTER
Filename filter to filter .src files. Directories are always accepted.


NULL_FILTER

public static FileFilter NULL_FILTER
a FileFilter accepting all input


FILE_PRED

public static Predicate<File> FILE_PRED
a Predicate that checks a file for null and existance

Constructor Detail

FileUtils

public FileUtils()
Method Detail

getSuffixFilter

public static FileFilter getSuffixFilter(String[] suffixes)
Returns a FileFilter which only accepts files ending with one of the given suffixes. The suffixes may be of the for .java or java. Directories will always be accepted.

Parameters:
suffixes - list of accepted suffixes
Returns:
a new FileFilter

getFilesRecursive

public static List<File> getFilesRecursive(File root,
                                           String suffix)
Get a List of files matching the given file extension (excluding directories). By default patterns from FileUtils.getDefaultExcludes() will always be excluded.

Parameters:
root - directory to start recursion from
suffix - file extension to match, can be e.g. eiter ".erl" or "erl"
Returns:
a List of found files

removeFilesRecursive

public static int removeFilesRecursive(File root,
                                       String suffix)
Removes all files ending with a specific suffix recursively from a directory. By default patterns from FileUtils.getDefaultExcludes() will always be excluded.

Parameters:
root - directory to remove specific files from
suffix - file suffixes to remove
Returns:
the number of removed files

removeDirectory

public static void removeDirectory(File directory)
Removes a directory recursively.

Parameters:
directory - to remove

removeEmptyDirectory

public static void removeEmptyDirectory(File directory)
                                 throws org.apache.maven.plugin.MojoExecutionException
Removes an empty directory. Non-empty directories will not be deleted.

Parameters:
directory - to remove
Throws:
org.apache.maven.plugin.MojoExecutionException

removeFiles

public static int removeFiles(File... files)
Removes the specific files from the file system. Returns the number of files removed. Directories are skipped.

Parameters:
files - to remove.
Returns:
The number of files actually removed.

removeFileNameSuffix

public static void removeFileNameSuffix(String suffix,
                                        File... files)
                                 throws org.apache.maven.plugin.MojoExecutionException
Renames a list of files by replacing the given string suffix from the current file name. Directories will be skipped.

Parameters:
suffix - to remove
files - to remove the filename suffix from
Throws:
org.apache.maven.plugin.MojoExecutionException - in case renaming fails

getFilesAndDirectoriesRecursive

public static List<File> getFilesAndDirectoriesRecursive(File root,
                                                         FileFilter filter)
Returns a list of all found filterend (sub) files and directories. In case a sub directory is excluded all of its sub files are also excluded. By default patterns from FileUtils.getDefaultExcludes() will always be excluded.

Parameters:
root - directory to start recursion from
filter - used to filter the found files and directories
Returns:
a List of found files and directories

getDirectoriesRecursive

public static List<File> getDirectoriesRecursive(File root,
                                                 String suffix)
Returns the list of sub directories containing files with the specified file suffix. By default patterns from FileUtils.getDefaultExcludes() will always be excluded.

Parameters:
root - directory to start the scan from
suffix - file suffix to search for
Returns:
a non-null list of directories containg files with a certain suffix

getDirectories

public static List<File> getDirectories(File root,
                                        Collection<String> excludes)
Returns a list of sub directories of a specific directory not matching a list of given excludes. By default patterns from FileUtils.getDefaultExcludes() will also be excluded.

Parameters:
root - to scan
excludes - exact matches to exclude from the returned list
Returns:
a non-null list of directories matching the prefix

copyDirectory

public static int copyDirectory(File from,
                                File to,
                                FileFilter filter)
                         throws org.apache.maven.plugin.MojoExecutionException
Copies the contents of the source directory recursively into the destination directory. The source directory must exist. All missing directories including the destination folder will be created if necessary, already existing files will be overwritten. By default patterns from FileUtils.getDefaultExcludes() will always be excluded. Empty directories will be skipped.

Parameters:
from - the source directory to copy from
to - the destination directory to copy to
filter - additional filter to apply before copying
Returns:
the number of copied files
Throws:
org.apache.maven.plugin.MojoExecutionException
See Also:
getFilesAndDirectoriesRecursive(File, FileFilter)

copyDirectory

public static int copyDirectory(File from,
                                File to,
                                FileFilter filter,
                                Map<String,String> replacements)
                         throws org.apache.maven.plugin.MojoExecutionException
Copies the contents of the source directory recursively into the destination directory. The source directory must exist. All missing directories including the destination folder will be created if necessary, already existing files will be overwritten. The given replacements will be applied to all found files. It is assumed that all files are UTF-8 encoded. By default patterns from FileUtils.getDefaultExcludes() will always be excluded. Empty directories will be skipped.

Parameters:
from - the source directory to copy from
to - the destination directory to copy to
filter - additional filter to apply before copying
replacements - a Map of String patterns to be replaced
Returns:
the number of copied files
Throws:
org.apache.maven.plugin.MojoExecutionException
See Also:
getFilesAndDirectoriesRecursive(File, FileFilter)

copyFile

public static void copyFile(File from,
                            File to,
                            Map<String,String> replacements)
                     throws org.apache.maven.plugin.MojoExecutionException
Copies the content of a file into another file (previous content will be overwritten). The source file must exist. All missing directories including the destination folder will be created if necessary. The given replacements will be applied to the content of the source file. It is assumed that the file is UTF-8 encoded.

Parameters:
from - file to copy the content from
to - file to copy the content into
replacements - a Map of String patterns to be replaced
Throws:
IOException
org.apache.maven.plugin.MojoExecutionException

copyFiles

public static void copyFiles(File destination,
                             File... files)
                      throws org.apache.maven.plugin.MojoExecutionException
Copies the given files to a specific destination directory. The destination directory will be created if necessary.

Parameters:
destination - directory to copy the files into
files - to copy
Throws:
org.apache.maven.plugin.MojoExecutionException - in case a file could not be copied

writeFile

public static void writeFile(File destination,
                             String data)
                      throws org.apache.maven.plugin.MojoExecutionException
Writes data to a destination file. The file will be created if it doesn't exist.

Parameters:
destination - to write to
data - to write
Throws:
org.apache.maven.plugin.MojoExecutionException

ensureDirectories

public static void ensureDirectories(File... dirs)
                              throws org.apache.maven.plugin.MojoExecutionException
Ensures that the directories denoted by the given files exist and are directories. If a directory does not exist the function will try to create it (with parent directories).

Parameters:
dirs - to create/assure.
Throws:
org.apache.maven.plugin.MojoExecutionException - in case one of the directories denotes a file or directory could not be created

touch

public static void touch(File file)
                  throws org.apache.maven.plugin.MojoExecutionException
Touches a file UNIX style. If the file does not exist it will be created, if the file already exists the modification timestamp is set to System.currentTimeMillis().

Parameters:
file - to touch
Throws:
org.apache.maven.plugin.MojoExecutionException

extractFileFromClassPath

public static void extractFileFromClassPath(Class<?> clazz,
                                            String path,
                                            String name,
                                            File dest)
                                     throws org.apache.maven.plugin.MojoExecutionException
Writes a file that is part of the classpath to a specific destination file. Parent directories will be created if necessary.

Parameters:
clazz - used to retrieve the classpath resource from
path - the file's classpath prefix
name - the file to extract denoted by its name in the classpath
dest - the file to write to
Throws:
org.apache.maven.plugin.MojoExecutionException

readFileFromClassPath

public static String readFileFromClassPath(Class<?> clazz,
                                           String path,
                                           String name)
                                    throws org.apache.maven.plugin.MojoExecutionException
Reads a file that is part of the classpath and returns its content as a String object.

Parameters:
clazz - used to retrieve the classpath resource from
path - the file's classpath prefix
name - the file to extract denoted by its name in the classpath
Returns:
a non-null object containing the content of the file
Throws:
org.apache.maven.plugin.MojoExecutionException - in case the file could not be found or read errors occured


Copyright © 2011 Lindenbaum GmbH. All Rights Reserved.