Is there a Gradle task to download testRuntime dependencies? - gradle

Is there a command that will instruct Gradle to resolve and download all testRuntime dependencies, but not run the tests?
Preferably, I want to do this without writing a custom task (such that the command can be run against any Gradle project).
For example, if my build.gradle has this dependency:
dependencies {
// ...
testRuntime "org.seleniumhq.selenium:selenium-htmlunit-driver:2.47.1"
}
The JAR files associated with selenium-htmlunit-driver are not downloaded until I run gradle test, which also runs the tests. I can download all other dependencies by running gradle testClasses, but not the testRuntime deps.

Put the following in a file called resolve.gradle
gradle.allprojects { project ->
project.task('resolveTestRuntime') {
doLast {
project.configurations.testRuntime.resolve()
}
}
}
Then run resolve.gradle as an init script
gradlew --init-script resolve.gradle resolveTestRuntime

Modifying the answer from Lance Java into a valid Init script, I was able to accomplish this with the following resolve.gradle:
apply plugin:MyInitPlugin
class MyInitPlugin implements Plugin<Gradle> {
#Override
void apply(Gradle gradle) {
gradle.allprojects{ project ->
project.task('resolveTestRuntime') {
doLast {
project.configurations.testRuntime.resolve()
}
}
}
}
}
Then running:
gradlew --init-script resolve.gradle resolveTestRuntime

Related

Gradle test suite on precompiled inherit implementation dependencies

I have a testing suite defined on a precompiled script as follows:
...
testing {
suites {
val integrationTest by registering(JvmTestSuite::class) {
testType.set(TestSuiteType.INTEGRATION_TEST)
}
}
}
...
And I have a multi-module project and the test dependencies inherit the dependencies of the module. Example:
dependencies {
implementation(project(":book"))
}
The project book will be available for the test sources.
I want the same thing to happen to the integrationTest sources. How can I do that?
I don't have to manually have to do in the previous example this:
dependencies {
implementation(project(":book"))
integrationTestImplementation(project(":book"))
}
Thanks

Gradle 7: how to get list of implementation dependencies (including sub) in a task?

I need to have full list of dependencies necessary to run a project (so subdependencies are also important!).
task generateLibsDescriptor() {
doFirst {
configurations.compileClasspath.resolvedConfiguration.resolvedArtifacts.each {
println it
}
}
}
This code works, but there are also compileOnly dependencies listed. I tried to change compileClasspath to implementation, but had an error Resolving dependency configuration 'implementation' is not allowed as it is defined as 'canBeResolved=false'.
Is it possible to have a list of just implementation dependencies (with subdependencies)?
Configuration compileClasspath extends compileOnly and implementation. New config should be created which extends only implementation but resolvable.
configurations {
resolvableImpl.extendsFrom(implementation)
resolvableImpl.canBeResolved(true)
}
task generateLibsDescriptor() {
doFirst {
configurations.resolvableImpl.resolvedConfiguration.resolvedArtifacts.each {
println it
}
}
}

Creating a fat jar using Kotlin and Gradle - compile vs implementation?

I'm tinkering with a simple "hello world" project in Kotlin & Gradle (see below). I've added the "fat jar" collection stuff to bring in the Kotlin dependency, but when I try to run java -jar build/libs/hello-1.0-SNAPSHOT.jar I get the java.lang.NoClassDefFoundError: kotlin/jvm/internal/Intrinsics error because the dependencies aren't available at runtime.
I've solved that problem by changing implementation to compile, which makes everything work fine. But from what I understand, we shouldn't be using compile anymore, and neither api nor implementation makes the "fat jar" collection process work, and as I look at the other options for dependencies I'm not sure which to use.
Question: what's the "right" thing to do in a case like this?
// build.gradle
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.3.41'
}
group 'com.example.test'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
// if I change "implementation" to "compile", running the jar works
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
}
jar {
manifest {
attributes "Main-Class": "ApplicationKt"
}
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
}
// Application.kt
fun main(args: Array<String>) {
println("hello world")
}
The compile dependency is deprecated. You should use implementation to declare your implementation dependencies, and compileClasspath to get all the compilation dependencies.

Import Cucumber with Gradle

I want to import cucumber.api.java.en.* into my groovy files, but cucmber.api will not be recognized as in my classpath. Thus every #Given or #When annotation is not recognized.
When I build with ./gradlew cucumber the .feature file is found and missing snippets are shown in the console. What do I have to include in my build.gradle to add above import into my classpath?
My gradle version is 2.2 and the cucumber related parts of my build.gradle file look like this:
dependencies {
testCompile 'info.cukes:cucumber-java:1.2.4'
testCompile 'info.cukes:cucumber-junit:1.2.4'
}
test {
testLogging.showStandardStreams = true
systemProperties System.getProperties()
}
configurations {
cucumberRuntime {
extendsFrom testRuntime
}
}
task cucumber() {
dependsOn assemble, compileTestJava
doLast {
javaexec {
main = "cucumber.api.cli.Main"
classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
args = ['-f', 'pretty', '--glue', 'gradle.cucumber', 'src/test/resources']
}
}
}
What am I missing?
You include info.cukes:cucumber-java:1.2.4 which is the jar containing the annotations you are missing. They are expected to be available in your test classpath.
To me, it sounds as an issue with your IDE.
If you are using IntelliJ IDEA, try to re-import the project. Click on the two rotating arrows in your Gradle tab and refresh the project.

gradle common code for multiproject build

I have some common class definitions for a multiproject build in folder buildSrc:
root
build.gradle
settings.gradle
a/
build.gradle
b/
build.gradle
buildSrc/
common.gradle
where
settings.gradle says
include ':a', ':b'
a/build.gradle
project(":a") {
task someTask(type: CommonTask) {
println "Running some task"
}
}
buildSrc/common.gradle
class CommonTask extends DefaultTask {
#TaskAction
def someAction() {
println "Running common task"
}
}
According to docs:
https://docs.gradle.org/current/userguide/organizing_build_logic.html
buildSrc project. Drop the source for your build classes into a
certain directory and Gradle automatically compiles them and includes
them in the classpath of your build script.
However when running gradle build I get an error:
Could not find property 'CommonTask' on project ':a'.
So how can we have common class definitions for a multiproject build?
Edit
Adding apply from: "$rootDir/buildSrc/common.gradle" to project a does not help
Edit2
I put the file common.gradle to buildSrc/src/main/groovy/org/gradle/Common.groovy
and in the root projects' build.gradle
repositories {
mavenLocal()
}
dependencies {
compile group: 'org.gradle'
}
A problem occurred evaluating root project 'root'.
Could not find method compile() for arguments [{group=org.gradle}] on root project 'root'.
Edit 3
I have 2 files:
buildSrc/src/main/groovy/com/iggy/gradle/A.groovy:
package com.iggy.gradle
String someA() {
return "a"
}
buildSrc/src/main/groovy/com/iggy/gradle/B.groovy:
package com.iggy.gradle
String someB() {
String a = someA()
return a
}
But I get an error: Could not find method someA()
Shouldn't methods from same package be resolved? Adding #PackageScope or public doesn't help. So how can one import a method from another groovy file?

Resources