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.
    Author:
    Lennart Jörelid
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static Pattern CONTAINS_WHITESPACE
      Pattern matching strings containing whitespace (or consisting only of whitespace).
      protected 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 String locale
      A Locale definition to create and set the system (default) Locale when the XJB or SchemaGen tools executes.
      static String NEWLINE
      Platform-independent newline control string.
      static String PACKAGE_INFO_FILENAME
      Standard name of the package-info.java file which may contain JAXB annotations and Package JavaDoc.
      protected File staleFileDirectory
      The directory where the staleFile is found.
      static String STANDARD_EPISODE_FILENAME
      Standard name of the generated JAXB episode file.
      static List<Filter<File>> STANDARD_EXCLUDE_FILTERS
      Standard excludes Filters for all Java generator Mojos.
      • 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 List<String> getClasspath()
      Retrieves the configured List of paths from which this AbstractJaxbMojo and its internal toolset (XJC or SchemaGen) should read bytecode classes.
      protected 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 File getEpisodeFile​(String episodeFileName)
      Retrieves a File to the JAXB Episode (which is normally written during the XJC process).
      org.apache.maven.plugin.MojoExecution getExecution()  
      protected abstract File getOutputDirectory()
      Retrieves the directory where the generated files should be written to.
      protected org.apache.maven.project.MavenProject getProject()  
      protected abstract List<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 File getStaleFile()
      Acquires the staleFile for this execution
      protected abstract String getStaleFileName()
      Retrieves the last name part of the stale file.
      protected abstract boolean isReGenerationRequired()  
      protected String[] logAndReturnToolArguments​(String[] arguments, String toolName)  
      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.
      protected void warnAboutIncorrectPluginConfiguration​(String propertyName, 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
    • Field Detail

      • STANDARD_EPISODE_FILENAME

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

        public static final 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 String NEWLINE
        Platform-independent newline control string.
      • CONTAINS_WHITESPACE

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

        public static final List<Filter<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.

      • staleFileDirectory

        @Parameter(defaultValue="${project.build.directory}/jaxb2",
                   readonly=true,
                   required=true)
        protected 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
      • locale

        @Parameter(required=false)
        protected 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 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 List<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 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 List<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​(String propertyName,
                                                             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 String[] logAndReturnToolArguments​(String[] arguments,
                                                           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 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 File getStaleFile()
        Acquires the staleFile for this execution
        Returns:
        the staleFile (used to define where) for this execution
      • getEncoding

        protected final 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 File getEpisodeFile​(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.
      • logSystemPropertiesAndBasedir

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