Gradle: Configure Repository for transitive dependencies - gradle

imagine you have to go through proxy repositories for all dependency resolutions, because you are behind a proxy.
For most repositories, this can be easily configured within the build.gradle file:
buildscript {
repositories { ... }
}
and
repositories { ... }
and even in settings.gradle the
pluginManagement {
repositories { ... }
}
but it seems that transitive dependencies are still downloaded through jCenter.
Where can I configure the repositories for transitive dependencies?
Update: I still try to find the real root cause of my problem. It could be that it has to do with the way I've build my build script - the dependencies are defined in a script plugin of a sub project.

I still don't know why my system behaves this way, but here is a solution which works for me:
create a file called init.gradle in USER_HOME/.gradle/ and add your repositiories like this:
allprojects {
buildscript {
repositories { ... }
}
repositories { ... }
}

Related

Cannot add subproject to gradle buildscript classpath in 7.3

I have a subprojectA and subprojectB. There are some artifacts from subprojectA that i need to build subprojectB. It seems that after upgrading to 7.3, i can no longer configure it like this.
Getting the error 'Cannot transition to state Configure as already transitioning to this state.'
May I know if there is an alternative or a solution to the error?
subprojectb build.gradle:
buildscript {
dependencies {
classpath project(':subprojectA')
}
}
plugins {
...
}
...
The solution is to make subprojectA an included build instead of a subproject, then use the GAV coordinates instead of the project dir.
//build.gradle
buildscript {
dependencies {
classpath 'com.example:subprojectA:1.0'
}
}
plugins {
...
}
...
// settings.gradle
includeBuild('./subprojectA')
https://docs.gradle.org/current/userguide/structuring_software_products.html#connecting_components

Gradle can't find dependency in private nexus repo

I'm having trouble getting gradle to find a dependency I put in my private nexus repo. The dependency is in maven, but I can't seem to get it to find it there either. I did get it into my nexus repo and the location is http://nexus.hq.chris.com/content/repositories/emoji4j/
Could not resolve all dependencies for configuration ':business:compile'.
> Could not find com.kcthota:emoji4j:6.0.
Searched in the following locations:
http://nexus.hq.chris.com/content/groups/public/com/kcthota/emoji4j/6.0/emoji4j-6.0.pom
http://nexus.hq.chris.com/content/groups/public/com/kcthota/emoji4j/6.0/emoji4j-6.0.jar
file:/Users/chris/.m2/repository/com/kcthota/emoji4j/6.0/emoji4j-6.0.pom
file:/Users/chris/.m2/repository/com/kcthota/emoji4j/6.0/emoji4j-6.0.jar
Required by:
Build.gradle snipet
dependencies {
// https://mvnrepository.com/artifact/com.kcthota/emoji4j
compile group: 'com.kcthota', name: 'emoji4j', version: '6.0'
}
buildscript {
repositories {
maven {
url "http://nexus.hq.chris.com/content/groups/public/"
}
maven { url "https://repo1.maven.org/maven2/" }
maven { url "http://nexus.hq.chris.com/content/repositories/emoji4j/" }
mavenCentral()
}
dependencies {
classpath 'com.jcraft:jsch:0.1.54'
}
}
Anyone know how I can get gradle to look in both http://nexus.hq.chris.com/content/groups/public/ and http://nexus.hq.chris.com/content/repositories/emoji4j/ for all my dependencies? I need the http://nexus.hq.chris.com/content/groups/public/ location for other dependencies. I tried adding it in there but I only have read access to that repo.
Another acceptable solution would be to get gradle to look in both http://nexus.hq.chris.com/content/groups/public/ and maven central for it. Any help would be appreciated.
I think you may be confusing dependencies for your build script and application dependencies.
You've configured your build script repositories, but you'll need to also configure your application repositories as well:
// build.gradle
repositories {
maven {
url "http://nexus.hq.chris.com/content/groups/public/"
}
maven { url "https://repo1.maven.org/maven2/" }
maven { url "http://nexus.hq.chris.com/content/repositories/emoji4j/" }
mavenCentral()
}

