-
Notifications
You must be signed in to change notification settings - Fork 30
/
AspectJPlugin.java
105 lines (81 loc) · 5.31 KB
/
AspectJPlugin.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package io.freefair.gradle.plugins.aspectj;
import io.freefair.gradle.plugins.aspectj.internal.DefaultAspectjSourceSet;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.file.FileCollection;
import org.gradle.api.file.SourceDirectorySet;
import org.gradle.api.internal.plugins.DslObject;
import org.gradle.api.plugins.JavaBasePlugin;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.plugins.JavaPluginExtension;
import org.gradle.api.plugins.internal.JvmPluginsHelper;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.TaskProvider;
import org.gradle.jvm.toolchain.JavaLauncher;
import org.gradle.jvm.toolchain.JavaToolchainService;
/**
* @see org.gradle.api.plugins.GroovyBasePlugin
* @see org.gradle.api.plugins.GroovyPlugin
*/
public class AspectJPlugin implements Plugin<Project> {
private Project project;
private Provider<JavaLauncher> defaultLauncher;
@Override
public void apply(Project project) {
if (project.getPlugins().hasPlugin(AspectJPostCompileWeavingPlugin.class)) {
throw new IllegalStateException("Another aspectj plugin (which is excludes this one) has already been applied to the project.");
}
project.getPlugins().withId("com.android.application", ignored -> {
throw new IllegalStateException("The 'io.freefair.aspectj' plugin is not compatible with android projects");
});
project.getPlugins().withId("com.android.library", ignored -> {
throw new IllegalStateException("The 'io.freefair.aspectj' plugin is not compatible with android projects");
});
this.project = project;
project.getPlugins().apply(AspectJBasePlugin.class);
project.getPlugins().apply(JavaBasePlugin.class);
JavaPluginExtension plugin = project.getExtensions().getByType(JavaPluginExtension.class);
JavaToolchainService service = project.getExtensions().getByType(JavaToolchainService.class);
defaultLauncher = service.launcherFor(plugin.getToolchain());
plugin.getSourceSets().all(this::configureSourceSet);
project.getPlugins().withType(JavaPlugin.class, javaPlugin -> {
SourceSet main = plugin.getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME);
SourceSet test = plugin.getSourceSets().getByName(SourceSet.TEST_SOURCE_SET_NAME);
Configuration aspectpath = project.getConfigurations().getByName(WeavingSourceSet.getAspectConfigurationName(main));
Configuration testAspectpath = project.getConfigurations().getByName(WeavingSourceSet.getAspectConfigurationName(test));
testAspectpath.extendsFrom(aspectpath);
WeavingSourceSet.getAspectPath(test).setFrom(main.getOutput(), testAspectpath);
});
}
private void configureSourceSet(SourceSet sourceSet) {
DefaultAspectjSourceSet aspectjSourceSet = new DefaultAspectjSourceSet(project.getObjects(), sourceSet);
new DslObject(sourceSet).getConvention().getPlugins().put("aspectj", aspectjSourceSet);
final SourceDirectorySet aspectjSource = AspectjSourceSet.getAspectj(sourceSet);
aspectjSource.srcDir("src/" + sourceSet.getName() + "/aspectj");
// Explicitly capture only a FileCollection in the lambda below for compatibility with configuration-cache.
//noinspection UnnecessaryLocalVariable
FileCollection aspectjSourceFileCollection = aspectjSource;
sourceSet.getResources().getFilter().exclude(element -> aspectjSourceFileCollection.contains(element.getFile()));
sourceSet.getAllJava().source(aspectjSource);
sourceSet.getAllSource().source(aspectjSource);
Configuration aspect = project.getConfigurations().create(WeavingSourceSet.getAspectConfigurationName(sourceSet));
WeavingSourceSet.getAspectPath(sourceSet).from(aspect);
Configuration inpath = project.getConfigurations().create(WeavingSourceSet.getInpathConfigurationName(sourceSet));
WeavingSourceSet.getInPath(sourceSet).from(inpath);
project.getConfigurations().getByName(sourceSet.getImplementationConfigurationName()).extendsFrom(aspect);
project.getConfigurations().getByName(sourceSet.getCompileOnlyConfigurationName()).extendsFrom(inpath);
final TaskProvider<AspectjCompile> compileTask = project.getTasks().register(sourceSet.getCompileTaskName("aspectj"), AspectjCompile.class, compile -> {
JvmPluginsHelper.configureForSourceSet(sourceSet, aspectjSource, compile, compile.getOptions(), project);
compile.dependsOn(sourceSet.getCompileJavaTaskName());
compile.getLauncher().convention(defaultLauncher);
compile.setDescription("Compiles the " + sourceSet.getName() + " AspectJ source.");
compile.setSource(aspectjSource);
compile.getAjcOptions().getAspectpath().from(WeavingSourceSet.getAspectPath(sourceSet));
compile.getAjcOptions().getInpath().from(WeavingSourceSet.getInPath(sourceSet));
});
JvmPluginsHelper.configureOutputDirectoryForSourceSet(sourceSet, aspectjSource, project, compileTask, compileTask.map(AspectjCompile::getOptions));
project.getTasks().named(sourceSet.getClassesTaskName(), task -> task.dependsOn(compileTask));
}
}