Test files in classpath - grails

Ive created a folder resources under test/integration path to store data files that will be used during integration test phase.
I use then
private static final Resource jsonCategory = new ClassPathResource("resources/testdata.json")
It's working on local with simple test-app (no special options) however when jenkings takes the code and try to run the tests they are failing because
java.io.FileNotFoundException: class path resource [resources/testdata.json] cannot be resolved to URL because it does not exist
I've checked that files are in the same location ... but it seems that by unknown reason jenking cannot find them in the classpath.
Might this be possible? ... do you have any idea about how to make jenkins resolve the same classpath i have in my local?
By the way the command line that jenkins uses looks like:
grails -Dgrails.work.dir=/var/lib/jenkins/workspace/myapp-develop//target -Dgrails.env=TEST clean --non-interactive --plain-output --refresh-dependencies
What i can see is that those files are not in that work.dir ... shall them be there?

Try to put your file under test/resources/testdata.file and access it via new File("test/resources/testdata.file).

Related

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.

Read DSL from file in Jenkins outside of workspace

I know its possible to run a .dsl file from an external source instead of just writing the code of the flow in the job's description, but every time I try to run lets say:
/home/flows/flow_script.dsl
I get the following error:
java.io.FileNotFoundException:/home/flows/flow_script.dsl (No such file or directory)
The path is correct, I can see the file through that path from the shell, but it doesnt let me select anything outside the "builds workspace" apparetly.
I recently ran into this very issue: my DSL script was outside of my workspace (installed via a package). The problem is that the DSL Scripts path is an Ant format that only allows specific patterns (and not absolute paths).
My workaround is hacky, but it did work: add an Execute Shell step before the "Process Job DSLs" step that symlinks the external directory into the workspace.
Something like this:
echo "Creating a symlink from /home/flows to workspace"
ln -sf "/home/flows" .flows
Then you can set the DSL Scripts path to ".flows/flow_script.dsl".
This has some additional caveats, of course: the directory you're symlinking from will need to be accessible by the jenkins user. And it likely violates a lot of best practices.

Ant cannot create task name is undefined

I'm using Ant with eclipse and everything was working fine, until I decided to do some house keeping and created 2 sub directories under my Ant dir.
I moved my build.xml to the sub directory and now nothing is working an I get:
BUILD FAILED C:\Users\OdedHarniv\Workspaces\Force.com
IDE\vidmind\ANTs\Vid Service\build.xml:26: Problem: failed to create
task or type antlib:com.salesforce:retrieve Cause: The name is
undefined.
Any idea what am I doing wrong?
I see two possible problems
1 You have a relative path defined somewhere in your ant file, or a file that you are importing. Note that if you are importing e.g. a properties file, relative paths in that one will be interpreted relative to the directory containing the main ant file.
2 If you are running through Eclipse, it will run through a run-configuration. That might have extra jars put on its class path. I expect Eclipse will create a new run configurations when you move the file, which has the default class path. Too see your run configuration go to Run > External Tools > External Tools....

grails where should I put a test for a Java class in src/java?

I have a Java class in src/java whose .class file will be copied to an 'externalConfig' folder and saved in SVN, so that it can be pulled out on the build server and sent to a remote platform. I'm using GGTS 3.1.
I have written a test for this. Where should I put it? I've tried in a xxx.yyy.test package in src/java. I can run this OK as jUnit Test (after putting Mockito in a lib folder and adding to project's classpath).
However, test-app produces compile errors for the test class as it can't find Mockito.*, mock etc. Same result for run-app.
I have refactored and moved the text package and class to test/unit (more in hope than expectation). I got the same errors running test-app, but not from run-app.
Basically, I would be happy for Grails to ignore this test class. What's the best solution?
You should put them in test/unit or test/integration.
Probably compiled classes are still there in target folder, after you moved them from src to test, call grails clean to remove compiled files.

Convert grails app to plugin

I started a grails application by grails create-app. For modularity, I feel like it would be better that component be a plugin. Can I convert this application into a grails plugin?
thanks,
Babu.
I never created a plugin based on an application written before but looking at the documentation for grails plugins you can read the following statement:
The structure of a Grails plugin is exactly the same as a regular Grails project's directory structure, except that in the root of the plugin directory you will find a plugin Groovy file called the "plugin descriptor".
So I would suggest to create a new plugin with grails create-plugin *your-plugin-name* and copy all files from your application into the plugin.
In case anyone else is looking, this should be exactly what you need: http://burtbeckwith.com/blog/?p=1973
Excerpt:
So to convert an application to a plugin, the general workflow would
be something like
Create the plugin descriptor, FooGrailsPlugin.groovy. The easiest way to do this is to rungrails create-plugin pluginname and copy the
generated file from there
delete everything from application.properties except the app.grails.version property
if you have jars in the lib directory that are available in a Maven repo, delete them and replace with BuildConfig.groovy dependencies
change any plugin and jar dependencies that are needed for development and testing but not when the plugin is installed to not be
exported, by adding export = false
If you need the _Install.groovy, _Uninstall.groovy, or _Upgrade.groovy scripts (you probably don’t) grab those from the dummy plugin from step 1 (but delete any you don’t need, they’re all
optional)
delete ApplicationResources.groovy if you aren’t using it and don’t depend on resources plugin
move code from BootStrap.groovy init() toFooGrailsPlugin.doWithApplicationContext
and/orFooGrailsPlugin.doWithDynamicMethods and destroy() to
FooGrailsPlugin.onShutdown, and delete BootStrap.groovy
add a dependency for the release plugin in BuildConfig.groovy
delete everything but the log4j configuration from Config.groovy
delete UrlMappings.groovy unless you have exported mappings; only keep the added ones
move bean definitions from resources.groovy to FooGrailsPlugin.doWithSpring and delete resources.groovy
delete grails-app/i18n message bundle files unless you added messages; only keep the added ones
delete everything from grails-app/views that you don’t use (in particular error.gsp,index.gsp, and layouts/main.gsp)
delete everything from web-app that you don’t use (including WEB-INF xml and tld files)
now would be a great time to write those tests you’ve been meaning to get to
create one or more test applications to install the plugin into to ensure that it works as a plugin; consider scripting this
write documentation for how to use the plugin; at a minimum a README file, but Grails gdoc files would be much better (run grails doc
--init to get started)

Resources