How to run groovy scripts in gradle project from windows command line - grails

I have created one gradle project for Geb-Spock. In this project I have created scripts in Groovy class.
I would like to execute those scripts from command line with the help of gradle commands. So that I can achieve Jenkins - Gradle - Geb integration.
Can you please help me to get the command which can execute the gradle-groovy scripts from windows command line. Thanks for your help on this.

Maybe so. If I understood correctly, you want run it like:
gradle myScript
You define a Task in gradle and import your groovy class into build.gradle like:
import com.MyScript
task myScript(){
new MyScript().run()
}
You also need a dependecy for your script (look her). e.g.:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath files('path/to/myScript/lib')
}
}
Also take a look how to compile the code for buildscript classpath if you need: https://docs.gradle.org/current/userguide/organizing_build_logic.html#sec:build_sources
I'm not really sure that you need all that. If you just want to run some Geb-Spock Test in Gradle take a look in https://github.com/geb/geb-example-gradle

Related

How do I make jar files available to ant in a Jenkins pipeline?

I've put together a basic Jenkins pipeline and it does what I expect for the most part.
However, I'm using ant and it requires access to specific jar files. I've specified the build step like so:
stage('Build') {
// Build the project
env.PATH = "${tool 'ant'}/bin:${env.PATH}"
sh 'ant -f dita-tools/build_all.xml -lib $WORKSPACE/dita-ot/lib:$WORKSPACE/dita-ot/lib/saxon'
}
The build I run through this pipeline fails and generates the following error:
java.lang.ClassNotFoundException: org.dita.dost.module.GenMapAndTopicListModule
From what I can tell, this is due to ant not having access to dost.jar that is in the dita ot. I've tried defining this argument a number of ways including specifically referencing dost.jar (I have a number of jars to include) but every time it fails with the same error.
When I put together a stand-alone ant project in Jenkins, ant has no problem accessing the jar by way of the argument I provide above. Is there a better way for me to supply this argument/dependency in a pipeline?
UPDATE:
I added an echo statement for the classpath to my build script and was able to verify that adding the jars to the classpath in the build script does in fact work. So, for all intents and purposes, ant has access to all the relevant base toolkit jars for the target but the error persists. At this point, it appears that the problem has something to do with how the jenkins pipeline works as opposed to the dita ot itself?
I assume you use custom plugins, if yes, please make sure, you correctly defined your jars in the plugin.xml like so:
<feature extension="dita.conductor.lib.import" file="lib/my.jar"/>
UPDATE
java.lang.ClassNotFoundException: org.dita.dost.module.GenMapAndTopicListModule
This error means, that the main DITA-OT jar is not found on your classpath. So this indicates, that this is not a plugin issue.
Usually you don't have to setup the classpath, Ant does that for you. Please also read Creating an Ant build script.
Please try a snippet like this one:
node {
try {
checkout scm
stage('Build') {
sh '''
dir=$(pwd)
curl [your-dita-ot-url] --output dita-ot.zip
unzip -qq "$dir/dita-ot.zip"
rm dita-ot.zip
chmod +x ${WORKSPACE}/dita-ot/bin/ant
${WORKSPACE}/dita-ot/bin/ant -f ${WORKSPACE}/build.xml -Ddita.dir=$dir/dita-ot -Dbranch.name=$BRANCH_NAME
'''
}
} catch (e) {
currentBuild.result = "FAILED"
throw e
} finally {
notifyBuild(currentBuild.result)
}
}

How to run the grails default commands from a custom script in grails

I need to run the grails default commands like clean, compile, prod war using the code.
I am created a custom script in my project and i need to run these commands through codes.
My custom script is given below.
class CustomDeploy extends Script {
def run() {
// my code for running commands.
}
}
please help me.
You can create a Grails script, which has access to gradle.
http://docs.grails.org/latest/guide/commandLine.html#creatingCustomScripts

How to run a custom grails command from grails executable jar/war

