Fail the Gradle build when dependencies are not available - gradle

build.gradle (unnecessary parts were ommited):
apply plugin: 'java'
repositories {
mavenCentral()
maven {
credentials {
username "$mavenUser"
password "$mavenPassword"
}
url "http://localhost:8081/nexus/content/groups/public"
}
}
dependencies {
compile("com.example:some-lib:1.0.0-RELEASE")
}
Assume that the defined dependency is missing in configured Maven repository. When ./gradlew clean build tasks are executed the application is built successfully, although the required dependencies are missing.
Is there a way to configure Gradle to fail if there are unresolved dependencies?
Relates to:
How to make Gradle fail the build if a file dependency is not found? - Solution provided there is not applicable.

Consider this build.gradle (note: intentionally bogus jar specified in dependencies):
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile("junit:junit:4.12")
compile("junit:junitxyz:51.50")
}
task checkDependencies() {
doLast {
configurations.compile.each { file ->
println "TRACER checking: " + file.name
assert file.exists()
}
}
}
compileJava.dependsOn checkDependencies
example output:
$ gradle -q clean compileJava
FAILURE: Build failed with an exception.
[snip]
* What went wrong:
Execution failed for task ':checkDependencies'.
> Could not resolve all files for configuration ':compile'.
> Could not find junit:junitxyz:51.50.
Searched in the following locations:
- https://repo.maven.apache.org/maven2/junit/junitxyz/51.50/junitxyz-51.50.pom
- https://repo.maven.apache.org/maven2/junit/junitxyz/51.50/junitxyz-51.50.jar

Related

Why can't Gradle resolve org.connectbot.jbcrypt:jbcrypt:1.0.0 from the Maven Central Repository?

I'm using Gradle 6.9 and here is my build.gradle file:
plugins {
id "groovy"
id "java"
}
group "com.matthiasdenu"
version "1.0-SNAPSHOT"
repositories {
mavenCentral()
maven {
url 'https://repo.jenkins-ci.org/releases/'
}
}
ext {
jobDslVersion = "1.77"
jenkinsVersion = "2.252"
}
sourceSets {
jobs {
groovy {
srcDirs "jobs"
compileClasspath += main.compileClasspath
}
compileClasspath += sourceSets.main.output
runtimeClasspath += sourceSets.main.output
}
}
dependencies {
compile("org.jenkins-ci.main:jenkins-war:${jenkinsVersion}"){
// https://github.com/sheehan/job-dsl-gradle-example/issues/87
exclude group: "org.jenkins-ci.ui", module: "bootstrap"
}
}
test {
useJUnitPlatform()
}
This is the error message I'm getting:
Execution failed for task ':compileTestGroovy'.
> Could not resolve all files for configuration ':testCompileClasspath'.
> Could not find org.connectbot.jbcrypt:jbcrypt:1.0.0.
Searched in the following locations:
- https://repo.maven.apache.org/maven2/org/connectbot/jbcrypt/jbcrypt/1.0.0/jbcrypt-1.0.0.pom
- https://repo.jenkins-ci.org/releases/org/connectbot/jbcrypt/jbcrypt/1.0.0/jbcrypt-1.0.0.pom
Required by:
project : > org.jenkins-ci.main:jenkins-war:2.252 > org.jenkins-ci.main:jenkins-core:2.252
Possible solution:
- Declare repository providing the artifact, see the documentation at https://docs.gradle.org/current/userguide/declaring_repositories.html
What's odd is that the 1.0.0 artifact doesn't show up at https://repo.maven.apache.org/maven2/org/connectbot/jbcrypt/. I also noticed that the urls don't quite match either. Like if I try to get v1.0.1 it doesn't resolve either because it expects an extra "jbcrypt" for the group name.
I have this problem even when using the latest jenkins-war release (2.304).
What's going on?
You have to add the Jenkins public repository to your configuration. It contains all libraries available in the releases repository and all required dependencies.
The file exists: https://repo.jenkins-ci.org/public/org/connectbot/jbcrypt/jbcrypt/1.0.0/jbcrypt-1.0.0.jar
repositories {
mavenCentral()
maven {
url 'https://repo.jenkins-ci.org/public/'
}
}

Multiple Gradle Files in the same project using apply from

