Package bm.target
Class FileTarget
java.lang.Object
bm.target.FileTarget
- All Implemented Interfaces:
StateProvider,Target
A target representing the state of a file or all files found in a directory, based on a checksum
taken over the file/all files.
Example: to compile a bunch of files in a directory, say src/main/java, the
compile target needs a dependency which is, of course, a target too. It is the
target which keeps track of whether the source files where changed or not:
Target javaSource = FileState.ofContent("src/main/java").toFileTarget();
This target has no builder since the source files need not be created, we only keep track of
whether they were changed by the developer. Now to compile the files into
build/classes we define
Target javaClasses = FileState.ofContent("build/classes")
.toFileTarget()
.dependsOn(javaSource)
.buildWith(target -> compileJava(target, javaSources);
Here compileJava is a method you have to write which executes javac to
run the compilation.
See also: FileState.toFileTarget(List, ConsumeOrThrow).
-
Constructor Summary
ConstructorsConstructorDescriptionFileTarget(FileState state, List<? extends Target> dependencies, ConsumeOrThrow<FileTarget> builder) Creates an instance. -
Method Summary
Modifier and TypeMethodDescriptionvoidGiven a sink forbyte[]this method should provide bytes representing a target's value.buildWith(ConsumeOrThrow<FileTarget> newBuilder) static FileTargetCreates a target with a builder to create a directory if it does not exist.Return the list of targets this one depends on when being build.getFiles()Provides all files in the target's directory or just the file.Provides all files in the target's directory or just the file.getName()A file target's name is the name of the target path.getPath()Provides the target's pathvoidrecreate()Recreate this target.toString()Creates a new instance having the new name.
-
Constructor Details
-
FileTarget
public FileTarget(FileState state, List<? extends Target> dependencies, ConsumeOrThrow<FileTarget> builder) Creates an instance. The name is taken from the target path of the state provided.Rather than this constructor consider using
FileContentState.fromContent(Path)orFileContentState.fromLastModified(String)and the convert to the target withFileState.toFileTarget().- Parameters:
state- representing this object's pathdependencies- it depends onbuilder- to run to re-create the target
-
-
Method Details
-
directoryCreateTarget
Creates a target with a builder to create a directory if it does not exist.- Parameters:
dir- to create.- Returns:
- the target
-
dependsOn
-
dependsOn
-
buildWith
-
withName
Creates a new instance having the new name.- Parameters:
newName- the new name- Returns:
- an instance with the new name and otherwise with the same parameters as
this.
-
getPath
Provides the target's path- Returns:
- file or directory path as provided in the constructor
-
getFiles
Provides all files in the target's directory or just the file.- Returns:
- file paths
-
getFilesRelative
Provides all files in the target's directory or just the file.- Returns:
- file paths relative to what
getPath()returns
-
getName
A file target's name is the name of the target path. -
getDependencies
Description copied from interface:TargetReturn the list of targets this one depends on when being build. This should list all targets whose change should trigger a rebuild of this target.- Specified by:
getDependenciesin interfaceTarget- Returns:
- the dependencies of this target
-
addState
Description copied from interface:StateProviderGiven a sink forbyte[]this method should provide bytes representing a target's value. If the value changes, the bytes provided must change too. But the bytes may also change if the value was not actually changed, though this will result in unnecessary builts of a target.This should not mingle in any dependency data.
As examples consider the full byte content of a file or only its last modified time. The latter changes even if a file is overwritten by itself, but for some cases, i.e. huge files, the time stamp may be preferable.
- Specified by:
addStatein interfaceStateProvider- Parameters:
sink- into which to fill the state representing bytes- Throws:
BuildProblemException- as needed
-
recreate
Description copied from interface:TargetRecreate this target.- Specified by:
recreatein interfaceTarget- Throws:
BuildProblemException- as needed
-
toString
-