How to declare dependency scope for a local plugin? - grails

I want to install my-plugin.zip with a certain dependency. How can I do this?
This doesn't seem to work inside BuildConfig.groovy:
runtime '/path/to/my/plugin.zip'

If the plugin is already packaged as zip then it can be referred as any other Grails plugin in BuildConfig.groovy. You just need to keep note of 2 things:
Plugin Name [Generally MyPlugin app would have plugin name as my-plugin]
Plugin Version
Then BuildConfig would look like:-
//BuildConfig.groovy
plugins{
runtime ':my-plugin:0.1'
//compile ':my-plugin:0.1'
}
When plugin is packaged it will be available in cached-installed-plugins inside $Userhome/.grails/<version> by default. Also, by default the group is org.grails.plugins. In order to test for compile, build and/or test configurations, try to change as above and run a dependency-report to see the plugin available in corressponding configurations.
Alternative:
There is another "soon-to-be-deprecated" approach to install your plugin.
grails install-plugin <path/to/YourPlugin.zip>
will install the plugin in the application in default, compile, runtime configurations. I would not suggest this approach as install-plugin is going to be deprecated.

Related

Creating and installing a grails plugin - how does my plugin access resources from a plugin its dependent upon during/after install?

This question is an extension from another question I posted here:
In Grails 2, how do you includeTargets from Gant scripts from a plugin your app is dependent upon?
I am writing a grails plugin that is a my-company specific version of the shiro plugin, ex. my-company-shiro. I set shiro as a dependency for my plugin in the BuildConfig.groovy like so:
plugins {compile(":shiro:1.1.4")}
I package the plugin and try to install it to a new grails app called foo:
foo> grails install-plugin ../my-company-shiro/grails-my-company-shiro-01.zip
No problems.
Now, I want to run a script in foo that is part of my-company-shiro which in turn references a script from the shiro plugin:
foo>grails create-auth-controller
I get the following failure:
Error Error executing script CreateAuthController: No such property: shiroPluginDir for class: .....
This occurrs b/c one of my scripts being executed tries to access one of shiro's scripts like so:
includeTargets << new File (shiroPluginDir, "/scripts/_ShiroInternal.groovy")
This reference works when I compile my plugin, but not here when I am installing it in another grails app.
Am I setting the dependency incorrectly in the BuildConfig.groovy such that shiro's files are not being included in my plugin therefor I cannot reference it?
The shiro plugin shows up in my .grails cache my-compnay-shiro/plugins/shiro-1.1.4
When I install my-company-shiro plugin to foo, in the .grails cache foo/plugins/my-company-shiro-0.1/dependencies.groovy and plugin.xml files reference shiro. I do not see any of shiro's scripts or files here, but I have no idea if they are supposed to be copied here.
Is the reference to shiroPlugin incorrect at install time?
Thanks in advance!
grails install-plugin is deprecated, you need to use BuildConfig.groovy instead. I tested here, declaring the custom plugin inside the app and it works, you can use grails.plugin.location to specify the folder of your plugin.
Considering a plugin named shiro-test, the BuildConfig should be:
grails.project.dependency.resolution = {
...
legacyResolve true // whether to do a secondary resolve on plugin installation, not advised and here for backwards compatibility
...
}
grails.plugin.location."shiro-test" = "path/to/plugin"
Then you refresh your dependencies and can run any script from shiro-test.

install spring-security-core plugin into plugin, which then is installed in application

