@@ -13,18 +13,59 @@ import org.gradle.api.tasks.compile.AbstractCompile
1313import java.util.regex.Matcher
1414
1515/**
16- * instrument<Language> task plugin which performs build-time instrumentation of classes.
16+ * Gradle plugin that applies ByteBuddy plugins to perform build-time bytecode instrumentation.
17+ *
18+ * <p >This plugin appends a post-processing action to existing <strong >main</strong> compilation
19+ * tasks. This plugin allows to apply one or more ByteBuddy {@link net.bytebuddy.build.Plugin}
20+ * implementations.
21+ *
22+ * <h3>Configuration:</h3>
23+ * There are two main configuration points:
24+ * <ul >
25+ * <li >The {@code buildTimeInstrumentation } extension, which allows to specify:
26+ * <ul >
27+ * <li >The list of ByteBuddy plugin class names to apply</li>
28+ * <li >Additional classpath entries required to load the plugins</li>
29+ * </ul>
30+ * </li>
31+ * <li >The {@code buildTimeInstrumentationPlugin } configuration, which allows to specify
32+ * dependencies containing the ByteBuddy plugin implementations</li>
33+ * </ul>
34+ * <p >Example configuration:
35+ *
36+ * <pre >
37+ * buildTimeInstrumentation {
38+ * plugins = ['com.example.MyByteBuddyPlugin', 'com.example.AnotherPlugin']
39+ * additionalClasspath = [file('path/to/additional/classes')]
40+ * }
41+ *
42+ * dependencies {
43+ * buildTimeInstrumentationPlugin 'com.example:my-bytebuddy-plugin:1.0.0'
44+ * buildTimeInstrumentationPlugin project(path: ':some:project', configuration: 'buildTimeInstrumentationPlugin')
45+ * }
46+ * </pre>
47+ *
48+ * <h3>Requirements for ByteBuddy plugins:</h3>
49+ * <ul >
50+ * <li >Must implement {@link net.bytebuddy.build.Plugin}</li>
51+ * <li >Must have a constructor accepting a {@link File} parameter (the target directory)</li>
52+ * <li >Plugin classes must be available on the {@code buildTimeInstrumentationPlugin } configuration</li>
53+ * </ul>
54+ *
55+ * @see BuildTimeInstrumentationExtension
56+ * @see InstrumentPostProcessingAction
57+ * @see ByteBuddyInstrumenter
1758 */
1859@SuppressWarnings (' unused' )
19- class InstrumentPlugin implements Plugin<Project > {
60+ class BuildTimeInstrumentationPlugin implements Plugin<Project > {
2061 public static final String DEFAULT_JAVA_VERSION = ' default'
21- public static final String INSTRUMENT_PLUGIN_CLASSPATH_CONFIGURATION = ' instrumentPluginClasspath '
22- private final Logger logger = Logging . getLogger(InstrumentPlugin )
62+ public static final String BUILD_TIME_INSTRUMENTATION_PLUGIN_CONFIGURATION = ' buildTimeInstrumentationPlugin '
63+ private final Logger logger = Logging . getLogger(BuildTimeInstrumentationPlugin )
2364
2465 @Override
2566 void apply (Project project ) {
26- InstrumentExtension extension = project. extensions. create(' instrument ' , InstrumentExtension )
27- project. configurations. register(INSTRUMENT_PLUGIN_CLASSPATH_CONFIGURATION )
67+ BuildTimeInstrumentationExtension extension = project. extensions. create(' buildTimeInstrumentation ' , BuildTimeInstrumentationExtension )
68+ project. configurations. register(BUILD_TIME_INSTRUMENTATION_PLUGIN_CONFIGURATION )
2869
2970
3071 [' java' , ' kotlin' , ' scala' , ' groovy' ]. each { langPluginId ->
@@ -34,22 +75,22 @@ class InstrumentPlugin implements Plugin<Project> {
3475 }
3576 }
3677
37- private void configurePostCompilationInstrumentation (String language , Project project , InstrumentExtension extension ) {
78+ private void configurePostCompilationInstrumentation (String language , Project project , BuildTimeInstrumentationExtension extension ) {
3879 project. extensions. configure(SourceSetContainer ) { SourceSetContainer sourceSets ->
3980 // For any "main" source-set configure its compile task
4081 sourceSets. configureEach { SourceSet sourceSet ->
4182 def sourceSetName = sourceSet. name
42- logger. info(" [InstrumentPlugin ] source-set: $sourceSetName , language: $language " )
83+ logger. info(" [BuildTimeInstrumentationPlugin ] source-set: $sourceSetName , language: $language " )
4384
4485 if (! sourceSetName. startsWith(SourceSet . MAIN_SOURCE_SET_NAME )) {
45- logger. debug(" [InstrumentPlugin ] Skipping non-main source set {} for language {}" , sourceSetName, language)
86+ logger. debug(" [BuildTimeInstrumentationPlugin ] Skipping non-main source set {} for language {}" , sourceSetName, language)
4687 return
4788 }
4889
4990 def compileTaskName = sourceSet. getCompileTaskName(language)
50- logger. info(" [InstrumentPlugin ] compile task name: " + compileTaskName)
91+ logger. info(" [BuildTimeInstrumentationPlugin ] compile task name: " + compileTaskName)
5192
52- // For each compile task, append an instrumenting post-processing step
93+ // For each _main_ compile task, append an instrumenting post-processing step
5394 // Examples of compile tasks:
5495 // - compileJava,
5596 // - compileMain_java17Java,
@@ -61,11 +102,11 @@ class InstrumentPlugin implements Plugin<Project> {
61102 project. tasks. withType(AbstractCompile ). matching {
62103 it. name == compileTaskName && ! it. source. isEmpty()
63104 }. configureEach {
64- logger. info(' [InstrumentPlugin ] Applying instrumentPluginClasspath configuration as compile task input' )
65- it. inputs. files(project. configurations. named(INSTRUMENT_PLUGIN_CLASSPATH_CONFIGURATION ))
105+ logger. info(' [BuildTimeInstrumentationPlugin ] Applying buildTimeInstrumentationPlugin configuration as compile task input' )
106+ it. inputs. files(project. configurations. named(BUILD_TIME_INSTRUMENTATION_PLUGIN_CONFIGURATION ))
66107
67108 if (it. source. isEmpty()) {
68- logger. debug(" [InstrumentPlugin ] Skipping $compileTaskName for source set $sourceSetName as it has no source files" )
109+ logger. debug(" [BuildTimeInstrumentationPlugin ] Skipping $compileTaskName for source set $sourceSetName as it has no source files" )
69110 return
70111 }
71112
@@ -111,7 +152,7 @@ class InstrumentPlugin implements Plugin<Project> {
111152 tmpUninstrumentedClasses
112153 )
113154 )
114- logger. info(" [InstrumentPlugin ] Configured post-compile instrumentation for $compileTaskName for source-set $sourceSetName " )
155+ logger. info(" [BuildTimeInstrumentationPlugin ] Configured post-compile instrumentation for $compileTaskName for source-set $sourceSetName " )
115156 }
116157 }
117158 }
0 commit comments