2424 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2525 */
2626
27+ @file:Suppress(" UnstableApiUsage" ) // We need to use unstable settings API.
28+
2729package io.spine.tools.gradle.root
2830
2931import io.spine.tools.gradle.DslSpec
@@ -32,6 +34,9 @@ import io.spine.tools.gradle.project.ProjectPlugin
3234import io.spine.tools.gradle.root.RootExtension.Companion.NAME
3335import org.gradle.api.Project
3436import org.gradle.api.file.Directory
37+ import org.gradle.api.initialization.resolve.RepositoriesMode
38+ import org.gradle.api.internal.GradleInternal
39+ import org.gradle.api.internal.SettingsInternal
3540import org.gradle.api.plugins.ExtensionAware
3641
3742/* *
@@ -40,7 +45,9 @@ import org.gradle.api.plugins.ExtensionAware
4045 * The extension is used by Gradle plugins of libraries that extend
4146 * the [root extension][RootExtension] with custom configuration DSL.
4247 *
43- * The plugin also applies repositories [standard for Spine SDK][applyStandard] to the project.
48+ * The plugin also applies repositories [standard for Spine SDK][applyStandard]
49+ * to the project IFF `dependencyResolutionManagement.repositoriesMode` property set
50+ * in `settings.gradle.kts` to [RepositoriesMode.PREFER_PROJECT] or assumed such.
4451 */
4552public class RootPlugin :
4653 ProjectPlugin <RootExtension >(DslSpec (NAME , RootExtension ::class )) {
@@ -61,14 +68,12 @@ public class RootPlugin :
6168 }
6269
6370 /* *
64- * Applies the plugin to the given [project] by forcing creation of the [extension].
65- *
66- * Also applies repositories [standard for Spine SDK][applyStandard].
71+ * Applies the plugin to the given [project].
6772 */
6873 override fun apply (project : Project ) {
6974 super .apply (project)
7075 createExtension()
71- project.repositories.applyStandard ()
76+ project.tryApplyStandardRepositories ()
7277 check(extension != null ) {
7378 " The extension of the `${this ::class .simpleName} ` has not been created."
7479 }
@@ -82,3 +87,30 @@ public class RootPlugin :
8287 public const val ID : String = " io.spine.root"
8388 }
8489}
90+
91+ /* *
92+ * Attempts to apply repositories [standard for Spine SDK][applyStandard] to the project
93+ * by inspecting the `dependencyResolutionManagement.repositoriesMode` value set
94+ * in `settings.gradle.kts`.
95+ *
96+ * If the value set explicitly or assumed as [RepositoriesMode.PREFER_PROJECT],
97+ * the repositories are applied.
98+ */
99+ private fun Project.tryApplyStandardRepositories () {
100+ val settings = project.settings
101+ if (settings == null ) {
102+ repositories.applyStandard()
103+ return
104+ }
105+ val mode = settings.dependencyResolutionManagement.repositoriesMode.get()
106+ if (mode == RepositoriesMode .PREFER_PROJECT ) {
107+ repositories.applyStandard()
108+ }
109+ }
110+
111+ private val Project .settings: SettingsInternal ?
112+ get() = try {
113+ (project.gradle as GradleInternal ).settings
114+ } catch (_: IllegalStateException ) {
115+ null
116+ }
0 commit comments