Top Description Inners Fields Constructors Methods
org.apache.tools.ant.taskdefs

public Class Sync

extends Task
Class Inheritance
Imports
java.io.File, java.util.Collections, .HashSet, .LinkedHashSet, .Map, .Set, java.util.stream.Stream, org.apache.tools.ant.BuildException, .DirectoryScanner, .Project, .Task, org.apache.tools.ant.types.AbstractFileSet, .FileSet, .PatternSet, .Resource, .ResourceCollection, org.apache.tools.ant.types.resources.Resources, .Restrict, org.apache.tools.ant.types.resources.selectors.Exists, org.apache.tools.ant.types.selectors.FileSelector, .NoneSelector, org.apache.tools.ant.util.FileUtils

Synchronize a local target directory from the files defined in one or more filesets.

Uses a <copy> task internally, but forbidding the use of mappers and filter chains. Files of the destination directory not present in any of the source fileset are removed.

Since
Ant 1.6 revised by Dan Armbrust to remove orphaned directories.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
Sync.MyCopy

Subclass Copy in order to access it's file/dir maps.

public static class
Sync.SyncTarget

Inner class used to hold exclude patterns and selectors to save stuff that happens to live in the target directory but should not get removed.

Field Summary

Modifier and TypeField and Description
private Sync.MyCopy
private Resources
private Sync.SyncTarget
Inherited from org.apache.tools.ant.Task:
targettaskNametaskTypewrapper

Constructor Summary

AccessConstructor and Description
public
Sync()

Method Summary

Modifier and TypeMethod and Description
public void
add(ResourceCollection
a resource collection
rc
)

Adds a collection of filesystem resources to copy.

public void
addFileset(FileSet
a fileset
set
)

Adds a set of files to copy.

public void
addPreserveInTarget(Sync.SyncTarget
a preserveintarget nested element
s
)

A container for patterns and selectors that can be used to specify files that should be kept in the target even if they are not present in any source directory.

private static void
assertTrue(String message, boolean condition)

Pseudo-assert method.

private void
public void
execute()

Overrides org.apache.tools.ant.Task.execute.

Execute the sync task.
private Boolean
getExplicitPreserveEmptyDirs()

The value of preserveTarget's preserveEmptyDirs attribute if specified and preserveTarget has been used in the first place.

public void
init()

Overrides org.apache.tools.ant.Task.init.

Initialize the sync task.
private void
logRemovedCount(int count, String prefix, String singularSuffix, String pluralSuffix)

private int

Returns:

the number of empty directories actually removed.
removeEmptyDirectories
(File
the root directory to scan for empty directories.
dir
,
boolean
whether to remove the root directory itself if it becomes empty.
removeIfEmpty
,
Set<File>
directories matched by syncTarget
preservedEmptyDirectories
)

Removes all empty directories from a directory.

private int

Returns:

the number of empty directories actually removed.
removeEmptyDirectories
(Set<File>
directories matched by syncTarget
preservedEmptyDirectories
)

Removes all empty directories preserved by preserveInTarget in the preserveEmptyDirs == FALSE case.

private int[]

Returns:

the number of orphaned files and directories actually removed. Position 0 of the array is the number of orphaned directories. Position 1 of the array is the number or orphaned files.
removeOrphanFiles
(Set<String>
the table of all non-orphan Files.
nonOrphans
,
File
the initial file or directory to scan or test.
toDir
,
Set<File>
will be filled with the directories matched by preserveInTarget - if any. Will not be filled unless preserveEmptyDirs and includeEmptyDirs conflict.
preservedDirectories
)

Removes all files and folders not found as keys of a table (used as a set!).

public void
setFailOnError(boolean
true or false
failonerror
)

If false, note errors to the output but keep going.

public void
setGranularity(long
a long value
granularity
)

The number of milliseconds leeway to give before deciding a target is out of date.

public void
setIncludeEmptyDirs(boolean
If true copy empty directories.
includeEmpty
)

Used to copy empty directories.

public void
setOverwrite(boolean
if true overwrite any existing destination file(s).
overwrite
)

Overwrite any existing destination file(s).

public void
setTodir(File
the destination directory
destDir
)

Sets the destination directory.