I've just switched to grails 2.2 and have got a major plugin problem. I've got an application - my-app and a plugin - my-plugin. I want to install spring-security-core plugin into my-plugin, and then install my-plugin into my-app. When I've done this and did s2-quickstart, so that LoginController got created. I can start my-plugin with no problems now, but when I try to start my-app it complains that it cannot find any springsecurity classes. Errors looks like this:
12: unable to resolve class org.springframework.security.web.WebAttributes # line 12, column 1.
7: unable to resolve class org.springframework.security.authentication.AccountExpiredException # line 7, column 1.
11: unable to resolve class org.springframework.security.core.context.SecurityContextHolder # line 11, column 1.
It looks to me, like only my-plugin can see spring security plugin dependencies, and my-app cannot, so they didn't cascade even thought according to manual they should have.
I've also tryed to install spring-security-core plugin by adding in BuildConfig.conf this:
compile ":spring-security-core:1.2.7.3"
but it didn't work either.
Any ideas?
If you use install-plugin in a plugin, it's only installed locally by adding a line in application.properties. It doesn't get exported as a dependency of your plugin. This could be used for plugins like code-coverage where you want to use it during development and testing but not force users to also install it.
In older versions of Grails the dependsOn map in the plugin descriptor was used to express plugin dependencies. This is now deprecated in favor of dependencies registered in the plugins secton of BuildConfig.groovy. This is both for consistency and to take advantage of the more fine-grained features supported by the dependency DSL including specifying scopes and exclusions. This is also true for applications - don't use install-plugin for either apps or plugins, always use BuildConfig.groovy.
Take a look at the spring-security-ldap plugin's BuildConfig.groovy. It has a compile-scope dependency on the core plugin, plus one for the hibernate plugin that's not exported (since it's just for testing) and a build-scope dependency on the release plugin (also not exported since it's just used to release the plugins).
You should probably using a similar dependency on the core plugin in your BuildConfig.groovy. Delete any plugin references in your application.properties and convert to BuildConfig.groovy syntax and run grails clean followed by grails compile.
Thank you Burt for your advice. I've used it and here's what I came to:
I created a plugin-app and installed spring-security-core plugin in it (using DataSource.groovy, and not install plugin). Then I created a main-app and installed my plugin-app (again using DataSource.groovy). When I did this in grails 2.1.1 everything worked just fine - I could use spring-security in my main-app, so the dependency got pulled just right. When I did everything the same, but in grails 2.2 I couldn't use spring-security in my main-app, so dependencies didn't get pulled. That's why I think this might be some kind of a bug in new grails version.

I can't install spring-security-core plugin in Grails

I have windows 7 and Grails-2.0.4. Trying to install plugin spring-security-core. I've tried from everywhere: cmd, Idea, SPS. Nothing works. Everywhere the same error:
!Error resolving plugin [name: spring-security-core, group: org.grails.plugins, version: latest.integration(1.2.7.3 in SPS)]. Plugin not found.
!Error Plugin not found for name [spring-security-core] and version[not specified(1.2.7.3 in SPS)]
I tried to put the zip-file of this plugin in different directories. Doesn't work. With connection everything is OK. Help me please.
Check the repositories section of your BuildConfig dependencies, and add the following if it is not already there:
grailsRepo "http://grails.org/plugins"
The grails plugin repository moved earlier this year and the old repository does not contain any plugin versions released since April 2012. I don't remember exactly which versions of Grails will use the new repository by default, the change happened somewhere around 2.0.3/4.
The preferred way to install it is to add a dependency in BuildConfig.groovy. The syntax for this is listed for each plugin, in this case from http://grails.org/plugin/spring-security-core: compile":spring-security-core:1.2.7.3"(look for the 'Dependency' section at the top). So theplugins` section of your BuildConfig.groovy should look like this:
plugins {
runtime ":hibernate:$grailsVersion"
build ":tomcat:$grailsVersion"
...
compile ":spring-security-core:1.2.7.3"
}
Alternatively you can install it with the install-plugin script:
$ grails install-plugin spring-security-core
but the dependency approach is the better way.

How to run a local plugin in Grails 2.0?

