Read gradle paths from project - groovy

Is there any way to access Gradle groovy plugin sourceSets dirs from my Groovy project built by Gradle? I am looking for default gradle src and resources directories.
I need it to avoid hardcoding a resources directory in my project but use the default Groovy plugin resource directory (resources/main).

You shouldn't mix up your production code with your test sources and your test resources. I would suggest the following layout (the default of the groovy plugin) of your src directories in your project:
groovy production code in "src/main/groovy"
unit tests written in java or groovy in "src/test/groovy"
*.groovy resources for testing your DSL in "src/test/resources"
Now you can reference the .groovy dsl test files from your tests via
URL url = this.getClass().getResource("/testDsl.groovy");
File testDslFile = new File(url.getFile());

Can you explain the use case for that? within your gradle build you can access the groovy sourceSets dirs introduced by Gradles groovy plugin like this:
apply plugin:'groovy'
task printGroovySourceDirs << {
sourceSets.main.groovy.srcDirs.each{
println it.absolutePath
}
}

Related

Add groovy extention module in runtime

I writing gradle plugin for code generation, and it's use groovy org.codehaus.groovy.runtime.ExtensionModule.
But extensions resolve on gradle (daemon) start stage, and add jar with META-INF/services/org.codehaus.groovy.runtime.ExtensionModule into build script classpath has no effect.
How to register ExtentionModule manually?
I don't believe you can, as it says in the documentation:
to use an extension, it has to be available on classpath, as compiled classes, before the code using it gets compiled

Include dependencies in Groovy application without repository access

I have a Groovy project (using Eclipse) which makes use of several #Grab statements. This works fine on my development machine. However I need to distribute this application including all its dependencies to other machines which don't have any internet connection, i.e. it won't be possible to download the necessary JARs from these machines.
Is there a way to somehow automatically include the dependencies into the project, e.g. a lib folder? This way I could just copy the project to another machine and use it.
So, lets say for example you have a script Script.groovy like so, that you currently run with groovy Script.groovy:
#Grab('com.github.groovy-wslite:groovy-wslite:1.1.2')
import wslite.rest.*
def client = new RESTClient("http://httpbin.org")
def response = client.get(path:'/get')
assert 200 == response.statusCode
println "Received : $response.json"
Now, we want to get this into a jar file that you can distribute, and people can just run with java -jar myApp.jar
So make the following folder structure:
myApp
|-- src
| |-- main
| |-- groovy
| |-- example
| |-- Script.groovy
|-- build.gradle
Then, in Script.groovy, put your script (with a package name, and no #Grab annotation):
package example
import wslite.rest.*
def client = new RESTClient("http://httpbin.org")
def response = client.get(path:'/get')
assert 200 == response.statusCode
println "Received : $response.json"
And in build.gradle, put this script which pulls down the groovy, and groovy-wslite dependencies, and applies the shadow-jar plugin to bundle all dependencies into one single fat jar:
plugins {
id "com.github.johnrengelman.shadow" version "1.2.2"
}
apply plugin: 'groovy'
apply plugin: 'application'
repositories {
jcenter()
}
mainClassName = 'example.Script'
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.4.5'
compile 'com.github.groovy-wslite:groovy-wslite:1.1.2'
}
You can then (assuming you have installed Gradle), simply run:
gradle shadowJar
Which will compile your code, and put it and all its dependencies into build/libs/myApp-all.jar
So then, you can just run:
java -jar build/libs/myApp-all.jar
And your script should run as before...
You can then distribute this jar file, instead of just the script...
Hope this helps
I would suggest switching to Gradle or some other build tool that downloads the dependencies at build time. As you probably already know grape pulls down all the dependencies at runtime.
Grape (The Groovy Adaptable Packaging Engine or Groovy Advanced Packaging Engine) is the infrastructure enabling the grab() calls in Groovy, a set of classes leveraging Ivy to allow for a repository driven module system for Groovy. This allows a developer to write a script with an essentially arbitrary library requirement, and ship just the script. Grape will, at runtime, download as needed and link the named libraries and all dependencies forming a transitive closure when the script is run from existing repositories such as Ibiblio, Codehaus, and java.net.
This link might help you in your transition to using Gradle with your Groovy script.
Running Groovy scripts from Gradle
You could copy your Grape repo to the target deployment servers. Should be ~/.groovy/Grape. Then you can keep your #Grabs in the scripts as is
Two solutions
Replace the whole build progress with gradle, just like what's mentioned by #tim_yates' answer.
Use grape install command to pre-install the packages into the grape local repo, whose default path is "~/.groovy/grapes".Then package the scripts and the grape dir together. You might switch the grape repo dir to somewhere you prefer. See section 3.5 of http://docs.groovy-lang.org/latest/html/documentation/grape.html

