11package org .gradle .playframework .plugins ;
22
33import org .gradle .api .*;
4- import org .gradle .api .artifacts .ArtifactCollection ;
54import org .gradle .api .artifacts .Configuration ;
65import org .gradle .api .artifacts .component .ComponentIdentifier ;
76import org .gradle .api .artifacts .component .ModuleComponentIdentifier ;
1110import org .gradle .api .distribution .DistributionContainer ;
1211import org .gradle .api .distribution .plugins .DistributionPlugin ;
1312import org .gradle .api .file .CopySpec ;
13+ import org .gradle .api .file .FileCollection ;
1414import org .gradle .api .file .FileCopyDetails ;
1515import org .gradle .api .provider .Provider ;
1616import org .gradle .api .provider .ProviderFactory ;
2727import org .gradle .util .GradleVersion ;
2828
2929import java .io .File ;
30- import java .util .Collections ;
31- import java .util .HashMap ;
32- import java .util .Map ;
33- import java .util .Set ;
30+ import java .util .*;
3431import java .util .concurrent .Callable ;
3532import java .util .function .Function ;
3633import java .util .stream .Collectors ;
@@ -83,6 +80,14 @@ private void createDistributionContentTasks(Project project, Distribution distri
8380 final String capitalizedDistName = capitalizeDistributionName (distribution .getName ());
8481 final String jarTaskName = "create" + capitalizedDistName + "DistributionJar" ;
8582
83+ Configuration runtimeClasspath = project .getConfigurations ().getByName (RUNTIME_CLASSPATH_CONFIGURATION_NAME );
84+ Provider <Set <ResolvedArtifactResult >> artifactsProvider = runtimeClasspath .getIncoming ().getArtifacts ().getResolvedArtifacts ();
85+ Provider <Set <ComponentIdAndFile >> componentAndFileProvider = artifactsProvider .map (resultSet ->
86+ resultSet .stream ()
87+ .map (result -> new ComponentIdAndFile (result .getId ().getComponentIdentifier (), result .getFile ()))
88+ .collect (Collectors .toSet ())
89+ );
90+
8691 TaskProvider <Jar > distributionJarTask = project .getTasks ().register (jarTaskName , Jar .class , jar -> {
8792 jar .setDescription ("Assembles an application jar suitable for deployment." );
8893 jar .dependsOn (mainJarTask , assetsJarTask );
@@ -91,7 +96,7 @@ private void createDistributionContentTasks(Project project, Distribution distri
9196 jar .getArchiveBaseName ().convention (mainJarTask .flatMap (AbstractArchiveTask ::getArchiveBaseName ));
9297
9398 Map <String , Object > classpath = new HashMap <>();
94- classpath .put ("Class-Path" , new PlayManifestClasspath (project . getConfigurations (). getByName ( RUNTIME_CLASSPATH_CONFIGURATION_NAME ) , assetsJarTask .get ().getArchiveFile ().get ().getAsFile ()));
99+ classpath .put ("Class-Path" , new PlayManifestClasspath (runtimeClasspath , componentAndFileProvider , assetsJarTask .get ().getArchiveFile ().get ().getAsFile ()));
95100 jar .getManifest ().attributes (classpath );
96101 });
97102
@@ -114,7 +119,7 @@ private void createDistributionContentTasks(Project project, Distribution distri
114119 copySpec .from (distributionJarTask );
115120 copySpec .from (assetsJarTask .flatMap (AbstractArchiveTask ::getArchiveFile ));
116121 copySpec .from (project .getConfigurations ().getByName (RUNTIME_CLASSPATH_CONFIGURATION_NAME ));
117- copySpec .eachFile (new PrefixArtifactFileNames (project . getConfigurations (). getByName ( RUNTIME_CLASSPATH_CONFIGURATION_NAME ) ));
122+ copySpec .eachFile (new PrefixArtifactFileNames (componentAndFileProvider ));
118123 });
119124
120125 distSpec .into ("bin" , copySpec -> {
@@ -219,30 +224,32 @@ private String capitalizeDistributionName(String distributionName) {
219224 * Represents a classpath to be defined in a jar manifest
220225 */
221226 static class PlayManifestClasspath {
222- final Configuration configuration ;
223227 final File assetsJarFile ;
228+ private final Provider <Set <ComponentIdAndFile >> componentAndFileProvider ;
229+ private final FileCollection configuration ;
224230
225- public PlayManifestClasspath (Configuration configuration , File assetsJarFile ) {
231+ public PlayManifestClasspath (FileCollection configuration , Provider <Set <ComponentIdAndFile >> componentAndFileProvider , File assetsJarFile ) {
232+ this .componentAndFileProvider = componentAndFileProvider ;
226233 this .configuration = configuration ;
227234 this .assetsJarFile = assetsJarFile ;
228235 }
229236
230237 @ Override
231238 public String toString () {
232239 Stream <File > allFiles = Stream .concat (configuration .getFiles ().stream (), Collections .singleton (assetsJarFile ).stream ());
233- Stream <String > transformedFiles = allFiles .map (new PrefixArtifactFileNames (configuration ));
240+ Stream <String > transformedFiles = allFiles .map (new PrefixArtifactFileNames (componentAndFileProvider ));
234241 return String .join (" " ,
235242 transformedFiles .collect (Collectors .toList ())
236243 );
237244 }
238245 }
239246
240247 static class PrefixArtifactFileNames implements Action <FileCopyDetails >, Function <File , String > {
241- private final Configuration configuration ;
248+ private final Provider < Set < ComponentIdAndFile >> componentAndFileProvider ;
242249 Map <File , String > renames ;
243250
244- PrefixArtifactFileNames (Configuration configuration ) {
245- this .configuration = configuration ;
251+ PrefixArtifactFileNames (Provider < Set < ComponentIdAndFile >> componentAndFileProvider ) {
252+ this .componentAndFileProvider = componentAndFileProvider ;
246253 }
247254
248255 @ Override
@@ -268,26 +275,25 @@ private void calculateRenames() {
268275
269276 private Map <File , String > calculate () {
270277 Map <File , String > files = new HashMap <>();
271- for (ResolvedArtifactResult artifact : getResolvedArtifacts ()) {
272- ComponentIdentifier componentId = artifact .getId (). getComponentIdentifier ();
278+ for (ComponentIdAndFile artifact : getResolvedArtifacts ()) {
279+ ComponentIdentifier componentId = artifact .getComponentId ();
273280 if (componentId instanceof ProjectComponentIdentifier ) {
274281 // rename project dependencies
275282 ProjectComponentIdentifier projectComponentIdentifier = (ProjectComponentIdentifier ) componentId ;
276- files .put (artifact .getFile (), renameForProject (projectComponentIdentifier , artifact .getFile ()));
283+ files .put (artifact .getArtifactFile (), renameForProject (projectComponentIdentifier , artifact .getArtifactFile ()));
277284 } else if (componentId instanceof ModuleComponentIdentifier ) {
278285 ModuleComponentIdentifier moduleComponentIdentifier = (ModuleComponentIdentifier ) componentId ;
279- files .put (artifact .getFile (), renameForModule (moduleComponentIdentifier , artifact .getFile ()));
286+ files .put (artifact .getArtifactFile (), renameForModule (moduleComponentIdentifier , artifact .getArtifactFile ()));
280287 } else {
281288 // don't rename other types of dependencies
282- files .put (artifact .getFile (), artifact .getFile ().getName ());
289+ files .put (artifact .getArtifactFile (), artifact .getArtifactFile ().getName ());
283290 }
284291 }
285292 return Collections .unmodifiableMap (files );
286293 }
287294
288- Set <ResolvedArtifactResult > getResolvedArtifacts () {
289- ArtifactCollection artifacts = configuration .getIncoming ().getArtifacts ();
290- return artifacts .getArtifacts ();
295+ Set <ComponentIdAndFile > getResolvedArtifacts () {
296+ return componentAndFileProvider .get ();
291297 }
292298
293299 static String renameForProject (ProjectComponentIdentifier id , File file ) {
@@ -330,3 +336,34 @@ private static boolean hasExtension(File file, String extension) {
330336 return file .getPath ().endsWith (extension );
331337 }
332338}
339+
340+ class ComponentIdAndFile {
341+ private final ComponentIdentifier componentId ;
342+ private final File artifactFile ;
343+
344+
345+ ComponentIdAndFile (ComponentIdentifier componentId , File artifactFile ) {
346+ this .componentId = componentId ;
347+ this .artifactFile = artifactFile ;
348+ }
349+
350+ public ComponentIdentifier getComponentId () {
351+ return componentId ;
352+ }
353+
354+ public File getArtifactFile () {
355+ return artifactFile ;
356+ }
357+
358+ @ Override
359+ public boolean equals (Object o ) {
360+ if (o == null || getClass () != o .getClass ()) return false ;
361+ ComponentIdAndFile componentIdAndFile = (ComponentIdAndFile ) o ;
362+ return Objects .equals (componentId , componentIdAndFile .componentId ) && Objects .equals (artifactFile , componentIdAndFile .artifactFile );
363+ }
364+
365+ @ Override
366+ public int hashCode () {
367+ return Objects .hash (componentId , artifactFile );
368+ }
369+ }
0 commit comments