public void
setVerbose(boolean
if true force listing of all names of copied files.
verbose
)

Used to force listing of all names of copied files.

Inherited from org.apache.tools.ant.Task:
bindToOwnergetOwningTargetgetRuntimeConfigurableWrappergetTaskNamegetTaskTypegetWrapperhandleErrorFlushhandleErrorOutputhandleFlushhandleInputhandleOutputisInvalidloglogloglogmaybeConfigureperformreconfiguresetOwningTargetsetRuntimeConfigurableWrappersetTaskNamesetTaskType

Field Detail

myCopyback to summary
private Sync.MyCopy myCopy
resourcesback to summary
private Resources resources
syncTargetback to summary
private Sync.SyncTarget syncTarget

Constructor Detail

Syncback to summary
public Sync()

Method Detail

addback to summary
public void add(ResourceCollection rc)

Adds a collection of filesystem resources to copy.

Parameters
rc:ResourceCollection

a resource collection

Since
Ant 1.7
addFilesetback to summary
public void addFileset(FileSet set)

Adds a set of files to copy.

Parameters
set:FileSet

a fileset

addPreserveInTargetback to summary
public void addPreserveInTarget(Sync.SyncTarget s)

A container for patterns and selectors that can be used to specify files that should be kept in the target even if they are not present in any source directory.

You must not invoke this method more than once.

Parameters
s:Sync.SyncTarget

a preserveintarget nested element

Since
Ant 1.7
assertTrueback to summary
private static void assertTrue(String message, boolean condition)

Pseudo-assert method.

configureTaskback to summary
private void configureTask(Task helper)
executeback to summary
public void execute() throws BuildException

Overrides org.apache.tools.ant.Task.execute.

Execute the sync task.

Annotations
@Override
Exceptions
BuildException:
if there is an error.
See Also
Task#execute()
getExplicitPreserveEmptyDirsback to summary
private Boolean getExplicitPreserveEmptyDirs()

The value of preserveTarget's preserveEmptyDirs attribute if specified and preserveTarget has been used in the first place.

Since
Ant 1.8.0.
initback to summary
public void init() throws BuildException

Overrides org.apache.tools.ant.Task.init.

Initialize the sync task.

Annotations
@Override
Exceptions
BuildException:
if there is a problem.
See Also
Task#init()
logRemovedCountback to summary
private void logRemovedCount(int count, String prefix, String singularSuffix, String pluralSuffix)
removeEmptyDirectoriesback to summary
private int removeEmptyDirectories(File dir, boolean removeIfEmpty, Set<File> preservedEmptyDirectories)

Removes all empty directories from a directory.

Note that a directory that contains only empty directories, directly or not, will be removed!

Recurses depth-first to find the leaf directories which are empty and removes them, then unwinds the recursion stack, removing directories which have become empty themselves, etc...

Parameters
dir:File

the root directory to scan for empty directories.

removeIfEmpty:boolean

whether to remove the root directory itself if it becomes empty.

preservedEmptyDirectories:Set<File>

directories matched by syncTarget

Returns:int

the number of empty directories actually removed.

removeEmptyDirectoriesback to summary
private int removeEmptyDirectories(Set<File> preservedEmptyDirectories)

Removes all empty directories preserved by preserveInTarget in the preserveEmptyDirs == FALSE case.

Relies on the set to be ordered in reversed lexicographic order so that directories will be removed depth-first.

Parameters
preservedEmptyDirectories:Set<File>

directories matched by syncTarget

Returns:int

the number of empty directories actually removed.

Since
Ant 1.8.0
removeOrphanFilesback to summary
private int[] removeOrphanFiles(Set<String> nonOrphans, File toDir, Set<File> preservedDirectories)

Removes all files and folders not found as keys of a table (used as a set!).

If the provided file is a directory, it is recursively scanned for orphaned files which will be removed as well.

If the directory is an orphan, it will also be removed.

Parameters
nonOrphans:Set<String>

the table of all non-orphan Files.

toDir:File

the initial file or directory to scan or test.

preservedDirectories:Set<File>

will be filled with the directories matched by preserveInTarget - if any. Will not be filled unless preserveEmptyDirs and includeEmptyDirs conflict.

Returns:int[]

