gradle object in a gradle build script - groovy

Consider the following method invokation containing in the gradle build script:
gradle.taskGraph.whenReady{taskGraph ->
println gradle.toString()
println "Ready"
}
It prints
build 'task_graph'
Ready
I thought we work in the scope of Project object, since gradle should be a property of that Project object. But there is neither property nor even method with such name. Why can we use it in the build script?

I may be wrong but I think your confusion is that there exists a getGradle() method on the Project interface but no such public field named gradle. This is a Groovy feature. In Groovy, getter and setter methods can by referenced as properties. For example:
println project.description // same as project.getDescription()
project.description = 'My java project' // same as project.setDescription('My java project')
I'd highly suggest familiarizing yourself with Groovy by checking out their documentation. You'll see a lot of differing syntax in Gradle examples simply because there are many different ways to accomplish the same thing in Groovy.

Related

Groovy how can I build a custom library and use it in a project as dependency

I have a set of code procedures I use in a lot of places and I'm trying to basically move it to a library.
So I created my library with some unit test and everything was looking promising and at least working localy..
When I went to my project and deleted the files locally and then try to import them from my library as a dependency the code does not work.
I always get this kind of error
Class does not define or inherit an implementation of the resolved method abstract getProperty(Ljava/lang/String;)Ljava/lang/Object; of interface groovy.lang.GroovyObject.
I'm definitely not an expert on groovy but basically I use it in my Jenkins and Gradle for pipelines and some basic packaging or environment deployments.
I can show my class:
class ConsoleRow implements Comparable {
...
final Integer priority
final String rowStatus
final String message
final String rowReportClass
ConsoleRow(Integer priority, String status, String msg, String rowC) {
this.priority = priority
this.rowStatus = status
this.message = msg
this.rowReportClass = rowC
}
#Override
int compareTo(Object o) {
return this.priority <=> ((ConsoleRow) o).priority
}
The line that gives me the error is this actual compareTo when trying to do the "this.priority"
Caused by: java.lang.AbstractMethodError: Receiver class com.abc.insight.jenkins.ConsoleRow does not define or inherit an implementation of the resolved method abstract getProperty(Ljava/lang/String;)Ljava/lang/Object; of interface groovy.lang.GroovyObject.
at com.abc.insight.jenkins.ConsoleRow.compareTo(ConsoleRow.groovy:24)
at com.abc.insight.jenkins.ConsoleOutputHtmlBuilder.processOutput(ConsoleOutputHtmlBuilder.groovy:115)
at com.abc.insight.jenkins.ConsoleOutputHtmlBuilder.processOutput(ConsoleOutputHtmlBuilder.groovy)
at com.abc.insight.jenkins.ConsoleOutputHtmlBuilder.buildReport(ConsoleOutputHtmlBuilder.groovy:20)
at com.abc.insight.jenkins.ConsoleOutputHtmlBuilder$buildReport.call(Unknown Source)
at build_e548mc0tqjmi822clitlsycdk.runReport(C:\dev\repo\insight\insight-health-check\data-foundation\smoke-test\build.gradle:77)
The calling function is just trying to sort a list of those objects
List<ConsoleRow> outputRows = []
...
return outputRows.sort()
The part that gets me really confused is that if instead of importing the library as a dependency I just do this directly in this repo and put my sources in my buildSrc\src\main\groovy\com\abc\insight the code works fine...
So I really think it might be how I package and publish my library that might be wrong.
I'm really sure this is some basic error on my part because I never did a groovy library before but somehow I can't make it work.
It might be that my publication is just wrong, on my library side I'm using this plugins to do the publishing.
plugins {
id 'groovy'
id 'java-library'
id 'base'
}
publishing {
publications {
maven(MavenPublication) {
from components.java
}
}
}
I tried to change components.groovy but somehow it does not work.
Any ideas or tips, I think my question probably is showing some really lack of know-how on groovy but looking at the documentation and examples I could not figure it out.
Doing some debug in my IDE the compareTo that generates the exception looks like this.
public int compareTo(Object o) {
CallSite[] var2 = $getCallSiteArray();
return ScriptBytecodeAdapter.compareTo(this.priority, var2[0].callGroovyObjectGetProperty((ConsoleRow)ScriptBytecodeAdapter.castToType(o, ConsoleRow.class)));
}
I tried following this guide and code structure when doing moving the code to a library
https://docs.gradle.org/current/samples/sample_building_groovy_libraries.html
Thanks for any feedback
p.s: My code might look weird, I tried first to have everything with the def blablabla but I was having some issues with typecasting but I don't think this would be the reason for the problem I'm facing.
Anyway I got a look at the generated code in my IDE and I see a lot of get methods just no idea where they expected this getProperty from
Ok this was definitely a user error.
I am using distribution version of gradle 6.5.1
When I did the gradle init to bootstrap my project I was provided with the dependency of gradle groovy-all version 2.5.11
implementation group: 'org.codehaus.groovy', name: 'groovy-all', version: '2.5.11'
I thought that was a mistake and just updated to the latest version.
implementation group: 'org.codehaus.groovy', name: 'groovy-all', version: '3.0.9'
Now the problem is that the project in which I'm using the library is also running with gradle 6.5.1 so probably this version missmatch between compiple and usage was causing the problem.
By reverting to the correct version suggested by gradle the problem is gone.

Using Groovy create Project properties in SoapUI

I have 50+ tests in my current SoapUI workspace. I need to add a new project level parameter on all of these tests. Is there a way I can do this using the groovy console within SoapUI?
For example :
testRunner.testCase.testSuite.project.setPropertyValue( "SuperMan",("SuperMan"));
I can run this to create a property within the current working project.
Is there a way I can create this under all projects on my current workspace?
Thanks in advance.
Yes, We can do this but it's little tricky.
Way to do this:
Create a empty generic project in soapui, Add empty testsuite, add Empty testcase
In test case Add a groovy script step
Now paste this groovy code
for(project in com.eviware.soapui.SoapUI.workspace.projectList) {
if(project.open && project.name != testRunner.testCase.testSuite.project.name){
project.setPropertyValue( "TestProperty","TestValue");
}
}
Run the groovy step, you will get property added at Project level.
Please mark this as correct response if this solve your problem.
Using your testRunner access you simply need one level more workspace. From here you can access projects map and iterate over each project adding the property:
testRunner.testCase.testSuite.project.workspace.projects.each{ key, proj ->
proj.setPropertyValue('superman','superman')
}
This code doesn't add the property on closed projects in workspace but not fails accessing it.
Alternatively as #Rao suggest on his comment it's possible to use default it object iterator and access the value instead of defining key, proj -> on each:
testRunner.testCase.testSuite.project.workspace.projects.each{
it.value.setPropertyValue('superman','superman')
}
I prefer the first approach for clarity.
Hope it helps,

How was task gradle implemented to be used in the way that's currently used?

I'm learning gradle and for that reason I began to learn groovy.
I don't understand how gradle or groovy allows me to do this:
task nameOfTask(dependsOn: otherTask) {
//some stuff
}
If task is a class, nameOfTask would need to use the operator new to be instantiated. If task is a method, nameOfTask, I think, would need to be a String.
Please can you post an example code that allows me to reproduce the above code?
Quoting (and contextualizing) the answer to the same question on the gradle discussion forum
the snippet above is resolved to a method call
Project#task("nameOfTask", dependsOn: otherTask)
to get this working with the snippet above, AST transformation is used. See http://www.groovy-lang.org/metaprogramming.html#_compile_time_metaprogramming for more details

How to refer to the values to be declared in build.gradle

I'm totally new to this gradle, teamcity and groovy.
I'm tryign to write a plugin,which will get the value from svninfo. If the developer wants to override the value(in build.gradle) they can override something like this.
globalVariables{
virtualRepo = "virtualRepo"
baseName = "baseName"
version = "version"
group = "group"
}
Here i provide the extension called globalvariable.
Now, The jars to be produced shd hav the name according to the values from the build.gradle..
How to get the value from build.gradle in the plugin inorder name the jar???
Not sure I understand the question. It's the plugin that installs the extension object, and it's the plugin that needs to do something with it.
Note that the plugin has to defer reading information from the extension object because the latter might only get populated after the plugin has run. (A plugin runs when apply plugin: is called in a build script. globalVariables {} might come after that.) There are several techniques for deferring configuration. In your particular case, if I wanted to use the information provided by the extension object to configure a Jar task, I might use jar.doFirst { ... } or gradle.projectsEvaluated { jar. ... }.
Before you go about writing plugins, make sure to study the Writing Custom Plugins chapter in the Gradle user guide. A search on Stack Overflow or on http://forums.gradle.org should turn up more information on techniques for deferring configuration. Another valuable source of information is the Gradle codebase (e.g. the plugins in the code-quality subproject).

Enabling certain java refactoring within Groovy editor in IntelliJ

From within IntelliJ IDEA, when I'm within a groovy class, and refer to a property / field on java class that doesn't exist, is there a way to enable the create property refactoring?
Eg:
// Inside Foo.groovy
void method()
{
Bar bar = new Bar(); // Defined in Bar.java
bar.someProperty = "Hello, world"; // bar.someProperty doesn't exist.
}
In the above example, I'd like to get access to the "create property" refactoring option on someProperty. Is there a way to enable this?
Note: I'm using IntelliJ 10.
As of now, refactorings for Groovy in IntelliJ are pretty limited compared to Java. The reason for this is Groovy is an optionally typed language, so the IDE needs much more 'brain' for Groovy refactorings than for Java.
create property currently does not exist for Groovy.
IMHO the only thing you can do is filing a ticket on http://youtrack.jetbrains.net. Jetbrains is very responsive - I know from own experience.

Resources