I have a grails 'app' (say myapp) and a custom plugin (say myplugin) which are working together just fine.
I have some 'integration' tests in myapp and I can run them just fine from the app by doing cd myapp; grails test-app
I also have some 'integration' tests in myplugin and I can run them just fine from the plugin by doing cd myplugin; grails test-app
My problem is, that there does not seem to be any way to run myplugin test from myapp. E.g. what I'm trying to do is do a cd myapp; grails test-app to run both myapp and myplugin tests.
Is is even possible to do so?
Plugin tests aren't distributed by default, and if they were (it's certainly possible to manually or automatically get them included in the zip) you shouldn't treat the plugin as a Grails plugin project, but rather an installable plugin. Pretend the plugin isn't source code but compiled classes. The fact that the files' extensions are .groovy, .gsp, .java, etc. should be considered a coincidence.
The plugin's BuildConfig.groovy is not included with the plugin zip, and this likely has important dependencies and/or other info needed to run anything in the plugin. To run the tests, run them from the plugin source directory.
Related
Followed the tutorial on multi-projects
Everything mostly works. Plugin controllers & domain classes load properly in the application. However, a problem occurs when trying to run a Plugin's custom script from the application's grails CLI.
For example:
If you set up the multi-project directory structure like this:
Project Root
Application Directory
Plugin Directory
settings.gradle
And ran this command from the Plugin Directory
grails create-script hello
You'd be able to access the script when running grails from the Plugin Directory, but not the Application's Directory.
Is there a way to get this to work properly? Do I need to use an alternative set up?
Also see Creating a Custom Script in Grails
A conventional grails 3 plugin is different than a plugin within a multi-project. It doesn't seem to be designed to compile a plugin such as grails scaffolding with custom commands.
For this reason, you should package the plugin manually using:
grails package-plugin
grails install
Now in the build.gradle, add this line to dependencies:
compile "<plugin-group>:<plugin-name>:<plugin-version>
Subsituting the appropriate information within the brackets <>.
You can find the plugin-group in the plugin's build.gradle
group "org.grails.plugins"
plugin-name you specified in the grails create-plugin command
grails create-plugin plugin-name
plugin-version is also found in the plugin's build.gradle
version "0.1"
I've heard you should type command
grails war
to build your project. I've thought to this point that Gradle is responsible for building the app in Grails. I've been doing the latter with conviction that my app is built. So what's the difference between
grails war
and
gradle build
?
Is it just that grails war is gradle build + create the war file?
It is not that simple to compare Grails and Gradle. Gradle is a build tool, while Grails is a web application framework.
However, Grails provides a command line tool, that's described in the docs:
Grails incorporates the powerful build system Gant, which is a Groovy wrapper around Apache Ant.
So, Grails does not use Gradle.
The basic usage of the grails command looks the following:
grails [environment]* [command name]
Where especially the command name parameter must be one out of predefined values. You can find the documentation on the war command here.
The basic usage of the gradle command looks the following:
gradle [option...] [task...]
The listed task parameters can be names of tasks created either in the build.gradle script or by plugins. All mentioned tasks and their respective task dependencies will be executed. If you use the Gradle War Plugin, it will generate a war task, which will also (transitively) be added as a task dependency of the build task. So whenever you call gradle build, a WAR file will be created. You can also call this task directly via gradle war.
EDIT
I just learned that Grails can or even does use Gradle beginning at a certain version. I even found a list on which Grails command calls which Gradle task. According to this list, calling grails war is equivalent to calling gradle assemble. The assemble task directly depends on the war task.
gradle build is a Gradle lifecycle task which usually consists of other tasks required to build a software like compileJava and other lifecycle tasks like assemble and check.
In case of Grails it delegates build to Gradle and to war task and it doesn't include check lifecycle during which unit tests will be executed.
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.
In Grails 2.5.1, is it possible to use grails command-line tasks from a binary plugin in the lib folder?
I had to modify an existing third-party plugin to get it to work properly.
I put the jar generated by the following command in my project's lib directory:
grails package-plugin --binary
When my project runs, it correctly uses my modified version of the plugin.
At build time, however, I need to run a Grails task on the command line that was provided by the plugin, but, using the binary plugin, the task does not appear to be available.
e.g., if the task was abc, when using the real plugin being referenced in the plugins section of BuildConfig.groovy, then I could run:
grails abc
Using the binary plugin in the lib folder, however, results in the task not being available from the command line.
Also, the original plugin hooked into grails war to include extra steps in the build process without changing the command line. These hooks no longer run with the binary plugin. Is there any way to reinstate the hooks for the binary plugin?
Thanks.
I am working with a (sort of) framework built on top of Grails. This framework is a set of Grails plugins that add functionality to the Grails app (e.g. user authentication). This framework is a bit of a pain to setup, as it requires around 64 lines of site specific configuration in the apps's Config.groovy file.
I want to develop my addons to this app as plugins. That is, the Grails app would really just be a set of installed plugins and some configuration files.
I have created a local Maven style repository to hold all of my plugins. Thus, I can add plugin dependencies to the BuildConfig.groovy file and they will be installed automatically (side question: is it possible to specify the install order?).
So my question is, how do I create skeleton project for developing my plugins that would:
Include the base configuration for my application (the aforementioned 64 lines)
Allow me to do a grails package-plugin to package only the plugin's code
You can use the post-installation hooks mechanism: http://grails.org/doc/latest/guide/plugins.html#hookingIntoBuildEvents
Not really an ideal setup for me, but the following works:
Create the "base" application: cd ~/GrailsDev/ && grails create-app my-app
Configure my-app as desired/required
Create your dependent plugin: cd ~/GrailsDev/ && grails create-plugin my-app-plugin
Add the new plugin to the app by editing "~/GrailsDev/my-app/grails-app/conf/BuildConfig.groovy" and appending the line: grails.plugin.location.'my-app-plugin' = "../my-app-plugin"
You can now run the my-app Grails application and the plugin will be included. When your plugin is fully developed, you can do grails package-plugin from within the "~/GrailsDev/my-app-plugin" directory to package your plugin.
use gradle. you can specify the order and package your plugin alone.
e.g. include the required plugins as git modules (for easy versioning) and gradle modules (for building your plugin) in your plugin project.
this setup will serve your requirements well I suppose.
https://github.com/grails/grails-gradle-plugin
IntelliJ does have a template for gradle-backed grails applications and plugins.