18
18
19
19
import java .io .File ;
20
20
import java .io .IOException ;
21
+ import java .net .URL ;
22
+ import java .net .URLClassLoader ;
21
23
import java .nio .file .Files ;
22
24
import java .nio .file .Path ;
23
25
import java .nio .file .StandardOpenOption ;
26
+ import java .util .ArrayList ;
24
27
import java .util .Collections ;
25
28
import java .util .List ;
29
+ import java .util .concurrent .Callable ;
26
30
import java .util .function .Supplier ;
27
31
import java .util .stream .Stream ;
28
32
33
37
import org .gradle .api .DefaultTask ;
34
38
import org .gradle .api .Task ;
35
39
import org .gradle .api .Transformer ;
40
+ import org .gradle .api .file .ConfigurableFileCollection ;
36
41
import org .gradle .api .file .DirectoryProperty ;
37
42
import org .gradle .api .file .FileCollection ;
38
43
import org .gradle .api .file .FileTree ;
39
44
import org .gradle .api .provider .ListProperty ;
40
45
import org .gradle .api .provider .Property ;
46
+ import org .gradle .api .tasks .Classpath ;
41
47
import org .gradle .api .tasks .IgnoreEmptyDirectories ;
42
48
import org .gradle .api .tasks .Input ;
43
49
import org .gradle .api .tasks .InputFiles ;
58
64
* @author Scott Frederick
59
65
* @author Ivan Malutin
60
66
* @author Phillip Webb
67
+ * @author Dmytro Nosan
61
68
*/
62
69
public abstract class ArchitectureCheck extends DefaultTask {
63
70
@@ -80,14 +87,17 @@ private List<String> asDescriptions(List<ArchRule> rules) {
80
87
}
81
88
82
89
@ TaskAction
83
- void checkArchitecture () throws IOException {
84
- JavaClasses javaClasses = new ClassFileImporter ().importPaths (classFilesPaths ());
85
- List <EvaluationResult > violations = evaluate (javaClasses ).filter (EvaluationResult ::hasViolation ).toList ();
86
- File outputFile = getOutputDirectory ().file ("failure-report.txt" ).get ().getAsFile ();
87
- writeViolationReport (violations , outputFile );
88
- if (!violations .isEmpty ()) {
89
- throw new VerificationException ("Architecture check failed. See '" + outputFile + "' for details." );
90
- }
90
+ void checkArchitecture () throws Exception {
91
+ withCompileClasspath (() -> {
92
+ JavaClasses javaClasses = new ClassFileImporter ().importPaths (classFilesPaths ());
93
+ List <EvaluationResult > violations = evaluate (javaClasses ).filter (EvaluationResult ::hasViolation ).toList ();
94
+ File outputFile = getOutputDirectory ().file ("failure-report.txt" ).get ().getAsFile ();
95
+ writeViolationReport (violations , outputFile );
96
+ if (!violations .isEmpty ()) {
97
+ throw new VerificationException ("Architecture check failed. See '" + outputFile + "' for details." );
98
+ }
99
+ return null ;
100
+ });
91
101
}
92
102
93
103
private List <Path > classFilesPaths () {
@@ -98,6 +108,22 @@ private Stream<EvaluationResult> evaluate(JavaClasses javaClasses) {
98
108
return getRules ().get ().stream ().map ((rule ) -> rule .evaluate (javaClasses ));
99
109
}
100
110
111
+ private void withCompileClasspath (Callable <?> callable ) throws Exception {
112
+ ClassLoader previous = Thread .currentThread ().getContextClassLoader ();
113
+ try {
114
+ List <URL > urls = new ArrayList <>();
115
+ for (File file : getCompileClasspath ().getFiles ()) {
116
+ urls .add (file .toURI ().toURL ());
117
+ }
118
+ ClassLoader classLoader = new URLClassLoader (urls .toArray (new URL [0 ]), getClass ().getClassLoader ());
119
+ Thread .currentThread ().setContextClassLoader (classLoader );
120
+ callable .call ();
121
+ }
122
+ finally {
123
+ Thread .currentThread ().setContextClassLoader (previous );
124
+ }
125
+ }
126
+
101
127
private void writeViolationReport (List <EvaluationResult > violations , File outputFile ) throws IOException {
102
128
outputFile .getParentFile ().mkdirs ();
103
129
StringBuilder report = new StringBuilder ();
@@ -126,6 +152,10 @@ final FileTree getInputClasses() {
126
152
return this .classes .getAsFileTree ();
127
153
}
128
154
155
+ @ InputFiles
156
+ @ Classpath
157
+ public abstract ConfigurableFileCollection getCompileClasspath ();
158
+
129
159
@ Optional
130
160
@ InputFiles
131
161
@ PathSensitive (PathSensitivity .RELATIVE )
0 commit comments