Class ServiceloaderMojo

  • All Implemented Interfaces:
    org.apache.maven.plugin.ContextEnabled, org.apache.maven.plugin.Mojo

    @Mojo(name="generate",
          defaultPhase=COMPILE,
          requiresDependencyResolution=COMPILE,
          requiresProject=true,
          threadSafe=true)
    public class ServiceloaderMojo
    extends org.apache.maven.plugin.AbstractMojo
    Goal that generates the services files
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private org.sonatype.plexus.build.incremental.BuildContext buildContext  
      private java.io.File classFolder  
      private java.util.List<java.lang.String> compileClasspath  
      private java.lang.String[] excludes  
      private boolean failOnMissingServiceClass  
      private java.lang.String[] includes  
      private java.io.File outputDirectory  
      protected org.apache.maven.project.MavenProject project
      Maven Internal: Project to interact with.
      private java.lang.String[] services
      The service interfaces to generate service files for
      • Fields inherited from interface org.apache.maven.plugin.Mojo

        ROLE
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void execute()
      The main entry point for this Mojo.
      private java.util.Map<java.lang.String,​java.util.List<java.lang.String>> findImplementations​(java.lang.ClassLoader loader, java.util.List<java.lang.Class<?>> interfaceClasses)
      Finds all implementations of interfaces in a folder
      private java.net.URL[] generateClassPathUrls()
      Generates a URL[] with the project class path (can be used by a URLClassLoader)
      private java.io.File getClassFolder()  
      java.util.List<java.lang.String> getCompileClasspath()  
      private java.io.File getOutputDirectory()  
      org.apache.maven.project.MavenProject getProject()  
      java.lang.String[] getServices()  
      (package private) java.util.List<java.lang.String> listCompiledClasses​(java.io.File classFolder)
      Walks the classFolder and finds all classes
      private java.util.List<java.lang.String> listCompiledClassesRegex​(java.io.File classFolder)
      Walks the classFolder and finds all .class files
      private java.util.List<java.lang.Class<?>> loadServiceClasses​(java.lang.ClassLoader loader)
      Loads all interfaces using the provided ClassLoader
      void setBuildContext​(org.sonatype.plexus.build.incremental.BuildContext buildContext)  
      private boolean skipProject()  
      private void writeServiceFiles​(java.util.Map<java.lang.String,​java.util.List<java.lang.String>> serviceImplementations)
      Writes the output for the service files to disk
      • 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

      • buildContext

        @Component
        private org.sonatype.plexus.build.incremental.BuildContext buildContext
      • project

        @Parameter(property="project",
                   required=true,
                   readonly=true)
        protected org.apache.maven.project.MavenProject project
        Maven Internal: Project to interact with.
      • classFolder

        @Parameter(defaultValue="${project.build.outputDirectory}",
                   required=true,
                   readonly=true)
        private java.io.File classFolder
      • compileClasspath

        @Parameter(defaultValue="${project.compileClasspathElements}",
                   required=true,
                   readonly=true)
        private java.util.List<java.lang.String> compileClasspath
      • outputDirectory

        @Parameter(defaultValue="${project.build.outputDirectory}/META-INF/services",
                   required=true)
        private java.io.File outputDirectory
      • services

        @Parameter
        private java.lang.String[] services
        The service interfaces to generate service files for
      • includes

        @Parameter
        private java.lang.String[] includes
      • excludes

        @Parameter
        private java.lang.String[] excludes
      • failOnMissingServiceClass

        @Parameter(defaultValue="true")
        private boolean failOnMissingServiceClass
    • Constructor Detail

      • ServiceloaderMojo

        public ServiceloaderMojo()
    • Method Detail

      • getProject

        public org.apache.maven.project.MavenProject getProject()
      • getServices

        public java.lang.String[] getServices()
      • getClassFolder

        private java.io.File getClassFolder()
      • getCompileClasspath

        public java.util.List<java.lang.String> getCompileClasspath()
      • getOutputDirectory

        private java.io.File getOutputDirectory()
      • setBuildContext

        public void setBuildContext​(org.sonatype.plexus.build.incremental.BuildContext buildContext)
      • execute

        public void execute()
                     throws org.apache.maven.plugin.MojoExecutionException
        The main entry point for this Mojo.
        Throws:
        org.apache.maven.plugin.MojoExecutionException - when an exception occurred during the execution of this plugin
      • writeServiceFiles

        private void writeServiceFiles​(java.util.Map<java.lang.String,​java.util.List<java.lang.String>> serviceImplementations)
                                throws org.apache.maven.plugin.MojoExecutionException
        Writes the output for the service files to disk
        Parameters:
        serviceImplementations -
        Throws:
        org.apache.maven.plugin.MojoExecutionException
      • loadServiceClasses

        private java.util.List<java.lang.Class<?>> loadServiceClasses​(java.lang.ClassLoader loader)
                                                               throws org.apache.maven.plugin.MojoExecutionException
        Loads all interfaces using the provided ClassLoader
        Parameters:
        loader - the classloader
        Returns:
        thi List of Interface classes
        Throws:
        org.apache.maven.plugin.MojoExecutionException - is the interfaces are not interfaces or can not be found on the classpath
      • findImplementations

        private java.util.Map<java.lang.String,​java.util.List<java.lang.String>> findImplementations​(java.lang.ClassLoader loader,
                                                                                                           java.util.List<java.lang.Class<?>> interfaceClasses)
                                                                                                    throws org.apache.maven.plugin.MojoExecutionException
        Finds all implementations of interfaces in a folder
        Parameters:
        loader -
        interfaceClasses -
        Returns:
        Throws:
        org.apache.maven.plugin.MojoExecutionException
      • listCompiledClasses

        java.util.List<java.lang.String> listCompiledClasses​(java.io.File classFolder)
        Walks the classFolder and finds all classes
        Parameters:
        classFolder - the folder to scan for .class files
        Returns:
        the list of available class names
      • listCompiledClassesRegex

        private java.util.List<java.lang.String> listCompiledClassesRegex​(java.io.File classFolder)
        Walks the classFolder and finds all .class files
        Parameters:
        classFolder -
        Returns:
        the list of available class names
      • generateClassPathUrls

        private java.net.URL[] generateClassPathUrls()
                                              throws org.apache.maven.plugin.MojoExecutionException
        Generates a URL[] with the project class path (can be used by a URLClassLoader)
        Returns:
        the array of classpath URL's
        Throws:
        org.apache.maven.plugin.MojoExecutionException
      • skipProject

        private boolean skipProject()