How do I specify nexus credentials with `gradle init`? - gradle

When running gradle init to facilitate migration from a maven pom to a gradle build, is there a way to specify credentials for a repository that requires them for read-only access (our local nexus repo requires auth even for reads)?
Usually it fails because our parent pom would require read-only authentication to pull down from the local repo, but it doesn't seem like you can specify these credentials until after the build.gradle file already exists, which is counter-intuitive to the gradle init purpose.
Adding them via ~/.gradle/.gradle.properties or through various ~/.gradle/init.gradle methods doesn't seem to work.

What does your ~/.gradle/init.gradle look like? Something like this should work:
allprojects {
repositories {
mavenLocal()
maven {
url 'https://your.nexus/server'
credentials {
username "your_username"
password "your_password"
}
}
}
}

Related

How To use classes by integrating bitbucket repository url in Android Project Without Deploying to Maven Central

I have added the details in the root project build.gradle
maven {
credentials {
username ''
password ''
}
authentication {
basic(BasicAuthentication)
}
url 'https://bitbucket.org/.........git'
}
I am not sure what to do next. If i add implementation 'packagename:artifactid:version' it says repo not found. i did not deploy it to maven central. is it possible to keep it to myself and access the classes because i already added the repository library details above? if so, how?

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.

Gradle repositories in Gradle settings instead of build?

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'
}
}
}

How to provide correct settings for gradle?

Using Jenkins for CI, I need to use hidden credentials for Gradle or maven publishing. My credentials are applied in Jenkins rather than in source code. Maven settings are in a settings.xml, but I would like to define properties in Jenkins. What would I use in a command-line to do this? I imagine
gradle -Dsomething.username=blah -Dsomething.password=secret
There may be a better way but this is how we do it: we define target maven repository in build.gradle using custom project properties and then provide those properties via command line:
uploadArchives {
repositories {
mavenDeployer {
repository(url: "https://your-repo-server.company.com/your-built-artifacts") {
authentication(userName: project.getProperties()['nexusUploadUsername'], password: project.getProperties()['nexusUploadPassword'])
}
}
}
}
gradle -PnexusUploadUsername=blah -PnexusUploadPassword=secret
You can also take one step further and configure them as Jenkins build parameters or environment variables in order to prevent people from seeing them in logs (do not forget to set that 'Mask passwords' checkbox): gradle -PnexusUploadUsername=$JENKINS_NEXUS_USER -PnexusUploadPassword=$JENKINS_NEXUS_PASSWORD.

Is it possible to have multiple repos in a resolve closure for the Artifactory Gradle plugin?

I have not been able to resolve against two repositories (ext-releases-local and repo1-cache) on one Artifactory server. My build seems to only resolve against the last repository in the list and ignore the first one. Basically, I have my project's dependencies in ext-releases-local, and their transitive dependencies can be found in the remote repository called repo1-cache. So I need to resolve against both using the Artifactory Gradle plugin. Here is the setup that I have tried in my build.gradle:
artifactory {
contextUrl = "https://myartifactory.host.com"
publish {
repository {
repoKey = "myproj-releases-local"
username = "${artifactory_deployer_user}"
password = "${artifactory_deployer_password}"
}
}
resolve {
repository {
repoKey = "repo1-cache"
username = "${artifactory_reader_user}"
password = "${artifactory_reader_password}"
}
repository {
repoKey = "ext-releases-local"
username = "${artifactory_reader_user}"
password = "${artifactory_reader_password}"
}
}
}
I would appreciate any help/advice on getting this to work. There isn't any documentation out on jfrog's website to tell whether resolving to two repos is supported or not. It's definitely possible to do it without the Artifactory Gradle plugin, but unfortunately I will be building via Bamboo, and the Artifactory Gradle plugin has become necessary for release management.
From what I can tell, this isn't supported. However, you can always create a virtual repository on the Artifactory side that includes ext-releases-local and repo1-cache. (There is also a predefined virtual repository named /repo that includes all repositories.) As a nice side effect, this will likely perform better than declaring two repositories on the Gradle side.

Resources