Class DefaultBuildContext

  • All Implemented Interfaces:
    BuildContext

    @Named("default")
    @Singleton
    public class DefaultBuildContext
    extends java.lang.Object
    implements BuildContext
    Filesystem based non-incremental build context implementation which behaves as if all files were just created. More specifically,
    1. hasDelta returns true for all paths
    2. newScanner returns Scanner that scans all files under provided basedir
    3. newDeletedScanner always returns empty scanner
    4. isIncremental returns false
    5. getValue always returns the last set value in this session and only stores to memory
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.Map<java.lang.String,​java.lang.Object> contextMap  
      private static org.codehaus.plexus.logging.AbstractLogEnabled DUMMY  
      private org.sonatype.plexus.build.incremental.BuildContext legacy  
      private org.slf4j.Logger logger  
    • Constructor Summary

      Constructors 
      Constructor Description
      DefaultBuildContext​(org.sonatype.plexus.build.incremental.BuildContext legacy)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addError​(java.io.File file, int line, int column, java.lang.String message, java.lang.Throwable cause)
      addError.
      void addMessage​(java.io.File file, int line, int column, java.lang.String message, int severity, java.lang.Throwable cause)
      Adds a message to the build context.
      void addWarning​(java.io.File file, int line, int column, java.lang.String message, java.lang.Throwable cause)
      addWarning.
      private java.lang.String getMessage​(java.io.File file, int line, int column, java.lang.String message)  
      java.lang.Object getValue​(java.lang.String key)
      Returns value associated with key during previous mojo execution.
      boolean hasDelta​(java.io.File file)
      hasDelta.
      boolean hasDelta​(java.lang.String relpath)
      Returns true if file or folder identified by relpath has changed since last build.
      boolean hasDelta​(java.util.List<java.lang.String> relpaths)
      hasDelta.
      private boolean isDefaultImplementation()  
      boolean isIncremental()
      isIncremental.
      boolean isUptodate​(java.io.File target, java.io.File source)
      Returns true, if the target file exists and is uptodate compared to the source file.
      org.codehaus.plexus.util.Scanner newDeleteScanner​(java.io.File basedir)
      Returned Scanner scans basedir for files and directories deleted since last build.
      java.io.OutputStream newFileOutputStream​(java.io.File file)
      Returns new OutputStream that writes to the file.
      org.codehaus.plexus.util.Scanner newScanner​(java.io.File basedir)
      Convenience method, fully equal to newScanner(basedir, false)
      org.codehaus.plexus.util.Scanner newScanner​(java.io.File basedir, boolean ignoreDelta)
      Returned Scanner scans files and folders under basedir.
      void refresh​(java.io.File file)
      Indicates that the file or folder content has been modified during the build.
      void removeMessages​(java.io.File file)
      Removes all messages associated with a file or folder during a previous build.
      void setValue​(java.lang.String key, java.lang.Object value)
      Associate specified key with specified value in the build context.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • logger

        private final org.slf4j.Logger logger
      • DUMMY

        private static final org.codehaus.plexus.logging.AbstractLogEnabled DUMMY
      • contextMap

        private final java.util.Map<java.lang.String,​java.lang.Object> contextMap
      • legacy

        private org.sonatype.plexus.build.incremental.BuildContext legacy
    • Constructor Detail

      • DefaultBuildContext

        @Inject
        public DefaultBuildContext​(org.sonatype.plexus.build.incremental.BuildContext legacy)
        Parameters:
        legacy - the legacy API we delegate to by default, this allow us to support "older" plugins and implementors of the API while still having a way to move forward!
    • Method Detail

      • hasDelta

        public boolean hasDelta​(java.lang.String relpath)
        Returns true if file or folder identified by relpath has changed since last build.
        Specified by:
        hasDelta in interface BuildContext
        Parameters:
        relpath - is path relative to build context basedir
        Returns:
        a boolean.
      • hasDelta

        public boolean hasDelta​(java.io.File file)

        hasDelta.

        Specified by:
        hasDelta in interface BuildContext
        Parameters:
        file - a File object.
        Returns:
        a boolean.
      • hasDelta

        public boolean hasDelta​(java.util.List<java.lang.String> relpaths)

        hasDelta.

        Specified by:
        hasDelta in interface BuildContext
        Parameters:
        relpaths - a List object.
        Returns:
        a boolean.
      • newFileOutputStream

        public java.io.OutputStream newFileOutputStream​(java.io.File file)
                                                 throws java.io.IOException
        Returns new OutputStream that writes to the file. Files changed using OutputStream returned by this method do not need to be explicitly refreshed using BuildContext.refresh(File). As an optional optimisation, OutputStreams created by incremental build context will attempt to avoid writing to the file if file content has not changed.
        Specified by:
        newFileOutputStream in interface BuildContext
        Parameters:
        file - a File object.
        Returns:
        a OutputStream object.
        Throws:
        java.io.IOException - if any.
      • isDefaultImplementation

        private boolean isDefaultImplementation()
        Returns:
        true if the legacy is the default implementation and we can safely override/change behavior here, or false if a custom implementation is used and full delegation is required.
      • newScanner

        public org.codehaus.plexus.util.Scanner newScanner​(java.io.File basedir)
        Convenience method, fully equal to newScanner(basedir, false)
        Specified by:
        newScanner in interface BuildContext
        Parameters:
        basedir - a File object.
        Returns:
        a Scanner object.
      • newDeleteScanner

        public org.codehaus.plexus.util.Scanner newDeleteScanner​(java.io.File basedir)
        Returned Scanner scans basedir for files and directories deleted since last build. Returns empty Scanner if basedir is not under this build context basedir.
        Specified by:
        newDeleteScanner in interface BuildContext
        Parameters:
        basedir - a File object.
        Returns:
        a Scanner object.
      • newScanner

        public org.codehaus.plexus.util.Scanner newScanner​(java.io.File basedir,
                                                           boolean ignoreDelta)
        Returned Scanner scans files and folders under basedir. If this is an incremental build context and ignoreDelta is false, the scanner will only "see" files and folders with content changes since last build. If ignoreDelta is true, the scanner will "see" all files and folders. Please beware that ignoreDelta=false does NOT work reliably for operations that copy resources from source to target locations. Returned Scanner only scans changed source resources and it does not consider changed or deleted target resources. This results in missing or stale target resources. Starting with 0.5.0, recommended way to process resources is to use #newScanner(basedir,true) to locate all source resources and BuildContext.isUptodate(File, File) to optimized processing of uptodate target resources. Returns empty Scanner if basedir is not under this build context basedir. https://issues.apache.org/jira/browse/MSHARED-125
        Specified by:
        newScanner in interface BuildContext
        Parameters:
        basedir - a File object.
        ignoreDelta - a boolean.
        Returns:
        a Scanner object.
      • isIncremental

        public boolean isIncremental()

        isIncremental.

        Specified by:
        isIncremental in interface BuildContext
        Returns:
        a boolean.
      • setValue

        public void setValue​(java.lang.String key,
                             java.lang.Object value)
        Associate specified key with specified value in the build context. Primary (and the only) purpose of this method is to allow preservation of state needed for proper incremental behaviour between consecutive executions of the same mojo needed to. For example, maven-plugin-plugin:descriptor mojo can store collection of extracted MojoDescritpor during first invocation. Then on each consecutive execution maven-plugin-plugin:descriptor will only need to extract MojoDescriptors for changed files.
        Specified by:
        setValue in interface BuildContext
        Parameters:
        key - a String object.
        value - a Object object.
        See Also:
        BuildContext.getValue(String)
      • addError

        public void addError​(java.io.File file,
                             int line,
                             int column,
                             java.lang.String message,
                             java.lang.Throwable cause)

        addError.

        Specified by:
        addError in interface BuildContext
        Parameters:
        file - a File object.
        line - a int.
        column - a int.
        message - a String object.
        cause - a Throwable object.
      • addWarning

        public void addWarning​(java.io.File file,
                               int line,
                               int column,
                               java.lang.String message,
                               java.lang.Throwable cause)

        addWarning.

        Specified by:
        addWarning in interface BuildContext
        Parameters:
        file - a File object.
        line - a int.
        column - a int.
        message - a String object.
        cause - a Throwable object.
      • getMessage

        private java.lang.String getMessage​(java.io.File file,
                                            int line,
                                            int column,
                                            java.lang.String message)
      • addMessage

        public void addMessage​(java.io.File file,
                               int line,
                               int column,
                               java.lang.String message,
                               int severity,
                               java.lang.Throwable cause)
        Adds a message to the build context. The message is associated with a file and a location inside that file.
        Specified by:
        addMessage in interface BuildContext
        Parameters:
        file - The file or folder with which the message is associated. Should not be null and it is recommended to be an absolute path.
        line - The line number inside the file. Use 1 (not 0) for the first line. Use 0 for unknown/unspecified.
        column - The column number inside the file. Use 1 (not 0) for the first column. Use 0 for unknown/unspecified.
        message - a String object.
        severity - The severity of the message: SEVERITY_WARNING or SEVERITY_ERROR.
        cause - A Throwable object associated with the message. Can be null.
      • removeMessages

        public void removeMessages​(java.io.File file)
        Removes all messages associated with a file or folder during a previous build. It does not affect the messages added during the current build.
        Specified by:
        removeMessages in interface BuildContext
        Parameters:
        file - a File object.
      • isUptodate

        public boolean isUptodate​(java.io.File target,
                                  java.io.File source)
        Returns true, if the target file exists and is uptodate compared to the source file. More specifically, this method returns true when both target and source files exist, do not have changes since last incremental build and the target file was last modified later than the source file. Returns false in all other cases.
        Specified by:
        isUptodate in interface BuildContext
        Parameters:
        target - a File object.
        source - a File object.
        Returns:
        a boolean.