From 813d865a70ed1cdc065f40d57af488b017917a47 Mon Sep 17 00:00:00 2001 From: Ilia Trofimenko Date: Tue, 22 Sep 2020 21:25:00 +0300 Subject: [PATCH 1/4] ANDDEP-1076 fix module dependencies during deployment --- buildSrc/deploy.gradle | 5 +++-- .../kotlin/ru/surfstudio/android/build/Components.kt | 12 +++++------- .../android/build/DependencyConfigurator.kt | 4 ++-- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/buildSrc/deploy.gradle b/buildSrc/deploy.gradle index a2ed8c359..199668bbc 100644 --- a/buildSrc/deploy.gradle +++ b/buildSrc/deploy.gradle @@ -76,13 +76,14 @@ ext.configureDeploy = { context -> pom.project { dependencies { Components.getAndroidStandardDependencies(project.name).each { - def depArtifactName = it.getArtifactName() + def depArtifactName = it.getName() def depArtifactVersion = Components.getModuleVersion(it.getName()) + def depArtifactScope = it.type dependency { groupId artifactGroupId artifactId depArtifactName version depArtifactVersion - scope "compile" + scope depArtifactScope } } } diff --git a/buildSrc/src/main/kotlin/ru/surfstudio/android/build/Components.kt b/buildSrc/src/main/kotlin/ru/surfstudio/android/build/Components.kt index a1d36b3fd..ccbacbc0f 100644 --- a/buildSrc/src/main/kotlin/ru/surfstudio/android/build/Components.kt +++ b/buildSrc/src/main/kotlin/ru/surfstudio/android/build/Components.kt @@ -4,6 +4,7 @@ import org.gradle.api.GradleException import ru.surfstudio.android.build.exceptions.component.ComponentNotFoundForStandardDependencyException import ru.surfstudio.android.build.exceptions.library.LibraryNotFoundException import ru.surfstudio.android.build.model.Component +import ru.surfstudio.android.build.model.dependency.AndroidStandardDependency import ru.surfstudio.android.build.model.dependency.Dependency import ru.surfstudio.android.build.model.json.ComponentJson import ru.surfstudio.android.build.model.module.Library @@ -97,13 +98,10 @@ object Components { * Get standard artifact names by library name */ @JvmStatic - fun getAndroidStandardDependencies(libraryName: String): List { - val libs = value.flatMap { it.libraries } - val standardDepNames = libs.find { it.name == libraryName } - ?.androidStandardDependencies - ?.map(Dependency::name) ?: return emptyList() - return libs.filter { standardDepNames.contains(it.name) } - } + fun getAndroidStandardDependencies(libraryName: String): List = + value.flatMap { it.libraries } + .find { it.name == libraryName } + ?.androidStandardDependencies ?: emptyList() /** * Get component stability by module name diff --git a/buildSrc/src/main/kotlin/ru/surfstudio/android/build/DependencyConfigurator.kt b/buildSrc/src/main/kotlin/ru/surfstudio/android/build/DependencyConfigurator.kt index 89a67d2ec..14b409fe8 100644 --- a/buildSrc/src/main/kotlin/ru/surfstudio/android/build/DependencyConfigurator.kt +++ b/buildSrc/src/main/kotlin/ru/surfstudio/android/build/DependencyConfigurator.kt @@ -189,9 +189,9 @@ object DependencyConfigurator { private fun addAndroidStandardDependencies(project: Project, dependencies: List) { dependencies.forEach { if (!isProjectIncluded(project, it.name)) { - addDependency(project, IMPLEMENTATION_DEP_TYPE, getDependencyArtifactoryName(it.name)) + addDependency(project, it.type, getDependencyArtifactoryName(it.name)) } else { - addDependency(project, IMPLEMENTATION_DEP_TYPE, project.rootProject.project(":${it.name}")) + addDependency(project, it.type, project.rootProject.project(":${it.name}")) } } } From 87819004dfbf0efda49f51a140a0677d37f56501 Mon Sep 17 00:00:00 2001 From: Ilia Trofimenko Date: Thu, 24 Sep 2020 14:54:57 +0300 Subject: [PATCH 2/4] ANDDEP-1076 fix third-party dependencies scope --- buildSrc/deploy.gradle | 24 +++++++++++++++---- .../ru/surfstudio/android/build/Components.kt | 12 +++++++++- .../model/dependency/ThirdPartyDependency.kt | 6 ++++- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/buildSrc/deploy.gradle b/buildSrc/deploy.gradle index 199668bbc..a68ceb15c 100644 --- a/buildSrc/deploy.gradle +++ b/buildSrc/deploy.gradle @@ -9,6 +9,7 @@ apply plugin: 'maven' // Function for configuration of dependencies of current android-standard module. // Context of current module is passed in parameters. +final IMPLEMENTATION_DEP_TYPE = "implementation" def trueValue = "true" ext.configureDeploy = { context -> @@ -88,12 +89,25 @@ ext.configureDeploy = { context -> } } } - //This code deletes "implementation project("...") dependencies from pom file" - pom.whenConfigured { - p -> - p.dependencies = p.dependencies.findAll { - dep -> dep.groupId != rootDir.name + pom.whenConfigured { p -> + // Deletes "implementation project("...") dependencies from pom file" + p.dependencies = p.dependencies.findAll { + dep -> dep.groupId != rootDir.name + } + + // Replaces the third-party dependencies scope in the pom file + // to the type specified in the file components.json + Components.getThirdPartyDependencies(project.name).each { thirdPartyDependency -> + p.dependencies = p.dependencies.each { + if (it.groupId == thirdPartyDependency.artifactGroupId + && it.artifactId == thirdPartyDependency.artifactName + ) { + it.scope = thirdPartyDependency.type + } else { + it.scope = IMPLEMENTATION_DEP_TYPE + } } + } } } // repositories.mavenDeployer } diff --git a/buildSrc/src/main/kotlin/ru/surfstudio/android/build/Components.kt b/buildSrc/src/main/kotlin/ru/surfstudio/android/build/Components.kt index ccbacbc0f..d5d7c7b2f 100644 --- a/buildSrc/src/main/kotlin/ru/surfstudio/android/build/Components.kt +++ b/buildSrc/src/main/kotlin/ru/surfstudio/android/build/Components.kt @@ -6,6 +6,7 @@ import ru.surfstudio.android.build.exceptions.library.LibraryNotFoundException import ru.surfstudio.android.build.model.Component import ru.surfstudio.android.build.model.dependency.AndroidStandardDependency import ru.surfstudio.android.build.model.dependency.Dependency +import ru.surfstudio.android.build.model.dependency.ThirdPartyDependency import ru.surfstudio.android.build.model.json.ComponentJson import ru.surfstudio.android.build.model.module.Library import ru.surfstudio.android.build.model.module.Module @@ -95,7 +96,7 @@ object Components { } /** - * Get standard artifact names by library name + * Get standard artifacts */ @JvmStatic fun getAndroidStandardDependencies(libraryName: String): List = @@ -103,6 +104,15 @@ object Components { .find { it.name == libraryName } ?.androidStandardDependencies ?: emptyList() + /** + * Get third party artifacts + */ + @JvmStatic + fun getThirdPartyDependencies(libraryName: String): List = + value.flatMap { it.libraries } + .find { it.name == libraryName } + ?.thirdPartyDependencies ?: emptyList() + /** * Get component stability by module name */ diff --git a/buildSrc/src/main/kotlin/ru/surfstudio/android/build/model/dependency/ThirdPartyDependency.kt b/buildSrc/src/main/kotlin/ru/surfstudio/android/build/model/dependency/ThirdPartyDependency.kt index 16e022ee8..0c100f6d2 100644 --- a/buildSrc/src/main/kotlin/ru/surfstudio/android/build/model/dependency/ThirdPartyDependency.kt +++ b/buildSrc/src/main/kotlin/ru/surfstudio/android/build/model/dependency/ThirdPartyDependency.kt @@ -8,4 +8,8 @@ import ru.surfstudio.android.build.utils.EMPTY_STRING data class ThirdPartyDependency( override val name: String = EMPTY_STRING, override val type: String = EMPTY_STRING -) : Dependency(name, type) \ No newline at end of file +) : Dependency(name, type) { + + val artifactGroupId = name.substringBefore(':') + val artifactName = name.substringAfter(':') +} \ No newline at end of file From fb2e0ccbec6ee812455f6eb20ce9b570e1ff4f11 Mon Sep 17 00:00:00 2001 From: Ilia Trofimenko Date: Mon, 19 Oct 2020 06:16:15 +0300 Subject: [PATCH 3/4] ANDDEP-1076 fix third-party dependencies scope --- buildSrc/deploy.gradle | 32 ++++++++++++------- .../android/build/DependencyConfigurator.kt | 6 ++-- .../dependency/AndroidStandardDependency.kt | 2 +- .../build/model/dependency/Dependency.kt | 2 +- .../build/model/dependency/DependencyType.kt | 28 ++++++++++++++++ .../model/dependency/ThirdPartyDependency.kt | 2 +- .../build/model/json/DependencyJson.kt | 7 ++-- 7 files changed, 58 insertions(+), 21 deletions(-) create mode 100644 buildSrc/src/main/kotlin/ru/surfstudio/android/build/model/dependency/DependencyType.kt diff --git a/buildSrc/deploy.gradle b/buildSrc/deploy.gradle index a68ceb15c..1b52a8290 100644 --- a/buildSrc/deploy.gradle +++ b/buildSrc/deploy.gradle @@ -9,7 +9,7 @@ apply plugin: 'maven' // Function for configuration of dependencies of current android-standard module. // Context of current module is passed in parameters. -final IMPLEMENTATION_DEP_TYPE = "implementation" +final COMPILE_DEP_TYPE = "compile" def trueValue = "true" ext.configureDeploy = { context -> @@ -66,11 +66,11 @@ ext.configureDeploy = { context -> ) } - def artifactGroupId = ArtifactoryConfig.ANDROID_STANDARD_GROUP_ID + def androidStandardArtifactGroupId = ArtifactoryConfig.ANDROID_STANDARD_GROUP_ID def artifactVersion = Components.getModuleVersion(project.name) println "uploadAcrhives $artifactName $artifactVersion" - pom.groupId = artifactGroupId + pom.groupId = androidStandardArtifactGroupId pom.artifactId = artifactName pom.version = artifactVersion @@ -79,12 +79,15 @@ ext.configureDeploy = { context -> Components.getAndroidStandardDependencies(project.name).each { def depArtifactName = it.getName() def depArtifactVersion = Components.getModuleVersion(it.getName()) - def depArtifactScope = it.type + def depArtifactScope = it.type.mavenType + def isOptional = !it.type.isTransitive + dependency { - groupId artifactGroupId + groupId androidStandardArtifactGroupId artifactId depArtifactName version depArtifactVersion scope depArtifactScope + optional isOptional } } } @@ -97,14 +100,19 @@ ext.configureDeploy = { context -> // Replaces the third-party dependencies scope in the pom file // to the type specified in the file components.json - Components.getThirdPartyDependencies(project.name).each { thirdPartyDependency -> - p.dependencies = p.dependencies.each { - if (it.groupId == thirdPartyDependency.artifactGroupId - && it.artifactId == thirdPartyDependency.artifactName - ) { - it.scope = thirdPartyDependency.type + def thirdPartyDependencies = Components.getThirdPartyDependencies(project.name) + p.dependencies.each { dependency -> + if (dependency.groupId != androidStandardArtifactGroupId) { + def thirdPartyDependency = thirdPartyDependencies.find { + dependency.groupId == it.artifactGroupId && dependency.artifactId == it.artifactName + } + + if (thirdPartyDependency != null) { + dependency.scope = thirdPartyDependency.type.mavenType + dependency.optional = !thirdPartyDependency.type.isTransitive } else { - it.scope = IMPLEMENTATION_DEP_TYPE + dependency.scope = COMPILE_DEP_TYPE + dependency.optional = true } } } diff --git a/buildSrc/src/main/kotlin/ru/surfstudio/android/build/DependencyConfigurator.kt b/buildSrc/src/main/kotlin/ru/surfstudio/android/build/DependencyConfigurator.kt index 14b409fe8..9add308ac 100644 --- a/buildSrc/src/main/kotlin/ru/surfstudio/android/build/DependencyConfigurator.kt +++ b/buildSrc/src/main/kotlin/ru/surfstudio/android/build/DependencyConfigurator.kt @@ -182,16 +182,16 @@ object DependencyConfigurator { private fun addThirdPartyDependencies(project: Project, dependencies: List) { dependencies.forEach { - addDependency(project, it.type, "${it.name}:${libraryVersions[it.name]}") + addDependency(project, it.type.gradleType, "${it.name}:${libraryVersions[it.name]}") } } private fun addAndroidStandardDependencies(project: Project, dependencies: List) { dependencies.forEach { if (!isProjectIncluded(project, it.name)) { - addDependency(project, it.type, getDependencyArtifactoryName(it.name)) + addDependency(project, it.type.gradleType, getDependencyArtifactoryName(it.name)) } else { - addDependency(project, it.type, project.rootProject.project(":${it.name}")) + addDependency(project, it.type.gradleType, project.rootProject.project(":${it.name}")) } } } diff --git a/buildSrc/src/main/kotlin/ru/surfstudio/android/build/model/dependency/AndroidStandardDependency.kt b/buildSrc/src/main/kotlin/ru/surfstudio/android/build/model/dependency/AndroidStandardDependency.kt index 18dc96309..4ad4ee46f 100644 --- a/buildSrc/src/main/kotlin/ru/surfstudio/android/build/model/dependency/AndroidStandardDependency.kt +++ b/buildSrc/src/main/kotlin/ru/surfstudio/android/build/model/dependency/AndroidStandardDependency.kt @@ -8,6 +8,6 @@ import ru.surfstudio.android.build.utils.EMPTY_STRING */ data class AndroidStandardDependency( override val name: String = EMPTY_STRING, - override val type: String = EMPTY_STRING, + override val type: DependencyType = DependencyType.IMPLEMENTATION, var component: Component = Component() ) : Dependency(name, type) \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/ru/surfstudio/android/build/model/dependency/Dependency.kt b/buildSrc/src/main/kotlin/ru/surfstudio/android/build/model/dependency/Dependency.kt index eae718ede..0857fa32b 100644 --- a/buildSrc/src/main/kotlin/ru/surfstudio/android/build/model/dependency/Dependency.kt +++ b/buildSrc/src/main/kotlin/ru/surfstudio/android/build/model/dependency/Dependency.kt @@ -7,5 +7,5 @@ import ru.surfstudio.android.build.utils.EMPTY_STRING */ open class Dependency( open val name: String = EMPTY_STRING, - open val type: String = EMPTY_STRING + open val type: DependencyType = DependencyType.IMPLEMENTATION ) \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/ru/surfstudio/android/build/model/dependency/DependencyType.kt b/buildSrc/src/main/kotlin/ru/surfstudio/android/build/model/dependency/DependencyType.kt new file mode 100644 index 000000000..4d45c59ab --- /dev/null +++ b/buildSrc/src/main/kotlin/ru/surfstudio/android/build/model/dependency/DependencyType.kt @@ -0,0 +1,28 @@ +package ru.surfstudio.android.build.model.dependency + +import org.gradle.api.GradleException + +/** + * Dependencies types + */ +enum class DependencyType( + val gradleType: String, + val mavenType: String, + val isTransitive: Boolean +) { + + IMPLEMENTATION("implementation", "runtime", false), + API("api", "compile", true), + COMPILE_ONLY("compileOnly", "provided", false), + RUNTIME_ONLY("runtimeOnly", "runtime", false), + TEST_IMPLEMENTATION("testImplementation", "test", false), + ANDROID_TEST_IMPLEMENTATION("androidTestImplementation", "test", false), + KAPT("kapt", "", true), + KAPT_TEST("kaptTest", "", true); + + companion object { + + fun fomString(type: String) = values().find { it.gradleType == type } + ?: throw GradleException("failed to parse the type of dependency: $type") + } +} \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/ru/surfstudio/android/build/model/dependency/ThirdPartyDependency.kt b/buildSrc/src/main/kotlin/ru/surfstudio/android/build/model/dependency/ThirdPartyDependency.kt index 0c100f6d2..cf0d5e9db 100644 --- a/buildSrc/src/main/kotlin/ru/surfstudio/android/build/model/dependency/ThirdPartyDependency.kt +++ b/buildSrc/src/main/kotlin/ru/surfstudio/android/build/model/dependency/ThirdPartyDependency.kt @@ -7,7 +7,7 @@ import ru.surfstudio.android.build.utils.EMPTY_STRING */ data class ThirdPartyDependency( override val name: String = EMPTY_STRING, - override val type: String = EMPTY_STRING + override val type: DependencyType = DependencyType.IMPLEMENTATION ) : Dependency(name, type) { val artifactGroupId = name.substringBefore(':') diff --git a/buildSrc/src/main/kotlin/ru/surfstudio/android/build/model/json/DependencyJson.kt b/buildSrc/src/main/kotlin/ru/surfstudio/android/build/model/json/DependencyJson.kt index 3cdc4d28f..f5567c833 100644 --- a/buildSrc/src/main/kotlin/ru/surfstudio/android/build/model/json/DependencyJson.kt +++ b/buildSrc/src/main/kotlin/ru/surfstudio/android/build/model/json/DependencyJson.kt @@ -2,6 +2,7 @@ package ru.surfstudio.android.build.model.json import ru.surfstudio.android.build.model.dependency.AndroidStandardDependency import ru.surfstudio.android.build.model.dependency.Dependency +import ru.surfstudio.android.build.model.dependency.DependencyType import ru.surfstudio.android.build.model.dependency.ThirdPartyDependency import ru.surfstudio.android.build.utils.EMPTY_STRING @@ -15,14 +16,14 @@ data class DependencyJson( constructor(dependency: Dependency) : this( name = dependency.name, - type = dependency.type + type = dependency.type.gradleType ) fun transformToAndroidStandardDependency() = AndroidStandardDependency( - name, type + name, DependencyType.fomString(type) ) fun transformToThirdPartyDependency() = ThirdPartyDependency( - name, type + name, DependencyType.fomString(type) ) } \ No newline at end of file From bb4ed48f66e89c302b4792f5aee2ba62cae851ca Mon Sep 17 00:00:00 2001 From: Ilia Trofimenko Date: Mon, 8 Feb 2021 23:34:46 +0300 Subject: [PATCH 4/4] ANDDEP-1076 added comment --- .../surfstudio/android/build/model/dependency/DependencyType.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/buildSrc/src/main/kotlin/ru/surfstudio/android/build/model/dependency/DependencyType.kt b/buildSrc/src/main/kotlin/ru/surfstudio/android/build/model/dependency/DependencyType.kt index 4d45c59ab..2cc6e3cf7 100644 --- a/buildSrc/src/main/kotlin/ru/surfstudio/android/build/model/dependency/DependencyType.kt +++ b/buildSrc/src/main/kotlin/ru/surfstudio/android/build/model/dependency/DependencyType.kt @@ -3,7 +3,7 @@ package ru.surfstudio.android.build.model.dependency import org.gradle.api.GradleException /** - * Dependencies types + * Enum of maven and gradle dependencies ratios */ enum class DependencyType( val gradleType: String,