In Grails, there is a variant how to include local plugin from sources. According to docs, one may type in BuildConfig.groovy:
// Useful to test plugins you are developing.
grails.plugin.location.shiro =
"/home/dilbert/dev/plugins/grails-shiro"
// Useful for modular applications where all plugins and
// applications are in the same directory.
grails.plugin.location.'grails-ui' = "../grails-grails-ui"
The problem is that it doesn't work in Grails 2.0.RC1. I've tried to do grails clean, to install plugin with grails install-plugin and to place it to BuildConfig.groovy. Still unable to resolve.
This works for me
grails.plugin.location.shiro = "/home/dilbert/dev/plugins/grails-shiro"
Where shiro is the name of the plugin (not the name of the directory it's in). Make sure the path to the plugin is either an absolute path or the relative path to the plugin from the application.
I've found that this sometimes doesn't work if the plugin is listed in application.properties or BuildConfig.groovy, so if it is, remove it, then execute grails clean and restart the app.
You can also install the plugin into your local maven cache.
The documentation speaks about this:
3.7.10 Deploying to a Maven Repository
maven-install
The maven-install command will install the Grails project or plugin artifact into your local Maven cache:
grails maven-install
This has the advantage of allowing you to include the plugin in your parent application using the more common ":plugin-name:version" syntax
Which allows your application to determine the best place to retrieve the plugin when in production. From an internal maven-repo or equivalent.
With Grails 3.x there is another way to do this. Suppose you've a grails app and plugin (source code) inside the same project directory:
/my-project
---/my-app
---/grails-shiro
To run your local plugin, you must create a settings.gradle file in the my-projectdirectory specifying the location of your application and plugin:
include 'my-app', 'grails-shiro'
Then add the dependency in your application's build.gradle:
compile project(':grails-shiro')
You've done.
Look at the plugins documentation for more information.
Surround the plugin name with quotes in case it contains dashes:
grails.plugin.location.'plugin-name-with-dashes' = "<path>"
You can add the .zip file for the plugin in your /lib and it will be installed.
Example:
compile ":myPlugin:1.0"
File:
/lib/myPlugin-1.0.zip
Note: You have to zip the content of the plugin folder.
Source: http://grails.1312388.n4.nabble.com/Insert-own-local-plugin-into-build-config-td4646704.html

Grails Plugin Maven Integration

I'm trying to create Mavenized Grails application. Everything works fine but as I understood all the dependencies (all .jars like mysql-connector and also all grails (public) plugins like spring-security-core plugin) should be listed in pom.xml.
The thing is that I don't know how to include public grails plugins (is there any Maven repository for that, or should I include used plugins into my local repo?). Or is the proper way how to handle grails plugin to list them in "application.properties" and let the grails to manage these plugins?
Thank you for any comment.:-)
Mateo
You can specify your plugin dependencies in grails-app/conf/BuildConfig.groovy, for example:
grails.project.dependency.resolution = {
plugins {
runtime ':hibernate:1.2.1'
}
}
Update
In response to your comments below, a plugin dependency specified in BuildConfig.groovy (or application.properties) will still be resolved by Grails rather than Maven. I don't think there's any way that you can get Maven to resolve a Grails plugin dependency, because Maven can only work with JAR dependencies in Maven repositories. Remember, Grails plugins are not (typically) available from Maven repositories.
If you want to hand as much control as possible over to Maven, you can try excluding the JARs from your plugin dependencies, e.g.
plugins {
runtime( "org.grails.plugins:hibernate:1.2.1" ) {
excludes "javassist"
}
}
and add them to your pom.xml instead. Here be dragons (see below).
Editorializing
FWIW, unless you really have to build your Grails project with Maven (e.g. because another Maven project depends on it), my advice would be don't. I say this because Maven is very much a second-class citizen in the world of Grails build tools. The usual way to build a Grails app is using the built-in GAnt commands. Future versions of Grails will move towards Gradle as the default build tool, so it seems that Maven will be an afterthought for the forseeable future
By default, Grails plugins are included at the source level. A plugin zip is expanded, and the plugin source is compiled as part of the grails build process.
Since 2.0, grails can use binary plugins. You can depend on plain old JARS if those jars represent binary grails plugins.
Binary grails plugins can be referenced by normal maven coordinates.
Your project's BuildConfig.groovy is where you specify maven repositories and binary plugins.

Resources