How to use init.gradle to provide plugin repositories for plugins configured in the settings.gradle

I have configured a Gradle plugin in settings.gradle file as follows,
buildscript {
dependencies {
classpath "org.test.group:gradleplugins:${pluginVersion}"
...
}
}
apply plugin: 'org.test.group:gradleplugins'
....
and I am trying to provide artifacts repository using init.gradle as follows,
initscript {
repositories {
maven { url "https://test.repo/gradleplugins" }
...
}
}
also, I have provided init.gradle file to the build task using,
.... -i -I ./init.gradle'
but the build still gets a dependency resolution error as follows,
Cannot resolve external dependency org.test.group:gradleplugins:1.0.0-SNAPSHOT because no repositories are defined.
It could be done either way by writing Gradle plugin in the init.gradle file as following,
apply plugin: EnterpriseRepositoryPlugin
class EnterpriseRepositoryPlugin implements Plugin<Gradle> {
void apply(Gradle gradle) {
gradle.settingsEvaluated { settings ->
settings.pluginManagement {
repositories {
maven { url "https://repo.org/gradleplugins" }
maven { url "https://repo.org/maven" }
}
}
}
}
}
according to Gradle documentation,
https://docs.gradle.org/current/userguide/init_scripts.html
The init.gradle has another purpose (and that file is being automatically detected).
void settingsEvaluated​(Settings settings) might be to only chance to manipulate these settings (but the question does not provide the least valid reason to do so, with only one repository). It rather seems that you're unnecessarily over-complicating things, where there otherwise would be no problem. And this doesn't belong into the settings.gradle either (which also has another purpose). Just add the plugin repositories into the buildscript block of the root project's build.gradle, where they belong. The userguide shows how it should look alike, when defining the plugin repositories statically.

Can I Define The buildscript repositories In The Root Project For All Modules

I have code similar to the following in each of my sub-modules but with different plugins
buildscript {
ext {
springBootVersion = '2.0.0.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
Can the repositories be set in the root project so I don't repeat it?
I already have the main repositories section set in the root like this but this question is for the plugins and the buildscript section.
allprojects {
repositories {
jcenter()
maven { url 'https://jitpack.io' }
}
}
The  buildscript block can't be put into a separate file and then imported, it's a special one.
What you can do on the other hand is to put your buildscript block into the build.gradle file of the root project and then all other build files from subprojects will inherit this block.
You can copy the repositories from the buildscript of the root project, like this:
buildscript {
repositories.addAll rootProject.buildscript.repositories
// plugin dependencies, etc, go here
}
You still have to copy that one line to the buildscript block of your subsidiary Gradle file, but at least it is just one line. This works both for subprojects, and also for additional .gradle files you include using apply from:
You can even put that one-liner into a method in build.gradle:
rootProject.ext.setupPluginRepos = {
it.repositories.addAll rootProject.buildscript.repositories
}
So then your subproject (or Gradle file) just has to do this:
buildscript {
rootProject.ext.setupPluginRepos(it)
// plugin dependencies, etc, go here
}
I wrote a custom Gradle plugin, used for certain internal projects at my employer. It adds an extension to every project, let us just call it xyzzy (not its actual name). So I added the above setupPluginRepos to my plugin, and now I just have to do:
buildscript {
xyzzy.setupPluginRepos(it)
// plugin dependencies, etc, go here
}

Gradle custom plugin dependencies

If a have a custom plugin which handles the building and deploying of a specific component, where do I list the dependencies (other components in my system) which are required for the build?
Dependencies for your Gradle plugins should be listed in the buildscript portion of the build.gradle file. See this chapter of the User Guide, which also has an example:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:0.4.1"
}
}
apply plugin: "com.jfrog.bintray"
If your custom plugin depends on jar files on your local machine, I gather that you need to add those files as a "flatDir" repository in the repositories entry, as described here:
repositories {
flatDir {
dirs 'lib1', 'lib2'
}
}

Resources