diff --git a/buildSrc/deploy.gradle b/buildSrc/deploy.gradle index a2ed8c359..1b52a8290 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 COMPILE_DEP_TYPE = "compile" def trueValue = "true" ext.configureDeploy = { context -> @@ -65,34 +66,56 @@ 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 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.mavenType + def isOptional = !it.type.isTransitive + dependency { - groupId artifactGroupId + groupId androidStandardArtifactGroupId artifactId depArtifactName version depArtifactVersion - scope "compile" + scope depArtifactScope + optional isOptional } } } } - //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 + 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 { + dependency.scope = COMPILE_DEP_TYPE + dependency.optional = true + } } + } } } // 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 a1d36b3fd..d5d7c7b2f 100644 --- a/buildSrc/src/main/kotlin/ru/surfstudio/android/build/Components.kt +++ b/buildSrc/src/main/kotlin/ru/surfstudio/android/build/Components.kt @@ -4,7 +4,9 @@ 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.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 @@ -94,16 +96,22 @@ object Components { } /** - * Get standard artifact names by library name + * Get standard artifacts */ @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 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/DependencyConfigurator.kt b/buildSrc/src/main/kotlin/ru/surfstudio/android/build/DependencyConfigurator.kt index 89a67d2ec..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, IMPLEMENTATION_DEP_TYPE, getDependencyArtifactoryName(it.name)) + addDependency(project, it.type.gradleType, getDependencyArtifactoryName(it.name)) } else { - addDependency(project, IMPLEMENTATION_DEP_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..2cc6e3cf7 --- /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 + +/** + * Enum of maven and gradle dependencies ratios + */ +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 16e022ee8..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,5 +7,9 @@ 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 + override val type: DependencyType = DependencyType.IMPLEMENTATION +) : Dependency(name, type) { + + val artifactGroupId = name.substringBefore(':') + val artifactName = name.substringAfter(':') +} \ No newline at end of file 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