Search code examples
androidgradlegradle-kotlin-dsl

How does one configure custom maven dependency in `gradle.build.kts` while using `dependencyResolutionManagement` in `settings.gradle.kts`?


Gradle fails to resolve dependencies when declaring custom maven repo. It looks for dependencies in the custom repo instead of maven central.

What I have so far:

// build.gradle.kts
repositories {
    maven {
        url = uri("https://maven.pkg.jetbrains.space/myTeam/p/myProject/maven")
        credentials {
            username = project.extra["space_usr"].toString()
            password = project.extra["space_pwd"].toString()
        }
    }
}
// settings.gradle.kts
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}

But I get the following error : Build was configured to prefer settings repositories over project repositories but repository 'maven' was added by build file 'app/build.gradle.kts' .

If I remvove repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) then I get the following error :

Could not determine the dependencies of task ':app:kaptDebugKotlin'.
> Could not resolve all dependencies for configuration ':app:kotlinKaptWorkerDependencies'.
  The project declares repositories, effectively ignoring the repositories you have declared in the settings.
  You can figure out how project repositories are declared by configuring your build to fail on project repositories.
  See https://docs.gradle.org/7.0-rc-1/userguide/declaring_repositories.html#sub:fail_build_on_project_repositories for details.
   > Could not find org.jetbrains.kotlin:kotlin-annotation-processing-gradle:1.4.32.
     Searched in the following locations:
       - https://maven.pkg.jetbrains.space/...
       - https://maven.pkg.jetbrains.space/...
     Required by:
         project :app

I could add my custom maven dependency in settings.gradle.kts but project.extra is not accessible and requires clear text (edited)


Solution

  • I figured out it's not possible, instead, i declared my maven dependency in settings.gradle.kts and used the following :

    // settings.gradle.kts
    maven {
        url = uri(""https://maven.pkg.jetbrains.space/myTeam/p/myProject/maven")
        name = "myProject"
        credentials(PasswordCredentials::class)
    }
    
    // gradle.properties
    myProjectUsername = [username]
    myProjectPassword = [password]