How setup gradle to prefer repositories from init.gradle to build.gradle? - gradle

Pls, help understand how gradle work.
In linux server i have Java project and build them from gradle 4.10.3.
In Project i have build.gradle file with repositories section. Also in subproject i have this file too.
build.gradle example:
repositories {
maven { url 'http://1.2.3.4'}
}
subprojects {
buildscript {
repositories {
maven { url 'http://1.2.3.4'}
}
}
repositories {
maven { url 'http://8.2.3.4/'}
}
}
I`m try override repositories in init.gradle file (locate in folder $GRADLE_HOME/init.d/)
But this doesnt work. When build project, gradle use repositories from build.gradle project (and subproject) file.
Also i`m try run gradle with option -I and path to init.gradle file - same result.
gradle -I $GRADLE_HOME/init.d/init.gradle -Pprod clean build
Can i override repositories section in build.gradle (located in project and subproject) by use some file in local environment, or may be some $env or something else ?
UPD.
in init.gradle file i write this:
settingsEvaluated { settings ->
settings.pluginManagement {
resolutionStrategy {
}
repositories {
maven {url 'http://13.1.1.21:8084'
}
}
}
}
allprojects {
repositories {
mavenLocal()
maven {url "http://10.8.13.21:8080/repository/"
}
}
}

Related

Gradle single-project pluginManagement block not working (Kotlin DSL)

I need to change a multi-project build to a single-project build, as there is and only ever will be one project in this repo. Currently, in settings.gradle, I have a custom plugin repo that currently uses a pluginManagement block with resolutionStrategy and my list of repo's:
pluginManagement {
resolutionStrategy {
eachPlugin {
if (requested.id.namespace == 'com.meanwhileinhell.plugin') {
useModule('com.meanwhileinhell:gradle-plugin:1.0.0-SNAPSHOT')
}
}
}
repositories {
mavenLocal()
maven { url "https://repo.spring.io/milestone" }
maven { url "https://plugins.gradle.org/m2/" }
// Meanwhileinhell repo
maven {
url "s3://mvn.meanwhileinhell.com/releases"
credentials(AwsCredentials) {
accessKey s3_access_key
secretKey s3_access_secret
}
}
}
plugins {
...
...
}
}
However, deleting settings.gradle and moving this block into my build.gradle.kts (Kotlin DSL) seems to do nothing. I've tried wrapping in a
configurations {
all {
resolutionStrategy {
eachPlugin {
...
}
}
}
}
and also
settings {
pluginManagement {
resolutionStrategy {
eachPlugin {
...
}
}
}
}
I found a SO answer that used settingsEvaluated in order to get the settings object, but again this was a no go.
Currently my build.gradle.kts looks like this, without pulling any plugin in from my repo:
val springBootVersion: String by project
group = "com.meanwhileinhell.myapp"
version = "$version"
repositories {
mavenCentral()
mavenLocal()
maven ("https://repo.spring.io/snapshot")
maven ("https://repo.spring.io/milestone")
maven ("https://plugins.gradle.org/m2/")
maven {
url = uri("s3://mvn.meanwhileinhell.com/releases")
credentials(AwsCredentials::class) {
accessKey = (project.property("s3_access_key") as String)
secretKey = (project.property("s3_access_secret") as String)
}
}
}
plugins {
base
eclipse
idea
java
id("io.spring.dependency-management") version "1.0.9.RELEASE"
// Load but don't apply to root project
id("org.springframework.boot") version "1.5.14.RELEASE" apply false
}
dependencies {
...
}
Whenever I try to add a plugin id like id("com.meanwhileinhell.plugin.hell2java") version "1.0.0-SNAPSHOT" I get an error that looks like it isn't even looking in my S3 location:
* What went wrong:
Plugin [id: 'com.meanwhileinhell.plugin.hell2java', version: '1.0.0-SNAPSHOT'] was not found in any of the following sources:
- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'com.meanwhileinhell.plugin.hell2java:com.meanwhileinhell.plugin.hell2java.gradle.plugin:1.0.0-SNAPSHOT')
Searched in the following repositories:
Gradle Central Plugin Repository
Any help on this would be appreciated!
EDIT !!!! -----------------------
I've just found this in the Gradle docs:
https://docs.gradle.org/current/userguide/plugins.html#sec:plugin_management
The pluginManagement {} block may only appear in either the settings.gradle file....
Looks like I'm going down the wrong way entirely, so will look into the initialisation script route.
I think you may have missed something about the file structure.
In the Groovy DSL, you have the following files:
build.gradle
settings.gradle
init.gradle
In the Kotlin DSL, you have the same files but with the .kts extension:
build.gradle.kts
settings.gradle.kts
init.gradle.kts
The Kotlin DSL doesn't differ to the Groovy DSL in where to put things. pluginManagement need to go in to the settings file, so for Kotlin that would be settings.gradle.kts. If you are in doubt, look at the documentation. For almost all code examples, you can switch between Groovy and Kotlin DSL to see how to do it (and which files they are supposed go to into).

Plugin with id 'com.wonbin.myplugin' not found

