2626import org .gradle .api .DefaultTask ;
2727import org .gradle .api .Project ;
2828import org .gradle .api .tasks .TaskAction ;
29+ import org .gradle .api .tasks .TaskContainer ;
2930import org .gradle .api .tasks .bundling .Jar ;
3031import org .springframework .boot .gradle .SpringBootPluginExtension ;
3132import org .springframework .boot .loader .tools .LibraryCallback ;
@@ -105,14 +106,13 @@ public File[] getDependencies() {
105106 final List <File > files = new ArrayList <File >();
106107 try {
107108 libraries .doWithLibraries (new LibraryCallback () {
108-
109109 @ Override
110110 public void library (File file , LibraryScope scope ) throws IOException {
111111 files .add (file );
112112 }
113113 });
114- } catch (IOException e ) {
115- throw new IllegalStateException ("Cannot retrieve dependencies" , e );
114+ } catch (IOException ex ) {
115+ throw new IllegalStateException ("Cannot retrieve dependencies" , ex );
116116 }
117117 return files .toArray (new File [files .size ()]);
118118 }
@@ -133,6 +133,9 @@ private ProjectLibraries getLibraries() {
133133 return libraries ;
134134 }
135135
136+ /**
137+ * Action to repackage JARs.
138+ */
136139 private class RepackageAction implements Action <Jar > {
137140
138141 private final SpringBootPluginExtension extension ;
@@ -146,50 +149,63 @@ public RepackageAction(SpringBootPluginExtension extension,
146149 }
147150
148151 @ Override
149- public void execute (Jar archive ) {
150- if (!enabled ) {
152+ public void execute (Jar jarTask ) {
153+ if (!RepackageTask . this . enabled ) {
151154 getLogger ().info ("Repackage disabled" );
152155 return ;
153156 }
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 )) {
159159 getLogger ().info (
160- "Jar task not repackaged (didn't match withJarTask): " + archive );
160+ "Jar task not repackaged (didn't match withJarTask): " + jarTask );
161161 return ;
162162 }
163-
164- if ("" .equals (archive .getClassifier ())
163+ if ("" .equals (jarTask .getClassifier ())
165164 || RepackageTask .this .withJarTask != null ) {
166- File file = archive .getArchivePath ();
165+ File file = jarTask .getArchivePath ();
167166 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 );
189168 }
190169 }
191170 }
192171
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+
193209 private void setMainClass (Repackager repackager ) {
194210 String mainClass = (String ) getProject ().property ("mainClassName" );
195211 if (RepackageTask .this .mainClass != null ) {
@@ -205,6 +221,9 @@ private void setMainClass(Repackager repackager) {
205221 }
206222 }
207223
224+ /**
225+ * {@link Repackager} that also logs when searching takes too long.
226+ */
208227 private class LoggingRepackager extends Repackager {
209228
210229 public LoggingRepackager (File source ) {
0 commit comments