I'm trying to segregate the gradle tasks to respective gradle files.
build.gradle
plugins {
id 'org.openapi.generator' version '4.3.1'
}
apply from: "$projectDir/gradle/script/openapi.gradle"
openapi.gradle
task buildSampleClient(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {
generatorName = "spring"
inputSpec = "$rootDir/src/main/resources/sample.yaml".toString()
outputDir = "$buildDir/generated".toString()
modelPackage = "com.sample"
}
When gradle build is run, getting this error
A problem occurred evaluating script.
Could not get unknown property 'org' for root project 'sample' of type org.gradle.api.Project.
But If I move the content of openapi.gradle into build.gradle it works fine.
Not sure what is the issue, could anyone help here please?
You should add plugin dependencies before your task definition to your openapi.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "org.openapitools:openapi-generator-gradle-plugin:${openapiPluginDependencyVersion}"
}
}
apply plugin: "org.openapi.generator"
// your task goes here
task buildSampleClient(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {
...
}
gradle.properties:
openapiPluginDependencyVersion=4.3.0

How can I disable cache-redirector of `org.jetbrains.intellij` for some dependencies?

I want to introduce com.alibaba:fastjson:1.2.58 in my plugin project, but when gradle resolve dependencies, the org.jetbrains.intellij plugin redirect the download request to https://cache-redirector.jetbrains.com/www.jetbrains.com/intellij-repository/releases/com/alibaba/fastjson/1.2.58/fastjson-1.2.58.pom, which is not exists.
How can I force gradle download this dependency from mavenCentral ?
Here is my build.gradle:
buildscript {
repositories {
mavenCentral()
maven {
url "https://oss.sonatype.org/content/repositories/snapshots/"
}
maven {
url 'https://dl.bintray.com/jetbrains/intellij-plugin-service'
}
}
}
plugins {
id "org.jetbrains.intellij" version "0.4.4"
}
dependencies {
implementation 'com.alibaba:fastjson:1.2.58'
}
apply plugin: 'idea'
apply plugin: 'org.jetbrains.intellij'
apply plugin: 'java'
sourceCompatibility = 1.8
targetCompatibility = 1.8
intellij {
version ideaVersion
updateSinceUntilBuild false
plugins = [
"com.jetbrains.php:${phpPluginVersion}",
"com.jetbrains.php.blade:${bladePluginVersion}",
'CSS',
'java-i18n',
'properties',
'git4idea'
]
pluginName 'tw tools'
}
patchPluginXml {
sinceBuild '173'
}
group 'com.baiguiren'
version '1.2'
wrapper {
gradleVersion '5.4.1'
}
Output of ./gradlew clean && ./gradlew build:
BUILD SUCCESSFUL in 0s
1 actionable task: 1 up-to-date
FAILURE: Build failed with an exception.
* What went wrong:
Could not determine the dependencies of task ':prepareSandbox'.
> Could not resolve all files for configuration ':runtimeClasspath'.
> Could not find com.alibaba:fastjson:1.2.58.
Searched in the following locations:
https://cache-redirector.jetbrains.com/www.jetbrains.com/intellij-repository/releases/com/alibaba/fastjson/1.2.58/fastjson-1.2.58.pom
https://cache-redirector.jetbrains.com/www.jetbrains.com/intellij-repository/releases/com/alibaba/fastjson/1.2.58/fastjson-1.2.58.jar
file:/Users/ruby/.gradle/caches/modules-2/files-2.1/com.jetbrains.intellij.idea/ideaIU/2018.3.5/3abea719ce307bc4f573cba4e3d86f4e35cab07/ideaIU-2018.3.5/com.alibaba/fastjson/1.2.58/ivy-1.2.58.xml
file:/Users/ruby/.gradle/caches/modules-2/files-2.1/com.jetbrains.intellij.idea/ideaIU/2018.3.5/3abea719ce307bc4f573cba4e3d86f4e35cab07/ideaIU-2018.3.5/fastjson-1.2.58-withKotlin-withSources.xml
file:/Users/ruby/.gradle/caches/modules-2/files-2.1/com.jetbrains.intellij.idea/ideaIU/2018.3.5/3abea719ce307bc4f573cba4e3d86f4e35cab07/ideaIU-2018.3.5/com.alibaba/fastjson/1.2.58/fastjson-1.2.58.jar
file:/Users/ruby/.gradle/caches/modules-2/files-2.1/com.jetbrains.intellij.idea/ideaIU/2018.3.5/3abea719ce307bc4f573cba4e3d86f4e35cab07/ideaIU-2018.3.5/fastjson.jar
file:/Users/ruby/.gradle/caches/modules-2/files-2.1/com.jetbrains.intellij.idea/ideaIC/2018.3.5/78cbcd517ec112fbb0c7b45b00b464c1aa6371f3/fastjson-1.2.58-[classifier].jar
https://cache-redirector.jetbrains.com/plugins.jetbrains.com/maven/com/alibaba/fastjson/1.2.58/fastjson-1.2.58.pom
https://cache-redirector.jetbrains.com/plugins.jetbrains.com/maven/com/alibaba/fastjson/1.2.58/fastjson-1.2.58.jar
file:/Users/ruby/.gradle/caches/modules-2/files-2.1/com.jetbrains.intellij.idea/com.alibaba/fastjson-1.2.58.xml
file:/Users/ruby/.gradle/caches/modules-2/files-2.1/com.jetbrains.intellij.idea/ideaIU/2018.3.5/3abea719ce307bc4f573cba4e3d86f4e35cab07/ideaIU-2018.3.5/plugins/fastjson/fastjson.jar
file:/Users/ruby/.gradle/caches/modules-2/files-2.1/com.jetbrains.intellij.idea/fastjson-1.2.58/fastjson.jar
file:/Users/ruby/.gradle/caches/modules-2/files-2.1/com.jetbrains.intellij.idea/ideaIC/2018.3.5/78cbcd517ec112fbb0c7b45b00b464c1aa6371f3/fastjson-2018.3.5.jar
Required by:
project :
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
* Get more help at https://help.gradle.org
I have tried to Invalid caches and restart, but not work.
I even tried to remove org.jetbrains.intellij plugin, but it's required by the project.
There is nothing wrong with cache-redirector, it's just one of the repositories registered in the project, Gradle checks them all.
Try to add the mavenCentral repo to your build.gradle (at the moment it's defined for buildscript only):
repositories {
mavenCentral()
}

gradle Could not resolve all dependencies for configuration ':compile'

I have a dependencies problem I need help with.
I can build EGLSource fin on its own.
But when i try to build EGL2JS then I get this error:
Error message:
:compileJava
FAILURE: Build failed with an exception.
* What went wrong:
Could not resolve all dependencies for configuration ':compile'.
> Could not find :swt-64:.
Searched in the following locations:
https://repo1.maven.org/maven2//swt-64//swt-64-.pom
https://repo1.maven.org/maven2//swt-64//swt-64-.jar
Required by:
:EGL2JS:unspecified > EGL2JS:EGLSource:unspecified
Build and settings files for the two projects: EGLSource and EGL2JS.
EGL2JS: settings.gradle
include ':EGLSource'
project(':EGLSource').projectDir = new File(settingsDir, '../EGLSource')
EGL2JS: build.gradle
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile project(':EGLSource')
}
sourceSets {
main {
java.srcDirs = ['src', 'target/generated-sources']
}
}
EGLSource: build.gradle
apply plugin: 'java'
repositories {
flatDir {
dirs 'lib'
}
}
dependencies {
compile name: 'swt-64'
}
sourceSets {
main {
java.srcDirs = ['src', 'target/generated-sources/antlr4']
}
}
Why is EGL2JS complaining about a dependency in EGLSource?
I could add the swt-64.jar to EGL2JS. But EGL2JS does not directly depend on swt-64.jar so I don't like that solution.
Are there other ways to resolve this dependency?
For reasons I don't understand this makes a difference.
Removing flatFile from repositories
and changing dependencies
from:
compile name: 'swt-64'
to:
dependencies {
compile fileTree(dir: 'lib', include: 'swt-64.jar')
}
Also gradle dependencies not longer shows swt-64 failed.

Gradle: Cannot configure artifactory from an external build script

I am new to gradle and would like to access my artifactory repository from it. If I put all configurations into one build script, the build succeeds. Here are the relevant parts of my build.gradle:
allprojects {
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'artifactory'
}
// ...
buildscript {
repositories {
maven {
url 'http://repo.jfrog.org/artifactory/gradle-plugins'
}
maven {
url artifactory_contextUrl + 'plugins-release'
credentials {
username = artifactory_user
password = artifactory_password
}
}
}
dependencies {
classpath(group: 'org.jfrog.buildinfo', name: 'build-info-extractor-gradle', version: '2.0.16')
}
}
artifactory {
contextUrl = artifactory_contextUrl
publish {
repository {
repoKey = 'libs-release-local'
username = artifactory_user
password = artifactory_password
maven = true
}
}
resolve {
repository {
repoKey = 'libs-release'
username = artifactory_user
password = artifactory_password
maven = true
}
}
}
dependencies {
// My dependencies ...
}
// Rest of the build script ...
Now, I would like to pull out the artifactory part into a separate gradle script for better organization. This is where the build goes wrong. Quite surprisingly, I get the following error even if I copy my build.gradle to foo.gradle, and change build.gradle to just contain the single line
apply from: 'foo.gradle'
The error is
FAILURE: Build failed with an exception.
* Where:
Script '/path/to/my/project/foo.gradle' line: 5
* What went wrong:
A problem occurred evaluating script.
> Plugin with id 'artifactory' not found.
In case this is not a bug, can anyone please explain this behavior of gradle's apply from and propose a solution?
Thank you
The apply from part is parsed once the build script is already configured, so telling Gradle where to find the plugins with specific ID is too late. You'll have to keep the buildscript part in the script, or put it in the init script:
apply from : 'http://link.to/my/gradle.script'
You can also use the fully qualified class name to apply the plugins in your helper script:
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath "com.adaptc.gradle:nexus-workflow:0.5"
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:2.2.4"
}
}
apply plugin: org.jfrog.gradle.plugin.artifactory.ArtifactoryPublicationsPlugin
apply plugin: com.adaptc.gradle.nexusworkflow.NexusWorkflowPlugin
Note that Gradle won't find the plugins if you put quotes around the class name, as you would do normally with plugin names.
This is how I found the class name for the Artifactory plugin:
I downloaded the plugin which was thankfully open source.
I searched for the name of the plugin among the files and found
artifactory-puplish.properties.
It contained the following
property: implementation-class=org.jfrog.gradle.plugin.artifactory.ArtifactoryPublicationsPlugin
The source of nexus-workflow has no such properties file so I looked around until I found
plugins-gradle-master/nexus-workflow/src/main/groovy/com/adaptc/gradle/nexusworkflow/NexusWorkflowPlugin.groovy

Resources