Package bm.target

Class FileTarget

java.lang.Object
bm.target.FileTarget
All Implemented Interfaces:
StateProvider, Target

public final class FileTarget extends Object implements 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 Details

  • Method Details

    • directoryCreateTarget

      public static FileTarget directoryCreateTarget(Path dir)
      Creates a target with a builder to create a directory if it does not exist.
      Parameters:
      dir - to create.
      Returns:
      the target
    • dependsOn

      public FileTarget dependsOn(Target... newDependencies)
    • dependsOn

      public FileTarget dependsOn(List<? extends Target> newDependencies)
    • buildWith

      public FileTarget buildWith(ConsumeOrThrow<FileTarget> newBuilder)
    • withName

      public FileTarget withName(String newName)
      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

      public Path getPath()
      Provides the target's path
      Returns:
      file or directory path as provided in the constructor
    • getFiles

      public List<Path> getFiles()
      Provides all files in the target's directory or just the file.
      Returns:
      file paths
    • getFilesRelative

      public List<Path> getFilesRelative()
      Provides all files in the target's directory or just the file.
      Returns:
      file paths relative to what getPath() returns
    • getName

      public String getName()
      A file target's name is the name of the target path.
      Specified by:
      getName in interface Target
      Returns:
      the name
    • getDependencies

      public List<Target> getDependencies()
      Description copied from interface: Target
      Return 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:
      getDependencies in interface Target
      Returns:
      the dependencies of this target
    • addState

      public void addState(Consumer<byte[]> sink) throws BuildProblemException
      Description copied from interface: StateProvider
      Given a sink for byte[] 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:
      addState in interface StateProvider
      Parameters:
      sink - into which to fill the state representing bytes
      Throws:
      BuildProblemException - as needed
    • recreate

      public void recreate() throws BuildProblemException
      Description copied from interface: Target
      Recreate this target.
      Specified by:
      recreate in interface Target
      Throws:
      BuildProblemException - as needed
    • toString

      public String toString()
      Overrides:
      toString in class Object