Gradle repositories in Gradle settings instead of build? - maven

In Maven, you can store the list of repositories in settings.xml, which means each developer can have his own settings (when you are inside a company, you will use the local Nexus as a mirror, but when outside, you will use Maven Central).
How do you do that with Gradle ?

You can use a gradle init script for that. these init scripts can live in the ~/.gradle/init.d/ folder. Having on (e.g. default-repos.gradle) with adding default repositories can look like this:
allprojects {
repositories {
maven {
url = 'http://nexus.local.org'
}
}
}

Related

Java Spring Boot project error on download gradle dependency

One of my coworkers recently deleted his ".gradle" directory. He was not able to build the project again, because of the following error:
Caused by: org.gradle.api.resources.ResourceException: Could not get resource 'https://repo.spring.io/plugins-release/com/github/node-gradle/gradle-node-plugin/2.2.1/gradle-node-plugin-2.2.1.pom'.
at org.gradle.internal.resource.ResourceExceptions.failure(ResourceExceptions.java:74)
at org.gradle.internal.resource.ResourceExceptions.getFailed(ResourceExceptions.java:57)
at org.gradle.api.internal.artifacts.repositories.resolver.DefaultExternalResourceArtifactResolver.downloadByCoords(DefaultExternalResourceArtifactResolver.java:138)
at org.gradle.api.internal.artifacts.repositories.resolver.DefaultExternalResourceArtifactResolver.downloadStaticResource(DefaultExternalResourceArtifactResolver.java:97)
at org.gradle.api.internal.artifacts.repositories.resolver.DefaultExternalResourceArtifactResolver.resolveArtifact(DefaultExternalResourceArtifactResolver.java:64)
at org.gradle.api.internal.artifacts.repositories.metadata.AbstractRepositoryMetadataSource.parseMetaDataFromArtifact(AbstractRepositoryMetadataSource.java:69)
at org.gradle.api.internal.artifacts.repositories.metadata.AbstractRepositoryMetadataSource.create(AbstractRepositoryMetadataSource.java:59)
at org.gradle.api.internal.artifacts.repositories.resolver.ExternalResourceResolver.resolveStaticDependency(ExternalResourceResolver.java:244)
at org.gradle.api.internal.artifacts.repositories.resolver.MavenResolver.doResolveComponentMetaData(MavenResolver.java:127)
at org.gradle.api.internal.artifacts.repositories.resolver.ExternalResourceResolver$RemoteRepositoryAccess.resolveComponentMetaData(ExternalResourceResolver.java:445)
at org.gradle.api.internal.artifacts.ivyservice.ivyresolve.CachingModuleComponentRepository$ResolveAndCacheRepositoryAccess.resolveComponentMetaData(CachingModuleComponentRepository.java:378)
at org.gradle.api.internal.artifacts.ivyservice.ivyresolve.ErrorHandlingModuleComponentRepository$ErrorHandlingModuleComponentRepositoryAccess.resolveComponentMetaData(ErrorHandlingModuleComponentRepository.java:138)
And I tried to access the URL 'https://repo.spring.io/plugins-release/com/github/node-gradle/gradle-node-plugin/2.2.1/gradle-node-plugin-2.2.1.pom' directly on browser, and it's now asking user and password.
Did somewone known what's happening? Did this repo was moved to another host?
Thanks in advance.
edit: My gradle repositories:
repositories {
mavenLocal()
mavenCentral()
jcenter()
maven { url "https://repo.spring.io/plugins-release" }
maven { url "https://plugins.gradle.org/m2/" }
}
Looks like you are downloading from a jfrog repository and are missing the gradle.properties file ..
The .gradle folder contains a file called "gradle.properties" which contains the credentials (usually encrypted from the above repo in case of employer controlled repo)to access the jars/poms.
The steps to generate and download :
1.Go to https://repo.spring.io/ and login if you have workplace credentials for accessing the repo.
2.search for "plugins-release" is the "set-me-up" scroller.
3. select Gradle in "tools" drop-down and Generate gradle.properties.
4.download this "gradle.properties" and put it in .gradle folder under the username directory.
switch the order of the last 2 lines under repositories. It becomes:
repositories {
mavenLocal()
mavenCentral()
jcenter()
maven { url "https://plugins.gradle.org/m2/" }
maven { url "https://repo.spring.io/plugins-release" }
}
This way Gradle will resolve gradle-node-plugin from the official Gradle plugin portal (https://plugins.gradle.org/m2/), instead of from Spring's Artifactory (which caches 3rd party artifacts, but you don't really want to fetch non Spring plugins from it).
Also see this answer: https://stackoverflow.com/a/22170251/2591231
PS:
I am assuming that in your build.gradle you apply the plugin "com.moowork.node" as shown here under "Using legacy plugin application", and that the repositories block is inside a buildscript block. Otherwise, your repositories block has no effect on plugin resolution.

how to set archiveBaseName for local .m2 repository

I'm trying to upgrade a dependency to a project that will ultimately become a dependency to my project. I've made the upgrade and I want to test it locally before I put it out on the repo to be used. I'm learning Gradle and a few Google searches showed me how to add the project to the settings.gradle file. But the dependency project uses aliases for their dependencies (see build.gradle below).
settings.gradle
include ':TransportImpl'
Changed to:
include ':TransportImpl', ':jeromq'
project(':jeromq').projectDir = new File("../zeromq/jeromq")
build.gradle
//project.ext.set("JEROMQ", 'THIRD-PARTY:jeromq:0.4.2')
project.ext.set("JEROMQ", ':jeromq')
If I uncomment the original line (shown commented above), because that apk is in the repo it gets recognized. I'm guessing that this only works for external libraries.
Other things I have tried:
//project.ext.set("JEROMQ", 'C:/Users/username/.m2/repository/THIRD_PARTY/jeromq/0.5.1-SNAPSHOT/jeromq-0.5.1-SNAPSHOT-jeromq.jar')
//project.ext.set("JEROMQ", 'C:\\Users\\username\\.m2\\repository\\THIRD_PARTY\\jeromq\\0.5.1\\jeromq-0.5.1-jeromq.jar')
//implementation filetree(dir: 'C:\\Users\\username\\.m2\\repository\\THIRD_PARTY\\jeromq\\0.5.1', include:['jeromq-0.5.1-jeromq.jar'])
Can anyone give me a tip on how I can assign a variable that points to the local repository and use that variable to set an archiveBaseName?
New Information:
gradle.build for our jeromq project
apply plugin : 'maven'
apply plugin : 'maven-publish'
// Top-level build file where you can add configuration options common to all sub-projects/modules.
ext {
// Nexus paths
nexusUrl='https://nexus.path'
Releases='/Private_Releases'
nexusUsername = project.findProperty("nexusUsername") ?: (System.getenv("NEXUS_USERNAME") ?: "user_name"
nexusPassword = project.findProperty("nexusPassword") ?: (System.getenv("NEXUS_PASSWORD") ?: "password")
// Project versions
jeromqVersion = "0.5.1-SNAPSHOT"
}
allprojects {
// Read only repositories for dependencies; this should never be used to publish
repositories {
mavenCentral()
jcenter()
}
}
The project that uses it as a dependency finds it using the following from its build.gradle file:
// Create aliases for dependencies
project.ext.set("EASY_MOCK", 'Test:easymock:3.5.1')
project.ext.set("OBJENESIS", 'Test:objenesis:2.6')
// **************** HERE ***************************
// THIRD-PARTY is configured to look on the nexus server
project.ext.set("JEROMQ", 'THIRD-PARTY:jeromq:0.4.2') ... or 0.5.1 or 0.5.1-SNAPSHOT ...
allprojects {
// Read only repositories for dependencies; this should never be used to publish
repositories {
mavenCentral()
mavenLocal()
// maven {
// // trying to add my local repo,
// // BUT this still does not change where THIRD-PARTY is pointing to
// url 'file://C:/Users/me/.m2/repository/THIRD_PARTY/jeromq/0.5.1-SNAPSHOT/jeromq-0.5.1-SNAPSHOT-jeromq.jar'
// }
maven {
name 'ReleasesName'
url "$nexusUrl$ReleasesName
}
}
maven {
name 'ReleasesNameSnapshots'
url "$nexusUrl$ReleasesNameSnapshots"
credentials {
username "${rootProject.ext.nexusReadOnlyUsername}"
password "${rootProject.ext.nexusReadOnlyPassword}"
}
}
jcenter {
url "https://jcenter.bintray.com/"
}
}
The only reason I need the alias for that dependency is because it is used in other places.
I'm not entirely sure what you are asking, but I think what you are trying is completely off.
The build you are trying to include is a Maven build, not a Gradle build, so it is unlikely you can simply treat it as it were a Gradle build.
And even if it were a Gradle build, including it like you did would not be the right way. How you tried it is for including multiple projects of a multi-project build, not including external libraries.
If it were a Gradle build, you would use a composite build, which effectively replaces a declared binary dependency by the build output of a "sub-build". But afair this only works cleanly with a Gradle build.
Why don't you simply mvn install your modified jeromq version, add mavenLocal() to your dependencies and depend on that just installed version? That would be the usual way for locally testing new Maven built dependencies.

Gradle configuration to use maven local repository

I have Maven and Gradle both installed in my system.
By default maven stores the dependency jars in the USER_HOME/.m2/repository and Gradle stores in folder USER_HOME/.gradle/caches/modules-2 respectively.
For the same project, I am end up with 2 copies of jars in my system ultimately.
Is there any configuration to set up in the gradle at global level so that it uses existing maven repository instead of its own.
Thanks in advance.
You can use
mavenLocal()
If location of repository must be in other place you can use
repositories {
maven {
url '/path/to/your/repository'
}
}

Gradle: is it possible to publish to Gradle's own local cache?

I know that I can use the maven plugin coupled with mavenLocal() to install an artifact and use it locally.
However investigating this a bit further, I notice that this means the artifacts are installed to Mavens's standard ~/.m2, but at the same time Gradle's own cache lives under ~/.gradle/caches in a different format.
This seems wasteful to me, a) working with two local caches, and b) having to add mavenLocal() to all projects. I was wondering if there is a way to publish an artifact to Gradle's ~/.gradle/caches ?
Note that the local Maven repository is not (really) a cache, and that the Gradle cache is not a repository. Gradle uses its cache only to cache remote artifacts, it should not copy artifacts retrieved from local Maven repositories there. In turn, you cannot publish artifacts to the Gradle cache.
So the approach to publish to and use mavenLocal() should not be as wasteful as you think. Also, you do not have to add mavenLocal() to all projects of a multi-project separately. You can simply use something like allprojects { repositories { mavenLocal() } } in your root project. Or if you want mavenLocal() in all your independent Gradle projects you could even try adding it to ~/.gradle/init.gradle.
Here is an example with code.
As it's not possible to publish into Gradle. The workaround is to publish into the maven and use it in Gradle.
Step 1 publish the code to local maven repository /users/jay/.m2/repository/
Step2 - Use the local maven repo code in another project.
Step #1 (Publish to local maven repo)
apply plugin: 'maven-publish'
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
}
Step #2 (Use the local maven repo in your gradle project)
repositories {
// ..... Other repositories
mavenLocal()
}
dependencies {
compile 'com.jai:myapp:1.0.0'
}

How to set global repositories for gradle that I can use them in each gradle project

It's slow to visit the maven official repositories from my country, so I want to try some local repositories first.
Is it able to add them to some global gradle configuration file, that I can just use them in each gradle project, without modifying the project-scope build.gradle file?
I would suggest to use the init.gradle script which is located in your home folder $HOME/.gradle/init.gradle which might contain the following content:
allprojects {
repositories {
mavenLocal()
maven {
url "http://localhost:8081/nexus/content/groups/public/"
}
}
}
I guess that what are You looking for are init scripts.

Resources