@@ -36,17 +36,21 @@ public class GradleLibraryResolver {
36
36
37
37
private static String JAVA_PLUGIN = "org.gradle.api.plugins.JavaPlugin" ;
38
38
39
- private Map <String , JavaClass > gradleLibraries = new HashMap <>();
39
+ private Map <String , JavaClass > gradleClasses = new HashMap <>();
40
40
private Set <String > javaConfigurations = new HashSet <>();
41
41
private Set <String > javaPlugins = new HashSet <>();
42
42
private String gradleHome ;
43
43
private String gradleVersion ;
44
44
private boolean gradleWrapperEnabled ;
45
45
private String gradleUserHome ;
46
46
private Path workspacePath ;
47
+ private File coreAPI ;
48
+ private File pluginAPI ;
49
+ private boolean needToLoadClasses ;
47
50
48
51
public GradleLibraryResolver () {
49
52
this .javaPlugins .addAll (Arrays .asList ("java" , "application" , "groovy" , "java-library" , "war" ));
53
+ this .needToLoadClasses = true ;
50
54
}
51
55
52
56
public void setGradleHome (String gradleHome ) {
@@ -69,47 +73,52 @@ public void setWorkspacePath(Path workspacePath) {
69
73
this .workspacePath = workspacePath ;
70
74
}
71
75
72
- public Map <String , JavaClass > getGradleLibraries () {
73
- return this .gradleLibraries ;
76
+ public Map <String , JavaClass > getGradleClasses () {
77
+ return this .gradleClasses ;
74
78
}
75
79
76
80
public Set <String > getJavaConfigurations () {
77
81
return this .javaConfigurations ;
78
82
}
79
83
80
- public void resolve () {
84
+ public boolean resolveGradleAPI () {
85
+ this .needToLoadClasses = true ;
81
86
Path gradleUserHomePath = (this .gradleUserHome == null ) ? Path .of (System .getProperty ("user.home" ), ".gradle" )
82
87
: Path .of (this .gradleUserHome );
83
- File libFile = null ;
84
88
if (this .gradleWrapperEnabled ) {
85
- libFile = findLibWithWrapper (gradleUserHomePath );
89
+ this . coreAPI = findCoreAPIWithWrapper (gradleUserHomePath );
86
90
} else if (this .gradleVersion != null ) {
87
- libFile = findLibWithDist (gradleUserHomePath , "gradle-" + this .gradleVersion );
91
+ this . coreAPI = findCoreAPIWithDist (gradleUserHomePath , "gradle-" + this .gradleVersion );
88
92
} else if (this .gradleHome != null ) {
89
- Path libPath = Path .of (this .gradleHome ).resolve ("lib" );
90
- libFile = findLibFile (libPath .toFile ());
93
+ this .coreAPI = findCoreAPI (Path .of (this .gradleHome ).resolve ("lib" ).toFile ());
91
94
} else {
92
- return ;
95
+ return false ;
96
+ }
97
+ if (!isValidFile (this .coreAPI )) {
98
+ return false ;
93
99
}
94
- if (libFile == null || !libFile .exists ()) {
100
+ this .pluginAPI = findPluginAPI (this .coreAPI .toPath ().getParent ().resolve (Path .of ("plugins" )).toFile ());
101
+ return isValidFile (this .pluginAPI );
102
+ }
103
+
104
+ public void loadGradleClasses () {
105
+ boolean isAPIValid = isValidFile (this .coreAPI ) && isValidFile (this .pluginAPI );
106
+ if (!this .needToLoadClasses || (!isAPIValid && !this .resolveGradleAPI ())) {
95
107
return ;
96
108
}
97
109
try {
98
- JarFile libJar = new JarFile (libFile );
99
- getGradleLibraries (libFile .toPath (), libJar );
100
- File pluginLibFile = findPluginLibFile (libFile .toPath ().getParent ().resolve (Path .of ("plugins" )).toFile ());
101
- if (pluginLibFile == null ) {
102
- return ;
103
- }
104
- JarFile pluginLibJar = new JarFile (pluginLibFile );
105
- getGradleLibraries (pluginLibFile .toPath (), pluginLibJar );
106
- resolveJavaConfigurations ();
110
+ JarFile coreAPIJar = new JarFile (this .coreAPI );
111
+ loadClasses (this .coreAPI .toPath (), coreAPIJar );
112
+ JarFile pluginAPIJar = new JarFile (this .pluginAPI );
113
+ loadClasses (this .pluginAPI .toPath (), pluginAPIJar );
114
+ loadJavaConfigurations ();
115
+ this .needToLoadClasses = false ;
107
116
} catch (Exception e ) {
108
117
// Do Nothing
109
118
}
110
119
}
111
120
112
- private File findLibWithWrapper (Path gradleUserHomePath ) {
121
+ private File findCoreAPIWithWrapper (Path gradleUserHomePath ) {
113
122
if (this .workspacePath == null ) {
114
123
return null ;
115
124
}
@@ -128,7 +137,7 @@ private File findLibWithWrapper(Path gradleUserHomePath) {
128
137
Matcher matcher = p .matcher (content );
129
138
if (matcher .find ()) {
130
139
String gradleDist = matcher .group (1 );
131
- return findLibWithDist (gradleUserHomePath , gradleDist );
140
+ return findCoreAPIWithDist (gradleUserHomePath , gradleDist );
132
141
}
133
142
}
134
143
}
@@ -138,12 +147,12 @@ private File findLibWithWrapper(Path gradleUserHomePath) {
138
147
return null ;
139
148
}
140
149
141
- private File findLibWithDist (Path gradleUserHomePath , String gradleDist ) {
150
+ private File findCoreAPIWithDist (Path gradleUserHomePath , String gradleDist ) {
142
151
Path distPath = gradleUserHomePath .resolve (Path .of ("wrapper" , "dists" ));
143
152
File distFolder = searchInFolder (gradleDist , distPath .toFile ());
144
153
if (distFolder != null && distFolder .exists ()) {
145
154
Path libPath = distFolder .toPath ().resolve ("lib" );
146
- return findLibFile (libPath .toFile ());
155
+ return findCoreAPI (libPath .toFile ());
147
156
}
148
157
return null ;
149
158
}
@@ -164,14 +173,15 @@ private File searchInFolder(String gradleDist, File folder) {
164
173
return null ;
165
174
}
166
175
167
- private File findLibFile (File folder ) {
176
+ private File findCoreAPI (File folder ) {
168
177
for (File file : folder .listFiles ()) {
169
178
String name = file .getName ();
170
179
if (name .startsWith ("gradle-core-api" ) && name .endsWith (".jar" )) {
171
180
return file ;
172
181
}
173
182
}
174
- // For Gradle version under 5.6, the name of library file is like gradle-core-${version}.jar
183
+ // For Gradle version under 5.6, the name of library file is like
184
+ // gradle-core-${version}.jar
175
185
for (File file : folder .listFiles ()) {
176
186
String name = file .getName ();
177
187
if (name .startsWith ("gradle-core" ) && name .endsWith (".jar" )) {
@@ -181,7 +191,7 @@ private File findLibFile(File folder) {
181
191
return null ;
182
192
}
183
193
184
- private File findPluginLibFile (File folder ) {
194
+ private File findPluginAPI (File folder ) {
185
195
for (File file : folder .listFiles ()) {
186
196
String name = file .getName ();
187
197
if (name .startsWith ("gradle-plugins" ) && name .endsWith (".jar" )) {
@@ -191,7 +201,7 @@ private File findPluginLibFile(File folder) {
191
201
return null ;
192
202
}
193
203
194
- private void getGradleLibraries (Path jarPath , JarFile jarFile ) {
204
+ private void loadClasses (Path jarPath , JarFile jarFile ) {
195
205
Enumeration <JarEntry > entries = jarFile .entries ();
196
206
while (entries .hasMoreElements ()) {
197
207
JarEntry entry = entries .nextElement ();
@@ -202,15 +212,15 @@ private void getGradleLibraries(Path jarPath, JarFile jarFile) {
202
212
try {
203
213
JavaClass javaClass = parser .parse ();
204
214
String className = javaClass .getClassName ();
205
- this .gradleLibraries .put (className , javaClass );
215
+ this .gradleClasses .put (className , javaClass );
206
216
} catch (IOException | ClassFormatException e ) {
207
217
// Do Nothing
208
218
}
209
219
}
210
220
}
211
221
212
- private void resolveJavaConfigurations () {
213
- JavaClass javaPluginClass = this .gradleLibraries .get (GradleLibraryResolver .JAVA_PLUGIN );
222
+ private void loadJavaConfigurations () {
223
+ JavaClass javaPluginClass = this .gradleClasses .get (GradleLibraryResolver .JAVA_PLUGIN );
214
224
if (javaPluginClass == null ) {
215
225
return ;
216
226
}
@@ -222,7 +232,8 @@ private void resolveJavaConfigurations() {
222
232
}
223
233
224
234
private static String removeQuotes (String original ) {
225
- // for those fields parsed from class files, we get ""values"", so we remove the starting and ending quotes here
235
+ // for those fields parsed from class files, we get ""values"", so we remove the
236
+ // starting and ending quotes here
226
237
if (original .length () < 3 ) {
227
238
return original ;
228
239
}
@@ -237,4 +248,8 @@ public boolean isJavaPluginsIncluded(Set<String> plugins) {
237
248
}
238
249
return false ;
239
250
}
251
+
252
+ private static boolean isValidFile (File file ) {
253
+ return file != null && file .exists ();
254
+ }
240
255
}
0 commit comments