the number of orphaned files and directories actually removed. Position 0 of the array is the number of orphaned directories. Position 1 of the array is the number or orphaned files.

setFailOnErrorback to summary
public void setFailOnError(boolean failonerror)

If false, note errors to the output but keep going.

Parameters
failonerror:boolean

true or false

setGranularityback to summary
public void setGranularity(long granularity)

The number of milliseconds leeway to give before deciding a target is out of date.

Default is 0 milliseconds, or 2 seconds on DOS systems.

Parameters
granularity:long

a long value

Since
Ant 1.6.2
setIncludeEmptyDirsback to summary
public void setIncludeEmptyDirs(boolean includeEmpty)

Used to copy empty directories.

Parameters
includeEmpty:boolean

If true copy empty directories.

setOverwriteback to summary
public void setOverwrite(boolean overwrite)

Overwrite any existing destination file(s).

Parameters
overwrite:boolean

if true overwrite any existing destination file(s).

setTodirback to summary
public void setTodir(File destDir)

Sets the destination directory.

Parameters
destDir:File

the destination directory

setVerboseback to summary
public void setVerbose(boolean verbose)

Used to force listing of all names of copied files.

Parameters
verbose:boolean

if true force listing of all names of copied files.

org.apache.tools.ant.taskdefs back to summary

public Class Sync.MyCopy

extends Copy
Class Inheritance

Subclass Copy in order to access it's file/dir maps.

Field Summary

Modifier and TypeField and Description
private Set<String>
Inherited from org.apache.tools.ant.taskdefs.Copy:
completeDirMapdestDirdestFiledirCopyMapfailonerrorfilefileCopyMapfilesetsfileUtilsfilteringflattenforceOverwriteincludeEmptyLINE_SEPARATORmapperElementNULL_FILE_PLACEHOLDERpreserveLastModifiedrcsverbosity

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true if emptyDirs are to be included
getIncludeEmptyDirs
()

Get the includeEmptyDirs attribute.

public File

Returns:

the destination directory
getToDir
()

Get the destination directory.

protected void
scan(File
The source directory.
fromDir
,
File
The destination directory.
toDir
,
String[]
A list of files to copy.
files
,
String[]
A list of directories to copy.
dirs
)

Overrides org.apache.tools.ant.taskdefs.Copy.scan.

Compares source files to destination files to see if they should be copied.
protected Map<Resource, String[]>
scan(Resource[]
The source resources.
resources
,
File
The destination directory.
toDir
)

Overrides org.apache.tools.ant.taskdefs.Copy.scan.

Compares source resources to destination files to see if they should be copied.
protected boolean
Inherited from org.apache.tools.ant.taskdefs.Copy:
addaddaddFilesetbuildMapbuildMapcreateFilterChaincreateFilterSetcreateMapperdoFileOperationsdoResourceOperationsexecutegetEncodinggetFileUtilsgetFilterChainsgetFilterSetsgetForcegetOutputEncodinggetPreserveLastModifiedisEnableMultipleMappingsetEnableMultipleMappingssetEncodingsetFailOnErrorsetFilesetFilteringsetFlattensetForcesetGranularitysetIncludeEmptyDirssetOutputEncodingsetOverwritesetPreserveLastModifiedsetPreserveLastModifiedsetQuietsetTodirsetTofilesetVerbosevalidateAttributes

Field Detail

nonOrphansback to summary
private Set<String> nonOrphans

Constructor Detail

MyCopyback to summary
public MyCopy()

Method Detail

getIncludeEmptyDirsback to summary
public boolean getIncludeEmptyDirs()

Get the includeEmptyDirs attribute.

Returns:boolean

true if emptyDirs are to be included

getToDirback to summary
public File getToDir()

Get the destination directory.

Returns:File

the destination directory

scanback to summary
protected void scan(File fromDir, File toDir, String[] files, String[] dirs)

Overrides org.apache.tools.ant.taskdefs.Copy.scan.

Doc from org.apache.tools.ant.taskdefs.Copy.scan.

Compares source files to destination files to see if they should be copied.

Parameters
fromDir:File

The source directory.

toDir:File

The destination directory.

files:String[]

A list of files to copy.

dirs:String[]

A list of directories to copy.

