Class ByteBuddyMojo

java.lang.Object
org.apache.maven.plugin.AbstractMojo
net.bytebuddy.build.maven.ByteBuddyMojo
All Implemented Interfaces:
org.apache.maven.plugin.ContextEnabled, org.apache.maven.plugin.Mojo
Direct Known Subclasses:
ByteBuddyMojo.ForProductionTypes, ByteBuddyMojo.ForTestTypes

public abstract class ByteBuddyMojo extends org.apache.maven.plugin.AbstractMojo
A Maven plugin for applying Byte Buddy transformations during a build.
  • Field Details

    • JAVA_FILE_EXTENSION

      private static final String JAVA_FILE_EXTENSION
      The file extension for Java source files.
      See Also:
    • JAVA_CLASS_EXTENSION

      private static final String JAVA_CLASS_EXTENSION
      The file extension for Java class files.
      See Also:
    • context

      @MaybeNull @Component public org.sonatype.plexus.build.incremental.BuildContext context
      The build context to support incremental builds.
    • project

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

      @UnknownNull @Parameter(defaultValue="${mojoExecution}", readonly=true) public org.apache.maven.plugin.MojoExecution execution
      The current execution of this plugin.
    • repositorySystem

      @UnknownNull @Component public org.eclipse.aether.RepositorySystem repositorySystem
      The currently used repository system.
    • repositorySystemSession

      @MaybeNull @Parameter(defaultValue="${repositorySystemSession}", readonly=true) public org.eclipse.aether.RepositorySystemSession repositorySystemSession
      The currently used system session for the repository system.
    • transformations

      @MaybeNull @Parameter public List<Transformation> transformations

      The list of transformations. A transformation must specify the plugin property, containing the name of a class to apply. Additionally, it is possible to optionally specify Maven coordinates for a project that contains this plugin class as groupId, artifactId and version. If any of the latter properties is not set, this projects coordinate is used.

      For example, the following configuration applies the foo.Bar class which must implement Plugin from artifact transform-artifact with this project's group and version:

      
       <transformations>
         <transformation>
           <plugin>foo.Bar< /plugin>
           <artifactId>transform-artifact< /artifactId>
         < /transformation>
       < /transformations>
       

      If the list of transformations is empty or is not supplied at all, this plugin does not apply but prints a warning.

    • initialization

      @MaybeNull @Parameter public Initialization initialization

      The initializer used for creating a ByteBuddy instance and for applying a transformation. By default, a type is rebased. The initializer's entryPoint property can be set to any constant name of EntryPoint.Default or to a class name. If the latter applies, it is possible to set Maven coordinates for a Maven plugin which defines this class where any property defaults to this project's coordinates.

      For example, the following configuration applies the foo.Qux class which must implement EntryPoint from artifact initialization-artifact with this project's group and version:

      
       <initialization>
         <entryPoint>foo.Qux< /entryPoint>
         <artifactId>initialization-artifact< /artifactId>
       < /initialization>
       
    • suffix

      @MaybeNull @Parameter public String suffix
      Specifies the method name suffix that is used when type's method need to be rebased. If this property is not set or is empty, a random suffix will be appended to any rebased method. If this property is set, the supplied value is appended to the original method name.
    • failOnLiveInitializer

      @Parameter(defaultValue="true", required=true) public boolean failOnLiveInitializer
      When transforming classes during build time, it is not possible to apply any transformations which require a class in its loaded state. Such transformations might imply setting a type's static field to a user interceptor or similar transformations. If this property is set to false, this plugin does not throw an exception if such a live initializer is defined during a transformation process.
    • skip

      @Parameter(defaultValue="false", required=true) public boolean skip
      When set to true, this mojo is not applied to the current module.
    • warnOnMissingOutputDirectory

      @Parameter(defaultValue="true", required=true) public boolean warnOnMissingOutputDirectory
      When set to true, this mojo warns of an non-existent output directory.
    • warnOnEmptyTypeSet

      @Parameter(defaultValue="true", required=true) public boolean warnOnEmptyTypeSet
      When set to true, this mojo warns of not having transformed any types.
    • failFast

      @Parameter(defaultValue="true", required=true) public boolean failFast
      When set to true, this mojo fails immediately if a plugin cannot be applied.
    • extendedParsing

      @Parameter(defaultValue="false", required=true) public boolean extendedParsing
      When set to true, the debug information of class files should be parsed to extract parameter names.
    • discovery

      @MaybeNull @Parameter(defaultValue="EMPTY", required=true) public Discovery discovery
      Determines if the build should discover Byte Buddy build plugins on this Maven plugin's class loader. Discovered plugins are stored by their name in the /META-INF/net.bytebuddy/build.plugins file where each line contains the fully qualified class name. Discovered plugins are not provided with any explicit constructor arguments.
    • classPathDiscovery

      @Parameter(defaultValue="false", required=true) public boolean classPathDiscovery
      Scans the class path (or test class path) for Byte Buddy plugins to apply. This is not normally recommended as it might cause a spurious application of plugins that are accidentally configured on the class path. It can however serve as a convenience in projects with few dependencies where this allows for the use of Maven's dependency version management.
    • threads

      @Parameter(defaultValue="0", required=true) public int threads
      Indicates the amount of threads used for parallel type processing or 0 for serial processing.
    • incremental

      @Parameter(defaultValue="false", required=true) public boolean incremental
      Determines if plugins are attempted to be built incrementally.
    • staleMilliseconds

      @Parameter(defaultValue="0", required=true) public int staleMilliseconds
      Determines the tolerance of many milliseconds between this plugin run and the last edit are permitted for considering a file as stale if the plugin was executed before. Can be set to -1 to disable.
  • Constructor Details

    • ByteBuddyMojo

      public ByteBuddyMojo()
  • Method Details

    • execute

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

      protected abstract String getOutputDirectory()
      Returns the output directory to search for class files.
      Returns:
      The output directory to search for class files.
    • getSourceDirectory

      @MaybeNull protected abstract String getSourceDirectory()
      Returns the source directory that determines the class files to process.
      Returns:
      The source directory that serves as an input for the transformation.
    • getClassPathElements

      protected abstract List<String> getClassPathElements() throws org.apache.maven.plugin.MojoFailureException
      Returns the class path elements of the relevant output directory.
      Returns:
      The class path elements of the relevant output directory.
      Throws:
      org.apache.maven.plugin.MojoFailureException - If the class path cannot be resolved.
    • apply

      private Plugin.Engine.Summary apply(File root, List<? extends String> classPath, List<ByteBuddyMojo.Transformer> transformers, Plugin.Engine.Source source, boolean filtered) throws org.apache.maven.plugin.MojoExecutionException, IOException
      Applies the instrumentation.
      Parameters:
      root - The root folder that contains all class files.
      classPath - An iterable over all class path elements.
      transformers - The transformers to apply.
      source - The source for the plugin's application.
      filtered - true if files are already filtered and should not be checked for staleness.
      Returns:
      A summary of the applied transformation.
      Throws:
      org.apache.maven.plugin.MojoExecutionException - If the plugin cannot be applied.
      IOException - If an I/O exception occurs.
    • discover

      private void discover(InputStream inputStream, Set<String> undiscoverable, List<ByteBuddyMojo.Transformer> transformers, @MaybeNull List<String> classPath) throws IOException
      Discovers plugins from an input stream representing a META-INF/net.bytebuddy/build.plugins file.
      Parameters:
      inputStream - The input stream to read from.
      undiscoverable - A set of undiscoverable plugins.
      transformers - The list of transformers to add discovered plugins to.
      classPath - The class path elements to add if a plugin is loaded from the class path or null if the plugin is discovered as a dependency
      Throws:
      IOException - If an I/O exception occurs.
    • findJavaVersionString

      @MaybeNull private static String findJavaVersionString(org.apache.maven.project.MavenProject project, String property)
      Makes a best effort of locating the configured Java version.
      Parameters:
      project - The relevant Maven project.
      property - The targeted Maven property.
      Returns:
      The Java version string of the configured build Java version or null if no explicit configuration was detected.