Class AbstractJaxbMojo

  • All Implemented Interfaces:
    org.apache.maven.plugin.ContextEnabled, org.apache.maven.plugin.Mojo
    Direct Known Subclasses:
    AbstractJavaGeneratorMojo, AbstractXsdGeneratorMojo

    public abstract class AbstractJaxbMojo
    extends org.apache.maven.plugin.AbstractMojo
    Abstract Mojo which collects common infrastructure, required and needed by all subclass Mojos in the JAXB2 maven plugin codebase.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private org.sonatype.plexus.build.incremental.BuildContext buildContext
      The Plexus BuildContext is used to identify files or directories modified since last build, implying functionality used to define if java generation must be performed again.
      static java.util.regex.Pattern CONTAINS_WHITESPACE
      Pattern matching strings containing whitespace (or consisting only of whitespace).
      private java.lang.String encoding
      Defines the encoding used by XJC (for generating Java Source files) and schemagen (for generating XSDs).
      private org.apache.maven.plugin.MojoExecution execution
      Note that the execution parameter will be injected ONLY if this plugin is executed as part of a maven standard lifecycle - as opposed to directly invoked with a direct invocation.
      protected java.util.List<EnvironmentFacet> extraFacets
      Defines a set of extra EnvironmentFacet instances which are used to further configure the ToolExecutionEnvironment used by this plugin to fire XJC or SchemaGen.
      protected java.lang.String locale
      A Locale definition to create and set the system (default) Locale when the XJB or SchemaGen tools executes.
      static java.lang.String NEWLINE
      Platform-independent newline control string.
      private static java.lang.String OWN_ARTIFACT_ID  
      static java.lang.String PACKAGE_INFO_FILENAME
      Standard name of the package-info.java file which may contain JAXB annotations and Package JavaDoc.
      private org.apache.maven.project.MavenProject project
      The injected Maven project.
      private static java.util.List<java.lang.String> RELEVANT_GROUPIDS  
      protected java.io.File staleFileDirectory
      The directory where the staleFile is found.
      static java.lang.String STANDARD_EPISODE_FILENAME
      Standard name of the generated JAXB episode file.
      static java.util.List<Filter<java.io.File>> STANDARD_EXCLUDE_FILTERS
      Standard excludes Filters for all Java generator Mojos.
      private static java.lang.String[] STANDARD_EXCLUDE_SUFFIXES  
      private static java.lang.String[] STANDARD_PRELOADED_CLASSES  
      private static java.lang.String SYSTEM_FILE_ENCODING_PROPERTY  
      • Fields inherited from interface org.apache.maven.plugin.Mojo

        ROLE
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      protected abstract void addResource​(org.apache.maven.model.Resource resource)
      Adds the supplied Resource to the project using the appropriate scope (i.e.
      void execute()
      protected org.sonatype.plexus.build.incremental.BuildContext getBuildContext()
      The Plexus BuildContext is used to identify files or directories modified since last build, implying functionality used to define if java generation must be performed again.
      protected abstract java.util.List<java.lang.String> getClasspath()
      Retrieves the configured List of paths from which this AbstractJaxbMojo and its internal toolset (XJC or SchemaGen) should read bytecode classes.
      protected java.lang.String getEncoding​(boolean warnIfPlatformEncoding)
      The algorithm for finding the encoding to use is as follows (where the first non-null value found is used for encoding):
      protected java.io.File getEpisodeFile​(java.lang.String episodeFileName)
      Retrieves a File to the JAXB Episode (which is normally written during the XJC process).
      org.apache.maven.plugin.MojoExecution getExecution()  
      private <T> T getInjectedObject​(T objectOrNull, java.lang.String objectName)  
      protected abstract java.io.File getOutputDirectory()
      Retrieves the directory where the generated files should be written to.
      protected org.apache.maven.project.MavenProject getProject()  
      protected abstract java.util.List<java.net.URL> getSources()
      Override this method to acquire a List holding all URLs to the sources which this AbstractJaxbMojo should use to produce its output (XSDs files for AbstractXsdGeneratorMojos and Java Source Code for AbstractJavaGeneratorMojos).
      protected java.io.File getStaleFile()
      Acquires the staleFile for this execution
      protected abstract java.lang.String getStaleFileName()
      Retrieves the last name part of the stale file.
      protected abstract boolean isReGenerationRequired()  
      protected java.lang.String[] logAndReturnToolArguments​(java.lang.String[] arguments, java.lang.String toolName)  
      private void logPluginAndJaxbDependencyInfo()  
      protected void logSystemPropertiesAndBasedir()
      Prints out the system properties to the Maven Log at Debug level.
      protected abstract boolean performExecution()
      Implement this method to perform this Mojo's execution.
      protected abstract boolean shouldExecutionBeSkipped()
      Implement this method to check if this AbstractJaxbMojo should skip executing altogether.
      private void updateStaleFileTimestamp()  
      protected void warnAboutIncorrectPluginConfiguration​(java.lang.String propertyName, java.lang.String description)
      Convenience method to invoke when some plugin configuration is incorrect.
      • Methods inherited from class org.apache.maven.plugin.AbstractMojo

        getLog, getPluginContext, setLog, setPluginContext
      • Methods inherited from class java.lang.Object

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

      • STANDARD_EPISODE_FILENAME

        public static final java.lang.String STANDARD_EPISODE_FILENAME
        Standard name of the generated JAXB episode file.
        See Also:
        Constant Field Values
      • PACKAGE_INFO_FILENAME

        public static final java.lang.String PACKAGE_INFO_FILENAME
        Standard name of the package-info.java file which may contain JAXB annotations and Package JavaDoc.
        See Also:
        Constant Field Values
      • NEWLINE

        public static final java.lang.String NEWLINE
        Platform-independent newline control string.
      • CONTAINS_WHITESPACE

        public static final java.util.regex.Pattern CONTAINS_WHITESPACE
        Pattern matching strings containing whitespace (or consisting only of whitespace).
      • STANDARD_EXCLUDE_FILTERS

        public static final java.util.List<Filter<java.io.File>> STANDARD_EXCLUDE_FILTERS

        Standard excludes Filters for all Java generator Mojos. The List is unmodifiable, and contains Filters on the following form:

             
                 // The standard exclude filters contain simple, exclude pattern filters.
                 final List<Filter<File>> tmp = new ArrayList<Filter<File>>();
                 tmp.add(new PatternFileFilter(Arrays.asList({"README.*", "\\.xml", "\\.txt"}), true));
                 tmp.add(new FileFilterAdapter(new FileFilter() {
        
                     @Override
                     public boolean accept(final File aFileOrDir) {
        
                         // Check sanity
                         if (aFileOrDir == null) {
                             return false;
                         }
        
                         final String name = aFileOrDir.getName();
        
                         // Ignore hidden files and CVS directories
                         return name.startsWith(".")
                                 || (aFileOrDir.isDirectory() && name.equals("CVS"));
        
                     }
                 }));
             
         

        Note! Since the plugin is currently developed in jdk 1.7-compliant code, we cannot use lambdas within Filters just yet.

      • RELEVANT_GROUPIDS

        private static final java.util.List<java.lang.String> RELEVANT_GROUPIDS
      • SYSTEM_FILE_ENCODING_PROPERTY

        private static final java.lang.String SYSTEM_FILE_ENCODING_PROPERTY
        See Also:
        Constant Field Values
      • STANDARD_EXCLUDE_SUFFIXES

        private static final java.lang.String[] STANDARD_EXCLUDE_SUFFIXES
      • STANDARD_PRELOADED_CLASSES

        private static final java.lang.String[] STANDARD_PRELOADED_CLASSES
      • buildContext

        @Component
        private org.sonatype.plexus.build.incremental.BuildContext buildContext
        The Plexus BuildContext is used to identify files or directories modified since last build, implying functionality used to define if java generation must be performed again.
      • project

        @Parameter(defaultValue="${project}",
                   readonly=true)
        private org.apache.maven.project.MavenProject project
        The injected Maven project.
      • execution

        @Parameter(defaultValue="${mojoExecution}",
                   readonly=true)
        private org.apache.maven.plugin.MojoExecution execution
        Note that the execution parameter will be injected ONLY if this plugin is executed as part of a maven standard lifecycle - as opposed to directly invoked with a direct invocation. When firing this mojo directly (i.e. mvn xjc:something or mvn schemagen:something), the execution object will not be injected.
      • staleFileDirectory

        @Parameter(defaultValue="${project.build.directory}/jaxb2",
                   readonly=true,
                   required=true)
        protected java.io.File staleFileDirectory

        The directory where the staleFile is found. The staleFile assists in determining if re-generation of JAXB build products is required.

        While it is permitted to re-define the staleFileDirectory, it is recommended to keep it below the ${project.build.directory}, to ensure that JAXB code or XSD re-generation occurs after cleaning the project.

        Since:
        2.0
      • encoding

        @Parameter(defaultValue="${project.build.sourceEncoding}")
        private java.lang.String encoding

        Defines the encoding used by XJC (for generating Java Source files) and schemagen (for generating XSDs). The corresponding argument parameter for XJC and SchemaGen is: encoding.

        The algorithm for finding the encoding to use is as follows (where the first non-null value found is used for encoding):

        1. If the configuration property is explicitly given within the plugin's configuration, use that value.
        2. If the Maven property project.build.sourceEncoding is defined, use its value.
        3. Otherwise use the value from the system property file.encoding.

        Since:
        2.0
        See Also:
        getEncoding(boolean)
      • locale

        @Parameter(required=false)
        protected java.lang.String locale

        A Locale definition to create and set the system (default) Locale when the XJB or SchemaGen tools executes. The Locale will be reset to its default value after the execution of XJC or SchemaGen is complete.

        The configuration parameter must be supplied on the form language[,country[,variant]], such as sv,SE or fr. Refer to org.codehaus.mojo.jaxb2.shared.environment.locale.LocaleFacet.createFor(String, Log) for further information.

        Example (assigns french locale):

             
                 <configuration>
                      <locale>fr</locale>
                 </configuration>
             
         
        Since:
        2.2
        See Also:
        LocaleFacet.createFor(String, Log), Locale.getAvailableLocales()
      • extraFacets

        @Parameter(required=false)
        protected java.util.List<EnvironmentFacet> extraFacets

        Defines a set of extra EnvironmentFacet instances which are used to further configure the ToolExecutionEnvironment used by this plugin to fire XJC or SchemaGen.

        Example: If you implement the EnvironmentFacet interface in the class org.acme.MyCoolEnvironmentFacetImplementation, its setup() method is called before the XJC or SchemaGen tools are executed to setup some facet of their Execution environment. Correspondingly, the restore() method in your org.acme.MyCoolEnvironmentFacetImplementation class is invoked after the XJC or SchemaGen execution terminates.

             
                 <configuration>
                 ...
                      <extraFacets>
                          <extraFacet implementation="org.acme.MyCoolEnvironmentFacetImplementation" />
                      </extraFacets>
                 ...
                 </configuration>
             
         
        Since:
        2.2
        See Also:
        EnvironmentFacet, ToolExecutionEnvironment.add(EnvironmentFacet)
    • Constructor Detail

      • AbstractJaxbMojo

        public AbstractJaxbMojo()
    • Method Detail

      • addResource

        protected abstract void addResource​(org.apache.maven.model.Resource resource)
        Adds the supplied Resource to the project using the appropriate scope (i.e. resource or testResource) depending on the exact implementation of this AbstractJaxbMojo.
        Parameters:
        resource - The resource to add.
      • getBuildContext

        protected final org.sonatype.plexus.build.incremental.BuildContext getBuildContext()
        The Plexus BuildContext is used to identify files or directories modified since last build, implying functionality used to define if java generation must be performed again.
        Returns:
        the active Plexus BuildContext.
      • getProject

        protected final org.apache.maven.project.MavenProject getProject()
        Returns:
        The active MavenProject.
      • getExecution

        public org.apache.maven.plugin.MojoExecution getExecution()
        Returns:
        The active MojoExecution.
      • execute

        public final void execute()
                           throws org.apache.maven.plugin.MojoExecutionException,
                                  org.apache.maven.plugin.MojoFailureException
        Throws:
        org.apache.maven.plugin.MojoExecutionException
        org.apache.maven.plugin.MojoFailureException
      • shouldExecutionBeSkipped

        protected abstract boolean shouldExecutionBeSkipped()
        Implement this method to check if this AbstractJaxbMojo should skip executing altogether.
        Returns:
        true to indicate that this AbstractJaxbMojo should bail out of its execute method.
      • isReGenerationRequired

        protected abstract boolean isReGenerationRequired()
        Returns:
        true to indicate that this AbstractJaxbMojo should be run since its generated files were either stale or not present, and false otherwise.
      • performExecution

        protected abstract boolean performExecution()
                                             throws org.apache.maven.plugin.MojoExecutionException,
                                                    org.apache.maven.plugin.MojoFailureException

        Implement this method to perform this Mojo's execution. This method will only be called if !shouldExecutionBeSkipped() && isReGenerationRequired().

        Returns:
        true if the timestamp of the stale file should be updated.
        Throws:
        org.apache.maven.plugin.MojoExecutionException - if an unexpected problem occurs. Throwing this exception causes a "BUILD ERROR" message to be displayed.
        org.apache.maven.plugin.MojoFailureException - if an expected problem (such as a compilation failure) occurs. Throwing this exception causes a "BUILD FAILURE" message to be displayed.
      • getSources

        protected abstract java.util.List<java.net.URL> getSources()
        Override this method to acquire a List holding all URLs to the sources which this AbstractJaxbMojo should use to produce its output (XSDs files for AbstractXsdGeneratorMojos and Java Source Code for AbstractJavaGeneratorMojos).
        Returns:
        A non-null List holding URLs to sources used by this AbstractJaxbMojo to produce its output.
      • getOutputDirectory

        protected abstract java.io.File getOutputDirectory()
        Retrieves the directory where the generated files should be written to.
        Returns:
        the directory where the generated files should be written to.
      • getClasspath

        protected abstract java.util.List<java.lang.String> getClasspath()
                                                                  throws org.apache.maven.plugin.MojoExecutionException
        Retrieves the configured List of paths from which this AbstractJaxbMojo and its internal toolset (XJC or SchemaGen) should read bytecode classes.
        Returns:
        the configured List of paths from which this AbstractJaxbMojo and its internal toolset (XJC or SchemaGen) should read classes.
        Throws:
        org.apache.maven.plugin.MojoExecutionException - if the classpath could not be retrieved.
      • warnAboutIncorrectPluginConfiguration

        protected void warnAboutIncorrectPluginConfiguration​(java.lang.String propertyName,
                                                             java.lang.String description)
        Convenience method to invoke when some plugin configuration is incorrect. Will output the problem as a warning with some degree of log formatting.
        Parameters:
        propertyName - The name of the problematic property.
        description - The problem description.
      • logAndReturnToolArguments

        protected final java.lang.String[] logAndReturnToolArguments​(java.lang.String[] arguments,
                                                                     java.lang.String toolName)
        Parameters:
        arguments - The final arguments to be passed to a JAXB tool (XJC or SchemaGen).
        toolName - The name of the tool.
        Returns:
        the arguments, untouched.
      • getStaleFileName

        protected abstract java.lang.String getStaleFileName()
        Retrieves the last name part of the stale file. The full name of the stale file will be generated by pre-pending "." + getExecution().getExecutionId() before this staleFileName.
        Returns:
        The name of the stale file used by this AbstractJavaGeneratorMojo to detect staleness amongst its generated files.
      • getStaleFile

        protected final java.io.File getStaleFile()
        Acquires the staleFile for this execution
        Returns:
        the staleFile (used to define where) for this execution
      • getEncoding

        protected final java.lang.String getEncoding​(boolean warnIfPlatformEncoding)

        The algorithm for finding the encoding to use is as follows (where the first non-null value found is used for encoding):

        1. If the configuration property is explicitly given within the plugin's configuration, use that value.
        2. If the Maven property project.build.sourceEncoding is defined, use its value.
        3. Otherwise use the value from the system property file.encoding.
        Parameters:
        warnIfPlatformEncoding - Defines if a warning should be logged if encoding is not configured but the platform encoding (system property file.encoding) is used
        Returns:
        The encoding to be used by this AbstractJaxbMojo and its tools.
        See Also:
        encoding
      • getEpisodeFile

        protected java.io.File getEpisodeFile​(java.lang.String episodeFileName)
                                       throws org.apache.maven.plugin.MojoExecutionException
        Retrieves a File to the JAXB Episode (which is normally written during the XJC process). Moreover, ensures that the parent directory of that File is created, to enable writing the File.
        Parameters:
        episodeFileName - null to indicate that the standard episode file name ("sun-jaxb.episode") should be used, and otherwise a non-empty name which should be used as the episode file name.
        Returns:
        A non-null File where the JAXB episode file should be written.
        Throws:
        org.apache.maven.plugin.MojoExecutionException - if the parent directory of the episode file could not be created.
      • logPluginAndJaxbDependencyInfo

        private void logPluginAndJaxbDependencyInfo()
      • getInjectedObject

        private <T> T getInjectedObject​(T objectOrNull,
                                        java.lang.String objectName)
      • updateStaleFileTimestamp

        private void updateStaleFileTimestamp()
                                       throws org.apache.maven.plugin.MojoExecutionException
        Throws:
        org.apache.maven.plugin.MojoExecutionException
      • logSystemPropertiesAndBasedir

        protected void logSystemPropertiesAndBasedir()
        Prints out the system properties to the Maven Log at Debug level.