2
2
3
3
import static co .elastic .apm .compile .tools .utils .Constants .ARTIFACT_TYPE_ATTR ;
4
4
5
- import com .android .build .api .artifact .MultipleArtifact ;
5
+ import com .android .build .api .artifact .ScopedArtifact ;
6
6
import com .android .build .api .variant .AndroidComponentsExtension ;
7
+ import com .android .build .api .variant .ScopedArtifacts ;
7
8
import com .android .build .api .variant .Variant ;
8
9
9
10
import org .gradle .api .Plugin ;
10
11
import org .gradle .api .Project ;
11
12
import org .gradle .api .artifacts .Configuration ;
12
- import org .gradle .api .file .ConfigurableFileCollection ;
13
- import org .gradle .api .file .Directory ;
14
- import org .gradle .api .file .FileCollection ;
15
- import org .gradle .api .file .FileTree ;
16
- import org .gradle .api .provider .Provider ;
17
- import org .gradle .api .tasks .Sync ;
18
13
import org .gradle .api .tasks .TaskProvider ;
19
14
20
- import java .io .File ;
21
- import java .util .concurrent .Callable ;
15
+ import java .util .Collections ;
22
16
23
- import co .elastic .apm .compile .tools .embedding .tasks .EmbeddedClassesGathererTask ;
17
+ import co .elastic .apm .compile .tools .embedding .extensions .ShadowExtension ;
18
+ import co .elastic .apm .compile .tools .embedding .tasks .EmbeddedClassesMergerTask ;
24
19
import kotlin .Unit ;
25
20
26
21
@ SuppressWarnings ("unchecked" )
27
22
public class EmbeddingDependenciesPlugin implements Plugin <Project > {
28
23
29
24
public static final String EMBEDDED_CLASSPATH_NAME = "embeddedClasspath" ;
25
+ private static final String SHADOW_EXTENSION_NAME = "shadowJar" ;
26
+ private ShadowExtension shadowExtension ;
30
27
31
28
@ Override
32
29
public void apply (Project project ) {
33
30
AndroidComponentsExtension <?, ?, Variant > componentsExtension = project .getExtensions ().getByType (AndroidComponentsExtension .class );
34
31
Configuration embeddedClasspath = getEmbeddedClasspath (project );
35
- Provider <FileCollection > classesProvider = getClassesProvider (project , embeddedClasspath );
36
- String embeddedClassesTaskName = "embeddedClasses" ;
37
- Provider <Directory > classesDir = project .getLayout ().getBuildDirectory ().dir (embeddedClassesTaskName );
38
- TaskProvider <Sync > syncEmbeddedClassesTask = project .getTasks ().register (embeddedClassesTaskName , Sync .class , sync -> {
39
- sync .from (classesProvider );
40
- sync .into (classesDir );
41
- });
32
+ shadowExtension = project .getExtensions ().create (SHADOW_EXTENSION_NAME , ShadowExtension .class );
42
33
43
34
componentsExtension .onVariants (componentsExtension .selector ().all (), variant -> {
44
- TaskProvider <EmbeddedClassesGathererTask > taskProvider = getEmbeddedClassesGathererTaskProvider (project , classesDir , variant );
45
- taskProvider .configure (task -> task .dependsOn (syncEmbeddedClassesTask ));
46
35
47
- variant .getArtifacts ().use (taskProvider )
48
- .wiredWith ( EmbeddedClassesGathererTask :: getOutputDir )
49
- . toAppendTo ( MultipleArtifact . ALL_CLASSES_DIRS . INSTANCE );
36
+ variant .getArtifacts ().forScope ( ScopedArtifacts . Scope . PROJECT ). use (getEmbeddedClassesMergerTaskProvider ( project , embeddedClasspath , variant ) )
37
+ .toTransform ( ScopedArtifact . CLASSES . INSTANCE , EmbeddedClassesMergerTask :: getInputJars ,
38
+ EmbeddedClassesMergerTask :: getLocalClassesDirs , EmbeddedClassesMergerTask :: getOutputFile );
50
39
return Unit .INSTANCE ;
51
40
});
52
41
}
53
42
54
- private TaskProvider <EmbeddedClassesGathererTask > getEmbeddedClassesGathererTaskProvider (Project project , Provider < Directory > classesDir , Variant variant ) {
55
- TaskProvider <EmbeddedClassesGathererTask > taskProvider = project .getTasks ().register (variant .getName () + "EmbeddedClassesGatherer " , EmbeddedClassesGathererTask .class );
43
+ private TaskProvider <EmbeddedClassesMergerTask > getEmbeddedClassesMergerTaskProvider (Project project , Configuration embedded , Variant variant ) {
44
+ TaskProvider <EmbeddedClassesMergerTask > taskProvider = project .getTasks ().register (variant .getName () + "EmbeddedClassesMerger " , EmbeddedClassesMergerTask .class );
56
45
taskProvider .configure (task -> {
57
- task .getClassesDir ().set (classesDir );
58
- task .getOutputDir ().set (project .getLayout ().getBuildDirectory ().dir (task .getName ()));
46
+ task .from (task .getLocalClassesDirs ());
47
+ task .setConfigurations (Collections .singletonList (embedded ));
48
+ for (ShadowExtension .Relocation relocation : shadowExtension .getRelocations ()) {
49
+ task .relocate (relocation .getPattern ().get (), relocation .getDestination ().get ());
50
+ }
59
51
});
60
52
return taskProvider ;
61
53
}
@@ -76,36 +68,4 @@ private Configuration getEmbeddedClasspath(Project project) {
76
68
77
69
return classpath ;
78
70
}
79
-
80
- private Provider <FileCollection > getClassesProvider (Project project , Configuration classpath ) {
81
- return project .provider (new LazyFileCollectionProvider (project , classpath ));
82
- }
83
-
84
- private static class LazyFileCollectionProvider implements Callable <FileCollection > {
85
- private final Project project ;
86
- private final Configuration classpath ;
87
- private FileCollection cachedFileCollection ;
88
-
89
- private LazyFileCollectionProvider (Project project , Configuration classpath ) {
90
- this .project = project ;
91
- this .classpath = classpath ;
92
- }
93
-
94
- @ Override
95
- public FileCollection call () {
96
- if (cachedFileCollection != null ) {
97
- return cachedFileCollection ;
98
- }
99
- ConfigurableFileCollection fileCollection = project .files ();
100
- for (File file : classpath .getFiles ()) {
101
- if (file .getName ().endsWith (".jar" )) {
102
- FileTree files = project .zipTree (file );
103
- fileCollection .from (files );
104
- }
105
- }
106
-
107
- cachedFileCollection = fileCollection ;
108
- return fileCollection ;
109
- }
110
- }
111
71
}
0 commit comments