How to list the configured repositories? - gradle

How can I list all the repositories configured for a project?
Background: I have a pretty complex gradle build script and cannot get my NetBeans to download the sources for maven dependencies. In that issue-report I was suggested to double check the order in which mavenCentral is being imported.

For anyone interested, here is the code to list the loaded repositories (thanks #kelemen):
task listrepos {
doLast {
println "Repositories:"
project.repositories.each { println "Name: " + it.name + "; url: " + it.url }
}
}
After adding this code to the build script, execute gradle listrepos and voilĂ ...

If someone comes to this page looking for the Kotlin (build.gradle.kts) equivalent of #Alberto's answer, it would be done as follows:
tasks.register("listrepos") {
doLast {
println("Repositories:")
project.repositories.map{it as MavenArtifactRepository}
.forEach{
println("Name: ${it.name}; url: ${it.url}")
}
}
}
Just as a heads up, the cast to a MavenArtifactRepository is required in the Kotlin version to get the url property. This could be different for you if you are not adding Maven Repositories.

Trying to use Alberto's task from his answer I was getting the following error as in my case I had a Plugin repository defined:
No such property: url for class: org.gradle.plugin.use.internal.PluginDependencyResolutionServices$PluginArtifactRepository
To avoid this error I have changed the task logic a bit:
task listrepos {
doLast {
println "Repositories:"
project.repositories.each {
if (it.name == '__plugin_repository__Gradle Central Plugin Repository') {
println "Name: " + it.name + "; url: " + it.url
} else {
println "Name: " + it.displayName
}
}
}
}

Related

Pull Dependency version from Spring-dependency-management plugin

I have a Gradle Task to pull the dependencies from a project and play with that data. Below is my Gradle Task
task gavValidation() {
doLast {
project(':app').configurations.each {
configurationType ->
println "configurationType >>> "+configurationType.name
configurationType.allDependencies.each {
gav ->
println gav.group+" : "+gav.name+" : "+gav.version
}
}
}
}
The print statement always come as null when it is printing gav.version above.
What i found is that, the versions of these dependencies are maintained in Spring Dependency Management Plugin. Below is the snippet
apply plugin: 'io.spring.dependency-management'
dependencyManagement {
imports {
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Edgware.RELEASE'
mavenBom 'io.pivotal.spring.cloud:spring-cloud-services-dependencies:1.5.0.RELEASE'
mavenBom 'org.springframework.boot:spring-boot-dependencies:1.5.13.RELEASE'
}
dependencies {
dependency 'io.springfox:springfox-swagger2:2.8.0'
dependency 'io.springfox:springfox-swagger-ui:2.8.0'
}
}
How to get the version in my custom-task ? that is currently coming as null
As explained in Working with Dependencies , the methods Configuration.getDependencies() or Configuration.getAllDependencies() only return the declared dependencies and do not trigger dependency resolution. So for dependencies coming from Spring BOM, the version is not known yet.
You can use Configuration.getResolvedConfiguration() method instead, as follows:
task gavValidation() {
doLast {
configurations.each { configurationType ->
println " ***************** configurationType >>> " + configurationType.name
if (configurationType.canBeResolved) {
configurationType.getResolvedConfiguration().getResolvedArtifacts().each { artefact ->
ModuleVersionIdentifier id = artefact.getModuleVersion().getId()
println id.group + " : " + id.name + " : " + id.version
}
}
}
}
}

variable is read-only when it shouldn't be

When i execute this in my script
artifacts = ['abc123-com', 'abc123-ejb', 'abc123-spec', 'abc123-war', 'abc123-war2']
task clone_workspace() << {
for (item in artifacts) {
println item
}
}
i get
> Cannot set the value of read-only property 'artifacts' on root project 'abc123'.
I have tried scoping to project with project.artifacts, and to ext with project.ext.artifacts.
what am I doing wrong here?
The following minimalist Gradle file illustrates that project already has a property for artifacts (documented here):
println "TRACER : " + project.artifacts.class
output:
bash$ gradle
TRACER : class org.gradle.api.internal.artifacts.dsl.DefaultArtifactHandler_Decorated
By contrast, this version of the original is happier:
def myArtifacts = ['abc123-com', 'abc123-ejb', 'abc123-spec', 'abc123-war', 'abc123-war2']
task clone_workspace() << {
for (item in myArtifacts) {
println item
}
}

whenReady not setting gradle property before task runs

I have a a version property in my gradle.properties file that gives the version I am building. I have a task in the build called release that if present in the task graph will upload to the snapshot repo. However what is happening is that even though I include the release task in the build tasks, snapshot is not appended to my version property when uploadArchives runs so it attempts to upload to the wrong repository and fails. The when ready runs, but it does not seem to run before uploadArchives. Can anyone explain what is happening here?
uploadArchives {
repositories {
ivy {
credentials {
username nexusUser
password nexusPassword
}
if (version.endsWith("-SNAPSHOT")) {
url nexusSnapshotRepository
} else {
url nexusReleaseRepository
}
}
}
}
gradle.taskGraph.whenReady {taskGraph ->
if (!taskGraph.hasTask(release)) {
version = version + '-SNAPSHOT'
}
println "release task not included - version set to $version"
}
task release(){
doLast{
println "Releasing"
}
}
This is very similar to the example on the gradle site so I don't see what is going wrong.
http://www.gradle.org/docs/current/userguide/tutorial_using_tasks.html
The script is checking the project.version value in the configuration phase (not when the task executes), but only modifying it after the task execution graph has been built. One way to fix this is to override the repository url from inside the taskGraph.whenReady callback:
uploadArchives {
repositories {
ivy {
name "nexus"
url nexusReleaseRepository
...
}
}
}
gradle.taskGraph.whenReady { taskGraph ->
if (!taskGraph.hasTask(release)) {
version += '-SNAPSHOT'
uploadArchives.repositories.nexus.url nexusSnapshotRepository
// ps: println has to go inside here
}
}

(No such file or directory) how to make non existant file to be ignored by gradle

when i run the command 'gradle tasks' or anything for that fact, i got the following error:
Caused by: java.io.FileNotFoundException: /Users/maxit/workspace/Backbone/modules/contact-form/public/build/contact-src.js (No such file or directory)
Here is my gradle build file:
configurations {
sshAntTask
}
dependencies {
sshAntTask 'org.apache.ant:ant-jsch:1.7.1', 'jsch:jsch:0.1.29'
}
// Pull the plugin from Maven Central
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.eriwen:gradle-js-plugin:1.5.0'
}
}
// Invoke the plugin
apply plugin: 'js'
apply plugin:'base'
def appName = "some"
def version = "0.0.1"
def jsSrcDir = 'public/js'
javascript.source {
dev {
js {
srcDir jsSrcDir
include "*.js"
exclude "*.min.js"
}
}
prod {
js {
srcDir jsSrcDir
include "*.min.js"
}
}
}
task combineSrc(type: com.eriwen.gradle.js.tasks.CombineJsTask) {
source = ["${projectDir}/public/templates/templates.js","${projectDir}/public/js/models/contact_model.js", "${projectDir}/public/js/views/contact_form_view.js", "${projectDir}/public/js/app.js" ]
dest = file("${projectDir}/public/build/${appName}-src.js")
}
task appendJQuery(dependsOn: 'combineSrc') {
String backboneSrc = file(new File("${projectDir}/public/build/${appName}-src.js")).text
new File("${projectDir}/public/build/${appName}-jqueryWraped.js").withWriter{ out ->
out << "(function(\$){" + file("${projectDir}/public/build/${appName}-src.js").text + "})(jQuery); \n"
}
}
It appears, that gradle doesn't ignore a file that is none existant. The task 'combineSrc' hasn't been run, yet to create the file....and i am unable to run the task 'cobineSrc' to create the file in the first place. Its kind a dead end. what am i doing wrong and how to make this work? Thank you
The reason you're failing is, that all the stuff you currently doing during the configuration of the appendJQuery task should be done in the execution phase.
just refactor your appendJQuery task to do:
task appendJQuery(dependsOn: 'combineSrc') {
doLast{
String backboneSrc = file(new File("${projectDir}/public/build/${appName}-src.js")).text
new File("${projectDir}/public/build/${appName}-jqueryWraped.js").withWriter{ out ->
out << "(function(\$){" + file("${projectDir}/public/build/${appName}-src.js").text + "})(jQuery); \n"
}
}
}
hope that helps!
René

Is there a simple way to configure a Gradle plugin immediately if it exists or later if it's added?

I sometimes use apply from: 'some/common/config.gradle' and would like an easy way to configure a related plugin immediately if it already exists or to watch for it to be added and configure it later. I can do what I want, but it seems a bit messy since I end up copy / pasting the config into two spots. The below does what I want, but I'm wondering if there's a better way.
def configured = false;
if(plugins.hasPlugin(ApplicationPlugin)) {
run.classpath.add(configurations.jfxrt)
startScripts {
mainClassName = "com.javafx.main.Main"
doLast {
logger.warn(":${project.name}:startScripts:!! WARNING !!" +
" Replaced mainClassName with com.javafx.main.Main")
}
}
configured = true
}
else {
plugins.whenPluginAdded { plugin ->
if(plugins.hasPlugin(ApplicationPlugin) && !configured) {
run.classpath.add(configurations.jfxrt)
startScripts {
mainClassName = "com.javafx.main.Main"
doLast {
logger.warn(":${project.name}:startScripts:!! WARNING !!" +
" Replaced mainClassName with com.javafx.main.Main")
}
}
configured = true
}
}
}
The way to achieve this is plugins.withType(ApplicationPlugin) { ... }.

Resources