Import build script from main build.gradle

In my all projects I have build.gradle and it has buildscript with repositories, spring version & information
For security scanning, we are creating new job in Teamcity where it need all project dependencies in one folder, so it can scan all of them (instead of final build - for some reason).

I am trying to create a temporary file in TC job with name dependencies.gradle like below

apply from: 'build.gradle'
task copyDependencies(type: Copy) {
    from configurations.default
    into 'build/dependencies'
}

Running it with ./gradlew --build-file=dependencies.gradle copyDependencies
But this is not working as it is not getting repositories & other info from main build.gradle buildscript.

I am just trying to avoid adding this task in all projects for just scanning job. Is there any better way?

--include-build is for creating a composite build, which I don’t think is what you intended. Did you mean to use --build-file instead?

sorry my bad, I tried with --build-file, i will update my question

I am unable to reproduce an issue. I just tried your dependencies.gradle file and the copyDependencies task worked. In my build.gradle I have something simple:

apply plugin: 'java'
repositories {
    mavenCentral()
}
dependencies {
    implementation 'junit:junit:4.+'
}

Personally, I implement similar Jenkins-only content using init scripts. You can include them on the command line using -I or put them in ~/.gradle/init.d. For example:

allprojects {
    pluginManager.withPlugin( 'java' ) {
        task copyDependencies( type: Copy ) {
            from configurations.default
            into 'build/dependencies'
        }
    }
}

repository definition is in buildscript in main build.gradle
build.gradle

buildscript {
 ext {
        springBootVersion = '2.2.2.RELEASE'
        springCloudVersion = 'Hoxton.SR1'
        swaggerCodeGenVersion = '2.2.3'
 }
 repositories {
        artefact_repo_urls.split(',').each { repoUrl ->
            maven {
                url repoUrl
                credentials {
                    username = artefact_repo_user
                    password = artefact_repo_password
                }
            }
        }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath("test:test-gradle-plugin:2.0.7")
        classpath("io.swagger:swagger-codegen:${swaggerCodeGenVersion}")
    }
}

apply plugin: 'com.test.repositories'
apply plugin: 'com.test.java'
apply plugin: 'com.test.ear'
// some other custom plugins

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"

    }
}

dependencies {

    implementation('org.springframework.boot:spring-boot-starter-web')
    implementation("org.springframework.boot:spring-boot-autoconfigure")
// a lot more
}
1 Like

The buildscript { repositories { ... } } only apply to the classpath used to execute the gradle build script itself and are not related to the resolution of the dependencies of configurations.default. You must have an additional repositories { ... } block somewhere outside buildscript { ... } (or in a plugin?).

As far as I know what you have should work. Are you getting an error? What is it?

This is the error that I am getting with the command ./gradlew --build-file=dependencies.gradle copyDependencies

* What went wrong:
A problem occurred evaluating script.
> Plugin with id 'com.test.repository' not found.

Sorry, I thought you were having problems resolving the default configuration.

Something seems off, the code shown applies the plugin com.test.repositories and the error references com.test.repository. Are these the same plugin?

Where is this defined?

If you add println "urls='${artefact_repo_urls}'" before artefact_repo_urls.split ..., do you see the expected value?

Sorry my bad again, i renamed company names & missed that, it was complaining about the plugin com.test.repositories which defined in the main build.gradle

artefact_repo_urls are defined in gradle.properties and there is no issue if I run with build.gradle, looks like it plugins defined in the test:test-gradle-plugin in buildscript is not visible to dependencies.gradle.