Change Gradle's working directory when compiling a Groovy project

When compiling a groovy project, gradle creates files in $projectRoot/build/. Is there a way to configure this to e.g. an absolute path?
Yes, there is. You can put the following line in your build script:
buildDir = 'your_directory'
Or in gradle.properties file (no quotes around your_directory in that case).
Build directory is a project property. You can view all project properties available to you by typing:
gradle properties
You can set project.buildDir in your build.gradle, or buildDir = PATH in gradle.properties.
And from environment variable like this:
export GRADLE_OPTS=-Dorg.gradle.project.buildDir=/tmp/gradle-build
See Build Environment section of the documentation here:
https://docs.gradle.org/current/userguide/build_environment.html#sec:gradle_environment_variables
And Writing Build Scripts here (for buildDir property):
https://docs.gradle.org/current/userguide/writing_build_scripts.html
(Current version of gradle when writing this: 4.5)
You can add project.buildDir = 'your/directory' to the file build.gradle
you can place it anywhere in separate line

Adding Jar dependency in gradle custom plugin

Assuming all my Gradle plugin user going to have a MYAPP_HOME sys variable set in there system
in MYAPP_HOME page i have a jar at $MYAPP_HOME/lib/mylib.jar
i am writing my own plugin....
I can find the MYAPP_HOME variable is set and fine the jar exists..
How can i add this jar dependency in my custom gradle plugin... ? when user runs my plugin say compileMyplugin my custom gradle plugin need to set the $MYAPP_HOME/lib/mylib.jar jar as compiler dependent
How to do this any one help me ?
The plugin just needs to do:
project.dependencies {
compile project.files("${System.getenv("MYAPP_HOME")}/lib/mylib.jar"))
}
PS: In general, I wouldn't recommend relying on an environment variable and the availability of a Jar on the local file system. Instead, I'd publish the Jar to an artifact repository or put it under source control.

How to Run a Spock Test inside Eclipse

I try to run my first Spock Test inside Eclipse, and it does not work.
I added all the Maven dependencies and plugins in my pom.xml, but when I run my test with jUnit, there is a popup windows with this Warning message : "No jUnit tests found".
Did you already seen this kind of message ?
What config has to be done, in order to run a Spock Test inside Eclipse ?
Thanks a lot.
Its same as running Junit test cases.
Right click on the class and run as 4Junit Test runner. see below for complete configurations and running the spock test.
Running Spock Framework with Eclipse, Gradle, Groovy: Source -
Krzysztof Goralski, blog
-Install Gradle Plugin, check it here
-Install Groovy-Eclipse for Juno or Indigo from Eclipse Marketplace (or maybe Groovy/Grails Tool Suite for Eclipse)
-Install Spock Plugin From Eclipse Marketplace if you want, check it here
-Import Project to Eclipse through Gradle Import
-Add these lines to build.gradle:
apply plugin: ‘groovy’
testCompile ‘org.spockframework:spock-spring:1.0-groovy-2.3’ (for Spring)
this is quite important, version can make some conflicts
-After this *.groovy and *.gradle files will problably looks different, Syntax colour highlightning etc. Remember that you can right click on for eg. build.gradle -> Open with -> Open With Minimalist gradle Editor etc.
-Probably you will need to make additional folder for *.groovy test files
Create new *.groovy file, class
-Basic test example, extends Specification from Spock framework and needs specific Annotations when running with Spring
-Now you can run it with JUnit from Eclipse
For integration tests you can’t use #RunWith(SpringJUnit4ClassRunner.class), and Context should looks like here #ContextConfiguration(locations = [ “/restTestContext.xml” ]) , not {} braces, but [ ]
-Spock can be used for Mocks too. Something like this: Subscriber subscriber1 = Mock() , subscriber1.isActive() >> true , So, remember >> operator for mocks.
Right click on the project > Properties > Java Build Bath > Add External Jars and add spock-core-0.6-groovy-1.8.jar and check if Groovy Libraries are there in Build Path or not. If not click on Add Library and select Groovy Runtime Libraries and restart Eclipse. Now you should be able to run. If still can't Run then try creating New Configuration and change the Test runner to Junit4 and Run it...
Check if the folder your tests are in is a source folder.

Resources