Annotations
@Override
See Also
Copy#scan(File, File, String[], String[])
scanback to summary
protected Map<Resource, String[]> scan(Resource[] resources, File toDir)

Overrides org.apache.tools.ant.taskdefs.Copy.scan.

Doc from org.apache.tools.ant.taskdefs.Copy.scan.

Compares source resources to destination files to see if they should be copied.

Parameters
resources:Resource[]

The source resources.

toDir:File

The destination directory.

Returns:Map<Resource, String[]>

a Map with the out-of-date resources as keys and an array of target file names as values.

Annotations
@Override
See Also
Copy#scan(Resource[], File)
supportsNonFileResourcesback to summary
protected boolean supportsNonFileResources()

Overrides org.apache.tools.ant.taskdefs.Copy.supportsNonFileResources.

Yes, we can.

Returns:boolean

true always.

Annotations
@Override
Since
Ant 1.7
org.apache.tools.ant.taskdefs back to summary

public Class Sync.SyncTarget

extends AbstractFileSet
Class Inheritance

Inner class used to hold exclude patterns and selectors to save stuff that happens to live in the target directory but should not get removed.
Since
Ant 1.7

Field Summary

Modifier and TypeField and Description
private Boolean

Constructor Summary

AccessConstructor and Description
public
SyncTarget()

Constructor for SyncTarget.

Method Summary

Modifier and TypeMethod and Description
public Boolean

Returns:

Boolean
getPreserveEmptyDirs
()

Whether empty directories matched by this fileset should be preserved.

public void
setDir(File
ignored
dir
)

Overrides org.apache.tools.ant.types.AbstractFileSet.setDir.

Override AbstractFileSet#setDir(File) to disallow setting the directory.
public void
setPreserveEmptyDirs(boolean
boolean
b
)

Whether empty directories matched by this fileset should be preserved.

private FileSet
toFileSet(boolean withPatterns)

Inherited from org.apache.tools.ant.types.AbstractFileSet:
addaddAndaddContainsaddContainsRegexpaddCustomaddDateaddDependaddDepthaddDifferentaddExecutableaddFilenameaddMajorityaddModifiedaddNoneaddNotaddOraddOwnedByaddPosixGroupaddPosixPermissionsaddPresentaddReadableaddSelectoraddSizeaddSymlinkaddTypeaddWritableappendExcludesappendIncludesappendSelectorclonecreateExcludecreateExcludesFilecreateIncludecreateIncludesFilecreatePatternSetdieOnCircularReferencegetDefaultexcludesgetDirgetDirgetDirectoryScannergetDirectoryScannergetErrorOnMissingDirgetMaxLevelsOfSymlinksgetRefgetSelectorshasPatternshasSelectorsisCaseSensitiveisFollowSymlinksmergeExcludesmergeIncludesmergePatternsselectorCountselectorElementssetCaseSensitivesetDefaultexcludessetErrorOnMissingDirsetExcludessetExcludesfilesetFilesetFollowSymlinkssetIncludessetIncludesfilesetMaxLevelsOfSymlinkssetRefidsetupDirectoryScannersetupDirectoryScannertoString

Field Detail

preserveEmptyDirsback to summary
private Boolean preserveEmptyDirs

Constructor Detail

SyncTargetback to summary
public SyncTarget()

Constructor for SyncTarget. This just changes the default value of "defaultexcludes" from true to false.

Method Detail

getPreserveEmptyDirsback to summary
public Boolean getPreserveEmptyDirs()

Whether empty directories matched by this fileset should be preserved.

Returns:Boolean

Boolean

Since
Ant 1.8.0
setDirback to summary
public void setDir(File dir) throws BuildException

Overrides org.apache.tools.ant.types.AbstractFileSet.setDir.

Override AbstractFileSet#setDir(File) to disallow setting the directory.

Parameters
dir:File

ignored

Annotations
@Override
Exceptions
BuildException:
always
setPreserveEmptyDirsback to summary
public void setPreserveEmptyDirs(boolean b)

Whether empty directories matched by this fileset should be preserved.

Parameters
b:boolean

boolean

Since
Ant 1.8.0
toFileSetback to summary
private FileSet toFileSet(boolean withPatterns)