3030import java .util .zip .ZipEntry ;
3131
3232import org .gradle .api .Action ;
33- import org .gradle .api .artifacts .ArtifactCollection ;
3433import 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 ;
3638import org .gradle .api .artifacts .component .ComponentArtifactIdentifier ;
3739import org .gradle .api .artifacts .component .ModuleComponentIdentifier ;
38- import org .gradle .api .artifacts .result . ResolvedArtifactResult ;
40+ import org .gradle .api .artifacts .component . ProjectComponentIdentifier ;
3941import org .junit .jupiter .api .Test ;
4042
4143import org .springframework .boot .gradle .tasks .bundling .BootJarTests .TestBootJar ;
5153 * @author Andy Wilkinson
5254 * @author Madhura Bhave
5355 * @author Scott Frederick
56+ * @author Paddy Drury
5457 */
5558class BootJarTests extends AbstractBootArchiveTests <TestBootJar > {
5659
@@ -99,22 +102,26 @@ void whenJarIsLayeredThenLayersIndexIsPresentAndCorrect() throws IOException {
99102 try (JarFile jarFile = new JarFile (createLayeredJar ())) {
100103 List <String > entryNames = getEntryNames (jarFile );
101104 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" );
104109 List <String > index = entryLines (jarFile , "BOOT-INF/layers.idx" );
105110 assertThat (getLayerNames (index )).containsExactly ("dependencies" , "spring-boot-loader" ,
106111 "snapshot-dependencies" , "application" );
107112 String layerToolsJar = "BOOT-INF/lib/" + JarModeLibrary .LAYER_TOOLS .getName ();
108113 List <String > expected = new ArrayList <>();
109114 expected .add ("- \" dependencies\" :" );
110115 expected .add (" - \" BOOT-INF/lib/first-library.jar\" " );
116+ expected .add (" - \" BOOT-INF/lib/first-project-library.jar\" " );
111117 expected .add (" - \" BOOT-INF/lib/second-library.jar\" " );
112118 if (!layerToolsJar .contains ("SNAPSHOT" )) {
113119 expected .add (" - \" " + layerToolsJar + "\" " );
114120 }
115121 expected .add ("- \" spring-boot-loader\" :" );
116122 expected .add (" - \" org/\" " );
117123 expected .add ("- \" snapshot-dependencies\" :" );
124+ expected .add (" - \" BOOT-INF/lib/second-project-library-SNAPSHOT.jar\" " );
118125 if (layerToolsJar .contains ("SNAPSHOT" )) {
119126 expected .add (" - \" " + layerToolsJar + "\" " );
120127 }
@@ -145,8 +152,10 @@ void whenJarIsLayeredWithCustomStrategiesThenLayersIndexIsPresentAndCorrect() th
145152 try (JarFile jarFile = new JarFile (jar )) {
146153 List <String > entryNames = getEntryNames (jar );
147154 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" );
150159 List <String > index = entryLines (jarFile , "BOOT-INF/layers.idx" );
151160 assertThat (getLayerNames (index )).containsExactly ("my-deps" , "my-internal-deps" , "my-snapshot-deps" ,
152161 "resources" , "application" );
@@ -156,8 +165,10 @@ void whenJarIsLayeredWithCustomStrategiesThenLayersIndexIsPresentAndCorrect() th
156165 expected .add (" - \" " + layerToolsJar + "\" " );
157166 expected .add ("- \" my-internal-deps\" :" );
158167 expected .add (" - \" BOOT-INF/lib/first-library.jar\" " );
168+ expected .add (" - \" BOOT-INF/lib/first-project-library.jar\" " );
159169 expected .add (" - \" BOOT-INF/lib/second-library.jar\" " );
160170 expected .add ("- \" my-snapshot-deps\" :" );
171+ expected .add (" - \" BOOT-INF/lib/second-project-library-SNAPSHOT.jar\" " );
161172 expected .add (" - \" BOOT-INF/lib/third-library-SNAPSHOT.jar\" " );
162173 expected .add ("- \" resources\" :" );
163174 expected .add (" - \" BOOT-INF/classes/static/\" " );
@@ -179,14 +190,19 @@ void jarsInLibAreStored() throws IOException {
179190 assertThat (jarFile .getEntry ("BOOT-INF/lib/second-library.jar" ).getMethod ()).isEqualTo (ZipEntry .STORED );
180191 assertThat (jarFile .getEntry ("BOOT-INF/lib/third-library-SNAPSHOT.jar" ).getMethod ())
181192 .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 );
182197 }
183198 }
184199
185200 @ Test
186201 void whenJarIsLayeredClasspathIndexPointsToLayeredLibs () throws IOException {
187202 try (JarFile jarFile = new JarFile (createLayeredJar ())) {
188203 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\" " );
190206 }
191207 }
192208
@@ -209,7 +225,8 @@ void classpathIndexPointsToBootInfLibs() throws IOException {
209225 assertThat (jarFile .getManifest ().getMainAttributes ().getValue ("Spring-Boot-Classpath-Index" ))
210226 .isEqualTo ("BOOT-INF/classpath.idx" );
211227 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\" " );
213230 }
214231 }
215232
@@ -251,34 +268,55 @@ private void addContent() throws IOException {
251268 File css = new File (staticResources , "test.css" );
252269 css .createNewFile ();
253270 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 <>();
256274 artifacts .add (mockLibraryArtifact ("first-library.jar" , "com.example" , "first-library" , "1.0.0" ));
257275 artifacts .add (mockLibraryArtifact ("second-library.jar" , "com.example" , "second-library" , "1.0.0" ));
258276 artifacts .add (
259277 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 );
264284 Configuration configuration = mock (Configuration .class );
265285 given (configuration .isCanBeResolved ()).willReturn (true );
266- given (configuration .getIncoming ()).willReturn (resolvableDependencies );
286+ given (configuration .getResolvedConfiguration ()).willReturn (resolvedConfiguration );
267287 bootJar .setConfiguration (Collections .singleton (configuration ));
268288 }
269289
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 );
275292 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 );
278316 File file = new File (this .temp , fileName ).getAbsoluteFile ();
279317 System .out .println (file );
280318 given (libraryArtifact .getFile ()).willReturn (file );
281- given (libraryArtifact .getId ()).willReturn (libraryArtifactId );
319+ given (libraryArtifact .getModuleVersion ()).willReturn (moduleVersion );
282320 return libraryArtifact ;
283321 }
284322
0 commit comments