I'm using grails 3.2.8. I am generating an executable war file from my grails project that has web functionality in it. However, I've also written some custom grails commands that I'd like to be able to run in production (as cron jobs) from an executable jar/war that I build from the same grails project. I can run them in my development environment as "grails run-cmd ...", but I'd like to be able to deploy an executable jar/war file and run the custom command from the executable jar/war. In other words, I want to deploy a war file for web stuff to one server, and I want to deploy an executable jar file for some cron jobs all from a single grails project. I know how build/run the war file--grails makes that easy. However, I really have no idea how to generate an executable jar file from my project that allows me to run my custom grails commands as cron jobs. Can anyone tell me how to do this?
I have found something that seems to work. I've modified the Application.groovy class in my grails project to look like this:
import grails.boot.GrailsApp
import grails.boot.config.GrailsAutoConfiguration
import grails.ui.command.GrailsApplicationContextCommandRunner
class Application extends GrailsAutoConfiguration {
static void main(String[] args) {
if (args.length > 0 && args[0] == "run-command") {
// If the first argument is 'run-command', then we just want to run a command as if we were running
// 'grails run-command <grails-custom-command> <args>...'. We are adding the capability of running commands here
// because this is the class that is run from the executable war generated by running 'grails war'. If we just do the same
// thing that grails does to run a command, then the commands seem to execute just fine.
args = args.tail()
// The following code is copied from GrailsApplicationContextCommandRunner.main(). It is what grails does to make
// 'grails run-command' work from the grails console/command line. When upgrading grails, it may be necessary to update
// this code...
def runner = new GrailsApplicationContextCommandRunner(args[0], Application)
runner.run(args)
} else {
GrailsApp.run(Application, args)
}
}
}
I have also changed the dependency in build.gradle for the grails-console dependency from 'console' to 'compile':
compile "org.grails:grails-console" // changed from 'console' dependency since we want to be able to run custom grails commands from the executable jar/war
This is because the GrailsApplicationContextCommandRunner class is in grails-console.
With these changes in place, I can still run the war file with:
java -jar myWarFile.war
However, I am now also able to run my custom commands with the exact same war file like this:
java -jar myWarFile.war run-command my-command <command args>
It seems like there should be a better way to do this, so it would be great if the grails team would comment (and if there isn't a better way, then the grails team should consider adding running custom commands from the executable war file as a grails feature request), but I do seem to be able to run my custom commands from the executable war file this way.

Gradle Ant Cannot add task ':myproject:test' as a task with that name already exists

I'm trying to Gradle-ize our build by using Gradle to execute the Ant build. I'm using the java plugin so I can set source/target and I'm using ant.importBuild 'build.xml'. When I execute Gradle, I get the error above. I understand that both Ant and Gradle have these targets/tasks in common: clean, jar, javadoc, test. One option is to change the Ant target names in build.xml, but I'm hoping there's an easier way as I have a lot of projects and build files. I found this "wrapper" solution (http://issues.gradle.org/browse/GRADLE-771), but this did not work for me. How can I solve this?
Your options are:
Do not apply the plugin to the same project that imports the Ant build.
Rename the conflicting targets in the Ant build script.
You can rename all the ant targets:
ant.importBuild('build.xml') { String oldTargetName ->
return 'ant_' + oldTargetName
}

How to install gradle-grails-plugin?

Complete gradle nooby here.
I want to be able to execute grails build commands like "grails compile", "grails test-app" and so forth from gradle.
I found the grails-gradle-plugin from Peter Ledbrook at: https://github.com/grails/grails-gradle-plugin.
Cloning the repository I get some files and folders. In the readme file it says " include the required JARs via buildscript {} and 'apply' the plugin". The apply part sure I get but how do I add the JAR? And which jar? Do I need to use gradle on the build file in the folder of the downloaded plug-in and compile a jar? And ones I get the jar where do I place it and how do I include it in my projects build.gradle file?
I have a feeling this is going to be ridiculously easy but I just can't get it to work.
In Gradle, the jars are added to build script or to your application class path through dependencies closure e.g.
dependencies {
compile "org.grails:grails-crud:1.3.4"
compile "org.grails:grails-gorm:1.3.4"
compile "ch.qos.logback:logback-core:1.0.7"
compile "org.slf4j:slf4j-api:1.7.2"
}
compile is a name of one of the many configurations (there are also test, runtime etc.) and e.g. "org.grails:grails-crud:1.3.4" is a reference to a jar in one of the public repositories, which are also specified in your scripts in repositories closure.
You can read more about Gradle dependency management in http://gradle.org/docs/current/userguide/dependency_management.html.
For your Grails project you need to define a build.gradle file which looks similar to what is described in the README.
Though I tried today to just create a simple Grails project using that plugin and gradle init command and it didn't work. I have created an issue for that: https://github.com/grails/grails-gradle-plugin/issues/16.

Resources