26
26
import org .gradle .api .DefaultTask ;
27
27
import org .gradle .api .Project ;
28
28
import org .gradle .api .tasks .TaskAction ;
29
+ import org .gradle .api .tasks .TaskContainer ;
29
30
import org .gradle .api .tasks .bundling .Jar ;
30
31
import org .springframework .boot .gradle .SpringBootPluginExtension ;
31
32
import org .springframework .boot .loader .tools .LibraryCallback ;
@@ -105,14 +106,13 @@ public File[] getDependencies() {
105
106
final List <File > files = new ArrayList <File >();
106
107
try {
107
108
libraries .doWithLibraries (new LibraryCallback () {
108
-
109
109
@ Override
110
110
public void library (File file , LibraryScope scope ) throws IOException {
111
111
files .add (file );
112
112
}
113
113
});
114
- } catch (IOException e ) {
115
- throw new IllegalStateException ("Cannot retrieve dependencies" , e );
114
+ } catch (IOException ex ) {
115
+ throw new IllegalStateException ("Cannot retrieve dependencies" , ex );
116
116
}
117
117
return files .toArray (new File [files .size ()]);
118
118
}
@@ -133,6 +133,9 @@ private ProjectLibraries getLibraries() {
133
133
return libraries ;
134
134
}
135
135
136
+ /**
137
+ * Action to repackage JARs.
138
+ */
136
139
private class RepackageAction implements Action <Jar > {
137
140
138
141
private final SpringBootPluginExtension extension ;
@@ -146,50 +149,63 @@ public RepackageAction(SpringBootPluginExtension extension,
146
149
}
147
150
148
151
@ Override
149
- public void execute (Jar archive ) {
150
- if (!enabled ) {
152
+ public void execute (Jar jarTask ) {
153
+ if (!RepackageTask . this . enabled ) {
151
154
getLogger ().info ("Repackage disabled" );
152
155
return ;
153
156
}
154
- // if withJarTask is set, compare tasks and bail out if we didn't match
155
- if (RepackageTask .this .withJarTask != null
156
- && !(archive .equals (RepackageTask .this .withJarTask )
157
- || archive .equals (getProject ().getTasks ().findByName (
158
- RepackageTask .this .withJarTask .toString ())))) {
157
+ Object withJarTask = RepackageTask .this .withJarTask ;
158
+ if (isTaskMatch (jarTask , withJarTask )) {
159
159
getLogger ().info (
160
- "Jar task not repackaged (didn't match withJarTask): " + archive );
160
+ "Jar task not repackaged (didn't match withJarTask): " + jarTask );
161
161
return ;
162
162
}
163
-
164
- if ("" .equals (archive .getClassifier ())
163
+ if ("" .equals (jarTask .getClassifier ())
165
164
|| RepackageTask .this .withJarTask != null ) {
166
- File file = archive .getArchivePath ();
165
+ File file = jarTask .getArchivePath ();
167
166
if (file .exists ()) {
168
- Repackager repackager = new LoggingRepackager (file );
169
- File out = RepackageTask .this .outputFile ;
170
- if (out != null && !file .equals (out )) {
171
- try {
172
- FileCopyUtils .copy (file , out );
173
- } catch (IOException ex ) {
174
- throw new IllegalStateException (ex .getMessage (), ex );
175
- }
176
- file = out ;
177
- }
178
- RepackageTask .this .outputFile = file ;
179
- setMainClass (repackager );
180
- if (this .extension .convertLayout () != null ) {
181
- repackager .setLayout (this .extension .convertLayout ());
182
- }
183
- repackager .setBackupSource (this .extension .isBackupSource ());
184
- try {
185
- repackager .repackage (file , this .libraries );
186
- } catch (IOException ex ) {
187
- throw new IllegalStateException (ex .getMessage (), ex );
188
- }
167
+ repackage (file );
189
168
}
190
169
}
191
170
}
192
171
172
+ private boolean isTaskMatch (Jar task , Object compare ) {
173
+ if (compare == null ) {
174
+ return false ;
175
+ }
176
+ TaskContainer tasks = getProject ().getTasks ();
177
+ return task .equals (compare ) || task .equals (tasks .findByName (task .toString ()));
178
+ }
179
+
180
+ private void repackage (File file ) {
181
+ File outputFile = RepackageTask .this .outputFile ;
182
+ if (outputFile != null && !file .equals (outputFile )) {
183
+ copy (file , outputFile );
184
+ file = outputFile ;
185
+ }
186
+ Repackager repackager = new LoggingRepackager (file );
187
+ setMainClass (repackager );
188
+ if (this .extension .convertLayout () != null ) {
189
+ repackager .setLayout (this .extension .convertLayout ());
190
+ }
191
+ repackager .setBackupSource (this .extension .isBackupSource ());
192
+ try {
193
+ repackager .repackage (file , this .libraries );
194
+ }
195
+ catch (IOException ex ) {
196
+ throw new IllegalStateException (ex .getMessage (), ex );
197
+ }
198
+ }
199
+
200
+ private void copy (File source , File dest ) {
201
+ try {
202
+ FileCopyUtils .copy (source , dest );
203
+ }
204
+ catch (IOException ex ) {
205
+ throw new IllegalStateException (ex .getMessage (), ex );
206
+ }
207
+ }
208
+
193
209
private void setMainClass (Repackager repackager ) {
194
210
String mainClass = (String ) getProject ().property ("mainClassName" );
195
211
if (RepackageTask .this .mainClass != null ) {
@@ -205,6 +221,9 @@ private void setMainClass(Repackager repackager) {
205
221
}
206
222
}
207
223
224
+ /**
225
+ * {@link Repackager} that also logs when searching takes too long.
226
+ */
208
227
private class LoggingRepackager extends Repackager {
209
228
210
229
public LoggingRepackager (File source ) {
0 commit comments