I wrote a demo for learning gradle for android . i used the JAR
1) project root dir build.gradle
buildscript {
repositories {
jcenter()
flatDir {dirs 'build_libs'}
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
classpath 'com.wonbin.myplugin:MyPlugin:1.0'
}
}
apply plugin: 'com.wonbin.myplugin'
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
I have leanrd that the plugin id is the properties filename in the plugin project. But still when I run ./gradlew assemble , it happens:
Build file '/home/wonbin/MyApp/build.gradle' line: 13
What went wrong: A problem occurred evaluating root project 'MyApp'.
Plugin with id 'com.wonbin.myplugin' not found.
what should i do ?
Probably your build_dir doesn't correspond to the correct file structure. You can reference your jar in the classpath directly instead, like this:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
classpath file('/path/to/your/plugin.jar')
}
}
apply plugin: 'com.wonbin.myplugin'

Gradle: How can I configure repositories in buildscript block of external script

In external script common/buildversion.gradle I have:
buildscript {
// Copy repositories definitions from this buildscript to all projects
(allprojects*.repositories + [repositories]).each {
it.configure {
apply from: rootProject.file('../../common/repositories.gradle')
}
}
dependencies { classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:1.2" }
}
apply plugin: org.sonarqube.gradle.SonarQubePlugin
In common/repositories.gradle:
repositories{
maven { url "https://plugins.gradle.org/m2/" }
}
I am getting error:
Cannot resolve external dependency
sonarqube-gradle-plugin because no repositories are
defined.
Here is a way to have a single source for repositories definitions.
declare the repositories as an "ext" variables
// repositories.gradle
ext.repos = {
maven {
name "repo1"
url "repo1_url"
}
maven {
name "repo2"
url "repo2_url"
}
}
"apply" in your build.gradle
// build.gradle
apply from: "${project.projectDir}/repositories.gradle"
use variable in repositories declaration
// build.gradle
repositories repos
Externalizing sections of the buildScript block into other scripts is not supported. There is an open defect, you should vote on it.

Generate local maven repository with all dependencies using Gradle

What I currently have
dependencies {
compile ...
}
task copyDependencies(type:Copy) {
from configurations.compile
into 'build/dependencies/'
}
This task copies all the required dependencies to the build/dependencies/ directory. Inside the directory, it looks as follows:
/dep1-1.0.jar
/dep2-1.0.aar
...
So essentially what is known as flatDir in gradle terms.
What I'd like to have
It's a local maven repository with all these dependencies, instead of a flatDir.
If I understand your question correctly, you just want to publish to a local directory as if it were mavenLocal() but in a location you specify.
In that case, I believe you just need:
apply plugin: 'maven-publish'
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
repositories {
maven {
url "/path/to/wherever"
}
}
}
See the Maven Publish plugin docs

Gradle Multiple Maven Repo

I added a dependency to my build.gradle: spring-data-neo4j
It requires neo4j-cypher-dsl-2.0.1.jar/pom, which is only located in the repo: https://repo1.maven.org/maven2/.
However as per the output below gradle never looks at this repo for the artifact. How can I get gradle to search this repo as well.
//build.gradle
buildscript {
repositories {
mavenCentral()
maven {
url "https://repo1.maven.org/maven2/"
}
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.1.9.RELEASE")
}
}
gradle build:
FAILURE: Build failed with an exception.
What went wrong: Could not resolve all dependencies for configuration ':compile'.
Could not find org.neo4j:neo4j-cypher-dsl:2.0.1. Searched in the following locations:
https://repo1.maven.org/maven2/org/neo4j/neo4j-cypher-dsl/2.0.1/neo4j-cypher-dsl-2.0.1.pom
https://repo1.maven.org/maven2/org/neo4j/neo4j-cypher-dsl/2.0.1/neo4j-cypher-dsl-2.0.1.jar Required by:
**:feedserver:1.0.0 > org.springframework.data:spring-data-neo4j:3.2.2.RELEASE**
* Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
Edit:---------------------------------------
Sorry I accidently posted the incorrect build.gradle contents above, which has repeated maven central locations. This is my actual build.gradle file...When I build using these settings I still get the error above:
buildscript {
repositories {
mavenCentral()
maven {
url "http://m2.neo4j.org/content/repositories/releases/"
}
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.1.9.RELEASE")
}
}
neo4j-cypher-dsl is not in Maven central. It is available in Neo4j repository you have to add another repo something like this:
repositories {
maven {
url "http://m2.neo4j.org/content/repositories/releases/"
}
}
NOTE is not necessary to use another maven ponting to maven central using mavenCentral() is enough
EDIT 1
repositories section in buildscript just works for dependencies inside. In this case for spring-boot-gradle-plugin
buildscript {
ext {
springBootVersion = '1.1.9.RELEASE'
}
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
In your case you want to add another dependencies for your project. So you need to add another repositories section out of buildscript
repositories {
mavenLocal()
mavenCentral()
maven {
url "http://m2.neo4j.org/content/repositories/releases/"
}
}

Resources