Gradle: how to pass classes defined in script to JavaExec? - gradle

Gradle: how to pass classes defined in script to JavaExec? e.g.,
build.gradle
public class Foo {
...
}
javaexec {
classpath ...
main example.Executor
args "fooClass", "Foo"
}
The class example.Executor needs to load the class Foo.class defined in build.gradle script. Is this possible?
How to add build script in the javaexec classpath?

Related

How to pass #Input String in a task in buildSrc

This custom plugin exists in gradle's buildSrc/:
abstract class MyTask : DefaultTask() {
#get:Input
abstract val buildDir: Property<String>
#TaskAction
fun someTask() {
// do stuff
}
}
class DevelopmentPlugin : Plugin<Project> {
override fun apply(project: Project) {
project.tasks.run {
register("myTask", MyTask::class.java) {
inputs.property("buildDir", project.buildDir)
println(inputs.getProperties())
}
}
}
}
and by running the task with e.g. $ ./gradlew myTask fails with:
Could not determine the dependencies of task ':myTask'.
> Cannot query the value of task ':myTask' property 'rootDir' because it has no value available.
Also the prinln outputs {buildDir=null} meaning that the inputs.property("buildDir", project.buildDir) has failed.
How to pass the project.buildDir value from the Plugin in the task?
Using project.buildDir directly from inside the task is not an acceptable answer due to Gradle's incubating build-cache functionality.
Firstly, there is a class type issue which is not visible in Gradle.
buildDir is of type File while the property is String.
So "${project.buildDir}" should be used.
Secondly, since the property is abstract val it can directly be accessed in the closure. Therefore it can be set with:
// instead of:
inputs.property("buildDir", "${project.buildDir}")
// just this:
buildDir.set("${project.buildDir}")

How to provide the value of a #Nested property of a gradle task?

The gradle doc describes the #Nested annotation for custom gradle tasks:
https://docs.gradle.org/current/userguide/more_about_tasks.html#sec:task_input_output_annotations
Unfortunately, there is no complete example of this mechanism in terms of how it is used in a build.gradle file. I created a project to demonstrate a strange exception that happens whenever gradle configures the project:
https://github.com/NicolasRouquette/gradle-nested-property-test
The build.gradle has the following:
task T(type: NestedTest) {
tool = file('x')
metadata = {
a = "1"
}
}
The NestedTest custom task is in the buildSrc folder:
class NestedTest extends DefaultTask {
#InputFile
public File tool
#Nested
#Input
public Metadata metadata
#TaskAction
def run() throws IOException {
// do something...
}
}
The important bit is the #Nested property whose type is really basic:
class Mlang-groovyetadata {
String a
}
When I execute the following: ./gradlew tasks, I get this:
Build file '/opt/local/github.me/gradle-nested-property-test/build.gradle' line: 26
* What went wrong:
A problem occurred evaluating root project 'gradle-nested-property-test'.
> Cannot cast object 'build_6wy0cf8fn1e9nrlxf3vmxnl5z$_run_closure4$_closure5#2bde737' with class 'build_6wy0cf8fn1e9nrlxf3vmxnl5z$_run_closure4$_closure5' to class 'Metadata'
Can anyone explain what is happening and how to make this work?
Nicolas
Looking at the unit tests in Gradle's source code, I found that the syntax for #Nested properties requires invoking the type constructor in the build.gradle file.
That is, the following works:
task T(type: NestedTest) {
tool = file('x')
metadata = new Metadata(
a: "1"
)
}

Replace java class variable from gradle?

I would like to have a gradle task replace a variable in a java class. This is the java class I want to manipulate:
package com.me.test;
public class Config {
public static final String FLAG = "variable";
}
This is my build.gradle file:
apply plugin: 'java'
...
task specialJar(type: Jar) {
...
// Can I replace Config.FLAG in here somehow?
// Something like:
com.me.test.Config.FLAG = "foo";
}
Thanks

How to set the main class classpath for JavaExec task in Gradle?

I got that if I want to run a main from a Main class, by using the sourceSets.main.runtimeClasspath classpath, I have to put the Main class inside of src/main/java and use something like:
apply plugin: 'java'
dependencies {
}
task myTask (type: JavaExec){
dependsOn classes
classpath sourceSets.main.runtimeClasspath
main = 'Main'
}
What I want is understand how I can specify a different classpath from which to retrieve the class containing the main().
What if I want to run the main from a class which is not in src/main/java but it is in the same folder as the build.gradle?
I'm aware that it has no sense to do something like that, but I wish to find a solution as an exercise to learn Gradle.
As you still need to compile such class and in the case the class is not in the standard src/main/java directory, you will need to define additional SourceSet to that path and use the same approach as you described:
sourceSets {
main {
custom {
srcDirs = ['custom/path']
}
}
}
task myTask (type: JavaExec){
dependsOn classes
classpath sourceSets.custom.runtimeClasspath
main = 'Main'
}

Access property from settings.gradle in Gradle plugin

I am in the process of building a custom Gradle plugin using the following guide. I want to define a property in the settings.gradle file and read it in the plugin class, but every attempt failed so far.
Here is the error I am getting:
Could not find property 'MyProperty' on root Project 'MyProject'
Here is my settings.gradle file:
gradle.ext.MyProperty = "The Value"
and here is the file containing my plugin:
package myplugin.gradle
import org.gradle.api.Project
import org.gradle.api.Plugin
class FirstPlugin implements Plugin<Project> {
void apply(Project project) {
project.task('myTask') << {
def instance = project.MyProperty
println "VALUE : " + instance
}
}
}
I can access MyProperty if I put project.ext.set("MyProperty", "Property Value") in build.gradle, but I can't find a way to set it in the settings.gradle file.
I guess one way to fix this issue would be to read the content of settings.gradle in build.gradle and send it to the plugin using project.ext.set(...), but is there a more direct way?
Apparently, the way to go (which is better anyways) is to use an extension object. This extension object allows to send data from the build.gradle file to the plugin. The build.gradle file reads the content of the variable in the settings.gradle file and puts it in the extension object.
In the file containing the plugin:
package myplugin.gradle
import org.gradle.api.Project
import org.gradle.api.Plugin
class MyExtension {
String myProperty
}
class FirstPlugin implements Plugin<Project> {
void apply(Project project) {
project.extensions.create('myExtensionName', MyExtension)
project.task('myTask') << {
def instance = project.myExtensionName.myProperty
println "VALUE : " + instance
}
}
}
In the build.gradle file:
apply plugin: 'myPluginName'
...
myExtensionName {
myProperty = gradle.ext.myPropertyInSettings
}
And finally, in the settings.gradle file :
gradle.ext.myPropertyInSettings = "The Value"
Also, I recommend this tutorial instead of the one provided by Gradle.
I have a gradle project where a property set in settings.gradle is accessed from 3 places:
settings.gradle itself
build.gradle (including subprojects {} )
a gradle plugin in ./buildSrc
Accessing from settings.gradle and build.gradle:
gradle.ext.MY_PROPERTY_NAME
Accessing from a gradle plugin's apply() method:
(plugin is applied from subprojects {} closure)
project.rootProject.gradle.ext.MY_PROPERTY_NAME
This seems to work fine in Gradle 4.7 and Gradle 5.2

Resources