30
30
import java .util .zip .ZipEntry ;
31
31
32
32
import org .gradle .api .Action ;
33
- import org .gradle .api .artifacts .ArtifactCollection ;
34
33
import org .gradle .api .artifacts .Configuration ;
35
- import org .gradle .api .artifacts .ResolvableDependencies ;
34
+ import org .gradle .api .artifacts .ModuleVersionIdentifier ;
35
+ import org .gradle .api .artifacts .ResolvedArtifact ;
36
+ import org .gradle .api .artifacts .ResolvedConfiguration ;
37
+ import org .gradle .api .artifacts .ResolvedModuleVersion ;
36
38
import org .gradle .api .artifacts .component .ComponentArtifactIdentifier ;
37
39
import org .gradle .api .artifacts .component .ModuleComponentIdentifier ;
38
- import org .gradle .api .artifacts .result . ResolvedArtifactResult ;
40
+ import org .gradle .api .artifacts .component . ProjectComponentIdentifier ;
39
41
import org .junit .jupiter .api .Test ;
40
42
41
43
import org .springframework .boot .gradle .tasks .bundling .BootJarTests .TestBootJar ;
51
53
* @author Andy Wilkinson
52
54
* @author Madhura Bhave
53
55
* @author Scott Frederick
56
+ * @author Paddy Drury
54
57
*/
55
58
class BootJarTests extends AbstractBootArchiveTests <TestBootJar > {
56
59
@@ -99,22 +102,26 @@ void whenJarIsLayeredThenLayersIndexIsPresentAndCorrect() throws IOException {
99
102
try (JarFile jarFile = new JarFile (createLayeredJar ())) {
100
103
List <String > entryNames = getEntryNames (jarFile );
101
104
assertThat (entryNames ).contains ("BOOT-INF/lib/first-library.jar" , "BOOT-INF/lib/second-library.jar" ,
102
- "BOOT-INF/lib/third-library-SNAPSHOT.jar" , "BOOT-INF/classes/com/example/Application.class" ,
103
- "BOOT-INF/classes/application.properties" , "BOOT-INF/classes/static/test.css" );
105
+ "BOOT-INF/lib/third-library-SNAPSHOT.jar" , "BOOT-INF/lib/first-project-library.jar" ,
106
+ "BOOT-INF/lib/second-project-library-SNAPSHOT.jar" ,
107
+ "BOOT-INF/classes/com/example/Application.class" , "BOOT-INF/classes/application.properties" ,
108
+ "BOOT-INF/classes/static/test.css" );
104
109
List <String > index = entryLines (jarFile , "BOOT-INF/layers.idx" );
105
110
assertThat (getLayerNames (index )).containsExactly ("dependencies" , "spring-boot-loader" ,
106
111
"snapshot-dependencies" , "application" );
107
112
String layerToolsJar = "BOOT-INF/lib/" + JarModeLibrary .LAYER_TOOLS .getName ();
108
113
List <String > expected = new ArrayList <>();
109
114
expected .add ("- \" dependencies\" :" );
110
115
expected .add (" - \" BOOT-INF/lib/first-library.jar\" " );
116
+ expected .add (" - \" BOOT-INF/lib/first-project-library.jar\" " );
111
117
expected .add (" - \" BOOT-INF/lib/second-library.jar\" " );
112
118
if (!layerToolsJar .contains ("SNAPSHOT" )) {
113
119
expected .add (" - \" " + layerToolsJar + "\" " );
114
120
}
115
121
expected .add ("- \" spring-boot-loader\" :" );
116
122
expected .add (" - \" org/\" " );
117
123
expected .add ("- \" snapshot-dependencies\" :" );
124
+ expected .add (" - \" BOOT-INF/lib/second-project-library-SNAPSHOT.jar\" " );
118
125
if (layerToolsJar .contains ("SNAPSHOT" )) {
119
126
expected .add (" - \" " + layerToolsJar + "\" " );
120
127
}
@@ -145,8 +152,10 @@ void whenJarIsLayeredWithCustomStrategiesThenLayersIndexIsPresentAndCorrect() th
145
152
try (JarFile jarFile = new JarFile (jar )) {
146
153
List <String > entryNames = getEntryNames (jar );
147
154
assertThat (entryNames ).contains ("BOOT-INF/lib/first-library.jar" , "BOOT-INF/lib/second-library.jar" ,
148
- "BOOT-INF/lib/third-library-SNAPSHOT.jar" , "BOOT-INF/classes/com/example/Application.class" ,
149
- "BOOT-INF/classes/application.properties" , "BOOT-INF/classes/static/test.css" );
155
+ "BOOT-INF/lib/third-library-SNAPSHOT.jar" , "BOOT-INF/lib/first-project-library.jar" ,
156
+ "BOOT-INF/lib/second-project-library-SNAPSHOT.jar" ,
157
+ "BOOT-INF/classes/com/example/Application.class" , "BOOT-INF/classes/application.properties" ,
158
+ "BOOT-INF/classes/static/test.css" );
150
159
List <String > index = entryLines (jarFile , "BOOT-INF/layers.idx" );
151
160
assertThat (getLayerNames (index )).containsExactly ("my-deps" , "my-internal-deps" , "my-snapshot-deps" ,
152
161
"resources" , "application" );
@@ -156,8 +165,10 @@ void whenJarIsLayeredWithCustomStrategiesThenLayersIndexIsPresentAndCorrect() th
156
165
expected .add (" - \" " + layerToolsJar + "\" " );
157
166
expected .add ("- \" my-internal-deps\" :" );
158
167
expected .add (" - \" BOOT-INF/lib/first-library.jar\" " );
168
+ expected .add (" - \" BOOT-INF/lib/first-project-library.jar\" " );
159
169
expected .add (" - \" BOOT-INF/lib/second-library.jar\" " );
160
170
expected .add ("- \" my-snapshot-deps\" :" );
171
+ expected .add (" - \" BOOT-INF/lib/second-project-library-SNAPSHOT.jar\" " );
161
172
expected .add (" - \" BOOT-INF/lib/third-library-SNAPSHOT.jar\" " );
162
173
expected .add ("- \" resources\" :" );
163
174
expected .add (" - \" BOOT-INF/classes/static/\" " );
@@ -179,14 +190,19 @@ void jarsInLibAreStored() throws IOException {
179
190
assertThat (jarFile .getEntry ("BOOT-INF/lib/second-library.jar" ).getMethod ()).isEqualTo (ZipEntry .STORED );
180
191
assertThat (jarFile .getEntry ("BOOT-INF/lib/third-library-SNAPSHOT.jar" ).getMethod ())
181
192
.isEqualTo (ZipEntry .STORED );
193
+ assertThat (jarFile .getEntry ("BOOT-INF/lib/first-project-library.jar" ).getMethod ())
194
+ .isEqualTo (ZipEntry .STORED );
195
+ assertThat (jarFile .getEntry ("BOOT-INF/lib/second-project-library-SNAPSHOT.jar" ).getMethod ())
196
+ .isEqualTo (ZipEntry .STORED );
182
197
}
183
198
}
184
199
185
200
@ Test
186
201
void whenJarIsLayeredClasspathIndexPointsToLayeredLibs () throws IOException {
187
202
try (JarFile jarFile = new JarFile (createLayeredJar ())) {
188
203
assertThat (entryLines (jarFile , "BOOT-INF/classpath.idx" )).containsExactly ("- \" first-library.jar\" " ,
189
- "- \" second-library.jar\" " , "- \" third-library-SNAPSHOT.jar\" " );
204
+ "- \" second-library.jar\" " , "- \" third-library-SNAPSHOT.jar\" " , "- \" first-project-library.jar\" " ,
205
+ "- \" second-project-library-SNAPSHOT.jar\" " );
190
206
}
191
207
}
192
208
@@ -209,7 +225,8 @@ void classpathIndexPointsToBootInfLibs() throws IOException {
209
225
assertThat (jarFile .getManifest ().getMainAttributes ().getValue ("Spring-Boot-Classpath-Index" ))
210
226
.isEqualTo ("BOOT-INF/classpath.idx" );
211
227
assertThat (entryLines (jarFile , "BOOT-INF/classpath.idx" )).containsExactly ("- \" first-library.jar\" " ,
212
- "- \" second-library.jar\" " , "- \" third-library-SNAPSHOT.jar\" " );
228
+ "- \" second-library.jar\" " , "- \" third-library-SNAPSHOT.jar\" " , "- \" first-project-library.jar\" " ,
229
+ "- \" second-project-library-SNAPSHOT.jar\" " );
213
230
}
214
231
}
215
232
@@ -251,34 +268,55 @@ private void addContent() throws IOException {
251
268
File css = new File (staticResources , "test.css" );
252
269
css .createNewFile ();
253
270
bootJar .classpath (classesJavaMain , resourcesMain , jarFile ("first-library.jar" ), jarFile ("second-library.jar" ),
254
- jarFile ("third-library-SNAPSHOT.jar" ));
255
- Set <ResolvedArtifactResult > artifacts = new LinkedHashSet <>();
271
+ jarFile ("third-library-SNAPSHOT.jar" ), jarFile ("first-project-library.jar" ),
272
+ jarFile ("second-project-library-SNAPSHOT.jar" ));
273
+ Set <ResolvedArtifact > artifacts = new LinkedHashSet <>();
256
274
artifacts .add (mockLibraryArtifact ("first-library.jar" , "com.example" , "first-library" , "1.0.0" ));
257
275
artifacts .add (mockLibraryArtifact ("second-library.jar" , "com.example" , "second-library" , "1.0.0" ));
258
276
artifacts .add (
259
277
mockLibraryArtifact ("third-library-SNAPSHOT.jar" , "com.example" , "third-library" , "1.0.0.SNAPSHOT" ));
260
- ArtifactCollection resolvedDependencies = mock (ArtifactCollection .class );
261
- given (resolvedDependencies .getArtifacts ()).willReturn (artifacts );
262
- ResolvableDependencies resolvableDependencies = mock (ResolvableDependencies .class );
263
- given (resolvableDependencies .getArtifacts ()).willReturn (resolvedDependencies );
278
+ artifacts
279
+ .add (mockProjectArtifact ("first-project-library.jar" , "com.example" , "first-project-library" , "1.0.0" ));
280
+ artifacts .add (mockProjectArtifact ("second-project-library-SNAPSHOT.jar" , "com.example" ,
281
+ "second-project-library" , "1.0.0.SNAPSHOT" ));
282
+ ResolvedConfiguration resolvedConfiguration = mock (ResolvedConfiguration .class );
283
+ given (resolvedConfiguration .getResolvedArtifacts ()).willReturn (artifacts );
264
284
Configuration configuration = mock (Configuration .class );
265
285
given (configuration .isCanBeResolved ()).willReturn (true );
266
- given (configuration .getIncoming ()).willReturn (resolvableDependencies );
286
+ given (configuration .getResolvedConfiguration ()).willReturn (resolvedConfiguration );
267
287
bootJar .setConfiguration (Collections .singleton (configuration ));
268
288
}
269
289
270
- private ResolvedArtifactResult mockLibraryArtifact (String fileName , String group , String module , String version ) {
271
- ModuleComponentIdentifier identifier = mock (ModuleComponentIdentifier .class );
272
- given (identifier .getGroup ()).willReturn (group );
273
- given (identifier .getModule ()).willReturn (module );
274
- given (identifier .getVersion ()).willReturn (version );
290
+ private ResolvedArtifact mockLibraryArtifact (String fileName , String group , String module , String version ) {
291
+ ModuleComponentIdentifier moduleComponentIdentifier = mock (ModuleComponentIdentifier .class );
275
292
ComponentArtifactIdentifier libraryArtifactId = mock (ComponentArtifactIdentifier .class );
276
- given (libraryArtifactId .getComponentIdentifier ()).willReturn (identifier );
277
- ResolvedArtifactResult libraryArtifact = mock (ResolvedArtifactResult .class );
293
+ given (libraryArtifactId .getComponentIdentifier ()).willReturn (moduleComponentIdentifier );
294
+ ResolvedArtifact libraryArtifact = mockArtifact (fileName , group , module , version );
295
+ given (libraryArtifact .getId ()).willReturn (libraryArtifactId );
296
+ return libraryArtifact ;
297
+ }
298
+
299
+ private ResolvedArtifact mockProjectArtifact (String fileName , String group , String module , String version ) {
300
+ ProjectComponentIdentifier projectComponentIdentifier = mock (ProjectComponentIdentifier .class );
301
+ ComponentArtifactIdentifier projectArtifactId = mock (ComponentArtifactIdentifier .class );
302
+ given (projectArtifactId .getComponentIdentifier ()).willReturn (projectComponentIdentifier );
303
+ ResolvedArtifact projectArtifact = mockArtifact (fileName , group , module , version );
304
+ given (projectArtifact .getId ()).willReturn (projectArtifactId );
305
+ return projectArtifact ;
306
+ }
307
+
308
+ private ResolvedArtifact mockArtifact (String fileName , String group , String module , String version ) {
309
+ ModuleVersionIdentifier moduleVersionIdentifier = mock (ModuleVersionIdentifier .class );
310
+ given (moduleVersionIdentifier .getGroup ()).willReturn (group );
311
+ given (moduleVersionIdentifier .getName ()).willReturn (module );
312
+ given (moduleVersionIdentifier .getVersion ()).willReturn (version );
313
+ ResolvedModuleVersion moduleVersion = mock (ResolvedModuleVersion .class );
314
+ given (moduleVersion .getId ()).willReturn (moduleVersionIdentifier );
315
+ ResolvedArtifact libraryArtifact = mock (ResolvedArtifact .class );
278
316
File file = new File (this .temp , fileName ).getAbsoluteFile ();
279
317
System .out .println (file );
280
318
given (libraryArtifact .getFile ()).willReturn (file );
281
- given (libraryArtifact .getId ()).willReturn (libraryArtifactId );
319
+ given (libraryArtifact .getModuleVersion ()).willReturn (moduleVersion );
282
320
return libraryArtifact ;
283
321
}
284
322
0 commit comments