1616
1717package org .gradlex .jvm .dependency .conflict .resolution ;
1818
19+ import org .gradle .api .NamedDomainObjectProvider ;
1920import org .gradle .api .artifacts .Configuration ;
2021import org .gradle .api .artifacts .ConfigurationContainer ;
2122import org .gradle .api .artifacts .Dependency ;
@@ -56,8 +57,8 @@ public ConsistentResolution(SourceSetContainer sourceSets) {
5657 /**
5758 * The runtime classpath of the given project always respected in version conflict detection and resolution.
5859 */
59- public Configuration providesVersions (String versionProvidingProject ) {
60- Configuration mainRuntimeClasspath = maybeCreateMainRuntimeClasspathConfiguration ();
60+ public NamedDomainObjectProvider < Configuration > providesVersions (String versionProvidingProject ) {
61+ NamedDomainObjectProvider < Configuration > mainRuntimeClasspath = maybeCreateMainRuntimeClasspathConfiguration ();
6162 getDependencies ().add (mainRuntimeClasspath .getName (), createDependency (versionProvidingProject ));
6263 return mainRuntimeClasspath ;
6364 }
@@ -68,31 +69,30 @@ public Configuration providesVersions(String versionProvidingProject) {
6869 * Useful if additional dependencies are needed only for tests.
6970 */
7071 public void platform (String platform ) {
71- Configuration internal = maybeCreateInternalConfiguration ();
72- internal .withDependencies (d -> {
72+ NamedDomainObjectProvider < Configuration > internal = maybeCreateInternalConfiguration ();
73+ internal .configure ( conf -> conf . withDependencies (d -> {
7374 Dependency platformDependency = getDependencies ().platform (createDependency (platform ));
7475 d .add (platformDependency );
75- });
76+ })) ;
7677
7778 sourceSets .configureEach (sourceSet -> {
7879 ConfigurationContainer configurations = getConfigurations ();
79- configurations .getByName (sourceSet .getRuntimeClasspathConfigurationName ()) .extendsFrom (internal );
80- configurations .getByName (sourceSet .getCompileClasspathConfigurationName ()) .extendsFrom (internal );
81- configurations .getByName (sourceSet .getAnnotationProcessorConfigurationName ()) .extendsFrom (internal );
80+ configurations .named (sourceSet .getRuntimeClasspathConfigurationName (), c -> c .extendsFrom (internal . get ()) );
81+ configurations .named (sourceSet .getCompileClasspathConfigurationName (), c -> c .extendsFrom (internal . get ()) );
82+ configurations .named (sourceSet .getAnnotationProcessorConfigurationName (), c -> c .extendsFrom (internal . get ()) );
8283 });
8384 }
8485
85- private Configuration maybeCreateMainRuntimeClasspathConfiguration () {
86- Configuration existing = getConfigurations ().findByName (MAIN_RUNTIME_CLASSPATH_CONFIGURATION_NAME );
87- if (existing != null ) {
88- return existing ;
86+ private NamedDomainObjectProvider <Configuration > maybeCreateMainRuntimeClasspathConfiguration () {
87+ if (getConfigurations ().getNames ().contains (MAIN_RUNTIME_CLASSPATH_CONFIGURATION_NAME )) {
88+ return getConfigurations ().named (MAIN_RUNTIME_CLASSPATH_CONFIGURATION_NAME );
8989 }
9090
91- Configuration mainRuntimeClasspath = getConfigurations ().create (MAIN_RUNTIME_CLASSPATH_CONFIGURATION_NAME , c -> {
91+ NamedDomainObjectProvider < Configuration > mainRuntimeClasspath = getConfigurations ().register (MAIN_RUNTIME_CLASSPATH_CONFIGURATION_NAME , c -> {
9292 ObjectFactory objects = getObjects ();
9393 c .setCanBeResolved (true );
9494 c .setCanBeConsumed (false );
95- c .extendsFrom (maybeCreateInternalConfiguration ());
95+ c .extendsFrom (maybeCreateInternalConfiguration (). get () );
9696 c .getAttributes ().attribute (Usage .USAGE_ATTRIBUTE , objects .named (Usage .class , Usage .JAVA_RUNTIME ));
9797 c .getAttributes ().attribute (Category .CATEGORY_ATTRIBUTE , objects .named (Category .class , Category .LIBRARY ));
9898 c .getAttributes ().attribute (LibraryElements .LIBRARY_ELEMENTS_ATTRIBUTE , objects .named (LibraryElements .class , LibraryElements .JAR ));
@@ -104,22 +104,21 @@ private Configuration maybeCreateMainRuntimeClasspathConfiguration() {
104104 });
105105 sourceSets .configureEach (sourceSet -> {
106106 ConfigurationContainer configurations = getConfigurations ();
107- Configuration runtime = configurations .getByName (sourceSet .getRuntimeClasspathConfigurationName ());
108- Configuration compile = configurations . getByName ( sourceSet . getCompileClasspathConfigurationName ( ));
109- Configuration processor = configurations .getByName (sourceSet .getAnnotationProcessorConfigurationName ());
110- runtime .shouldResolveConsistentlyWith (mainRuntimeClasspath );
111- compile . shouldResolveConsistentlyWith ( runtime );
112- processor .shouldResolveConsistentlyWith (runtime );
107+ NamedDomainObjectProvider < Configuration > runtime = configurations .named (sourceSet .getRuntimeClasspathConfigurationName (), c ->
108+ c . shouldResolveConsistentlyWith ( mainRuntimeClasspath . get () ));
109+ configurations .named (sourceSet .getCompileClasspathConfigurationName (), c ->
110+ c .shouldResolveConsistentlyWith (runtime . get ()) );
111+ configurations . named ( sourceSet . getAnnotationProcessorConfigurationName (), c ->
112+ c .shouldResolveConsistentlyWith (runtime . get ()) );
113113 });
114114 return mainRuntimeClasspath ;
115115 }
116116
117- private Configuration maybeCreateInternalConfiguration () {
118- Configuration internal = getConfigurations ().findByName (INTERNAL_CONFIGURATION_NAME );
119- if (internal != null ) {
120- return internal ;
117+ private NamedDomainObjectProvider <Configuration > maybeCreateInternalConfiguration () {
118+ if (getConfigurations ().getNames ().contains (INTERNAL_CONFIGURATION_NAME )) {
119+ return getConfigurations ().named (INTERNAL_CONFIGURATION_NAME );
121120 }
122- return getConfigurations ().create (INTERNAL_CONFIGURATION_NAME , c -> {
121+ return getConfigurations ().register (INTERNAL_CONFIGURATION_NAME , c -> {
123122 c .setCanBeResolved (false );
124123 c .setCanBeConsumed (false );
125124 });
0 commit comments