aepfli / gradle-gitlab-repositories

Handling Maven GitLab dependencies made easy. Define multiple tokens and selectively apply them to repositories, remove the need for repeating Credential handling blocks for different environments.
Eclipse Public License 2.0
13 stars 1 forks source link

How to use this library with Google's new recommended Gradle config? #18

Closed kohenkatz closed 2 years ago

kohenkatz commented 2 years ago

When creating a new project in Android Studio Bumblebee (2021.1.1 RC 1), the Gradle files have the following structure:

settings.gradle

pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}
rootProject.name = "My App Name"
include ':app'

build.gradle

plugins {
    id 'com.android.application' version '7.1.0-rc01' apply false
    id 'com.android.library' version '7.1.0-rc01' apply false
    id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

app/build.gradle

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
}

android {
    compileSdk 32

    defaultConfig {
        applicationId "com.example.app"
        minSdk 21
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        viewBinding true
    }
}

dependencies {
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.gms:play-services-location:19.0.1'
    implementation 'com.google.android.material:material:1.5.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.4.0'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
    implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

The important line here is repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) in settings.gradle. It prevents repositories from being added in the build.gradle files, instead requiring them to be set here in settings.gradle.

How should this plugin be added to a project with this configuration?

aepfli commented 2 years ago

thank you for the questions :)

I will generate a test case for this, and will get back to you as soon as i have time.

what i will try is setting the following within the settings.gradle

applyToProject = false

like written in the documentation, but i am not sure if this will solve the issue. If not i will label this as a bug, and will start to investigate.

aepfli commented 2 years ago

I revoke my last answer :)

you need to define the repositories within your settings.gradle file, like

buildscript {
    gradlePluginPortal()
    dependencies {
        classpath 'at.schrottner.gradle.gitlab-plugin:gitlab-repositories:<version>'
    }
}

apply plugin: at.schrottner.gradle.GitlabRepositoriesPlugin

gitLab {
    token(PrivateToken) {
        it.key = 'privateToken'
        it.value = "token value, best stored in ~/.gradle/gradle.properties"
    }
}

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        maven gitLab.project("<your project ID>")
        maven gitLab.group("<your group ID>")
        }
}

the rest of the configuration is pretty similar to the documentation.

aepfli commented 2 years ago

see https://github.com/aepfli/gradle-gitlab-repositories/blob/issue/%2318/src/functionalTest/resources/GoogleRecommendationTest/test/groovy/settings.gradle as an example

kohenkatz commented 2 years ago

It looks like this works, thank you!

Here is an example of how to use settings.gradle with conditional token definitions based on whether the credential is set on the user's computer (since different devs on my projects want to configure it different ways).

pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
}

buildscript {
    repositories {
        gradlePluginPortal()
    }
    dependencies {
        classpath 'at.schrottner.gradle.gitlab-plugin:gitlab-repositories:0.5.0'
    }
}

apply plugin: at.schrottner.gradle.GitlabRepositoriesPlugin

def gitLabPrivateToken = properties['gitLabPrivateToken']

gitLab {
    applyToProject = true
    baseUrl = "gitlab.example.com"

    if (gitLabPrivateToken) {
        token(PrivateToken) {
            it.key = 'Private-Token'
            it.value = gitLabPrivateToken
        }
    }
    if (System.getenv("SM_GITLAB_TOKEN") != null) {
        token(PrivateToken) {
            it.key = 'Private-Token'
            it.value = System.getenv("SM_GITLAB_TOKEN")
        }
    }
}

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        maven { url "https://jitpack.io" }
        maven gitLab.project("4")
        maven gitLab.group("34")
    }
}

include ':app'
aepfli commented 2 years ago

Just for completeness, you do not need to use the ifs to define the tokens.

If the value is empty, it will not use it. And the key is just a identifier you can use. The header is defined by the method parameter.

Like

gitLab {
    applyToProject = true
    baseUrl = "dev.saferworld.live"

        token(PrivateToken) {
            it.key = 'privateToken'
            it.value = gitLabPrivateToken
        }

        token(PrivateToken) {
            it.key = 'smToken'
            it.value = System.getenv("SM_GITLAB_TOKEN")
        }

}