Manage source and javadoc for Grails automatic dependencies in IntelliJ IDEA? - grails

How do I attach source and javadoc to libraries in IntelliJ IDEA that are linked automatically by Grails dependency resolution and are not explicitly listed in the IDEA project settings?
For example in
BuildConfig.groovy:
grails.project.dependency.resolution = {
repositories {
mavenRepo "http://oss.sonatype.org/content/repositories/releases/"
}
dependencies {
runtime 'org.elasticsearch:elasticsearch:0.17.7'
}
}
Is there a method to have IDEA automatically pick up the source and javadoc from the Maven repository?

IDEA attached the source automatically to the project plugin module. The JavaDoc can be manually attached to the plugin module in the project structure interface at:
File -> Project Structure -> Project Settings -> Modules -> project name-grailsPlugins -> Paths -> JavaDoc

IDEA 11 will be ably to search and download sources for any jar.
The search is performed using https://oss.sonatype.org/ by artifactId and version got from name of jar.

not an answer, but an idea:
you might hook into the 'grails integrate-with' gant script and add this functionality yourself. $GRAILS_HOME/scripts/IntegrateWith.groovy uses some templates for .ipr and .iml file that could be modified.
At some point in future it is excepted that gradle will be used for building Grails projects but there's still a long way to go.

Related

How can I include a dependant jar in a multi project grails / gradle build?

I have the following setup within my project...
A multi project gradle build for a SOA style project, wired together using rabbitmq and spring integration.
Contains a number of grails projects as well as plain java / groovy projects to represent the services.
Alongside each of the service projects are a project (jar) containing all of the public interfaces (and messages) for the service that can be proxied using spring integration.
The projects are related to each other using gradle dependencies and then I generate IntelliJ projects files using the gradle idea plugin.
What I want to do is:
Include the interfaces jar in the grails project so that I can use spring integration there to proxy my calls into the services via rabbitmq.
When I run the grails app have this intefaces jar built and included within grails.
When I run the grails app from IntelliJ have it compile the latest version of the interfaces and include them in the grails project.
When I build the entire project from gradle, have gradle correctly associate the interfaces jar with the grails app.
Ideally I would love to be able to do this just using dependency declaration within gradle, but this is probably a pipe dream...
What are my options?
Add a task into the grails build lifecycle within gradle to build any dependant jars and copy them into the grails lib folder?
Hook into the grails build lifecycle by using Events.groovy or similar to call out to grails to build and package the dependant jars. This would cover both the IntelliJ and command line routes.
Build the interfaces as a grails plugin? I had discounted this as they also need to be used from non-grails projects.
Any help / advice would be appreciated.
Turns out all I needed to do was add the following and then the grails plugin deals with the dependencies for me...
compile project(':dependent-project')
Works nicely for run-app and war...
A partial solution to the problem would be to use both Gradle and Grails maven plug-ins. I have a similar situation where I am building jars that are dependencies of the Grails project.
The approach I've chosen is to install the java artifacts into the local .m2/repo and then declare the dependency under grails/conf/BuildConfig.groovy using the mavenLocal() repo.
What I hadn't considered was to hook gradle into the events lifecycle (interesting idea, btw) and instead defined a gradle project that wraps the grails app (executes test-app, run-app, etc). The gradle wrapper for my grails app has a dependency on the other component's install task so it always checks to see if it needs to be rebuilt.
I'm an Eclipse user so I can't comment on the Intellij part of your question but the above works for me so I hope it gives you some ideas?
My solution for the moment is to:
Use the grails / gradle plugin to build the grails projects.
Use this plugin to run my grails apps, ie. gradle grails-run-app.
Hook into the grails-run-app task in gradle (which is created on the fly) to call a task which builds and copies the dependencies into the lib directory.
This doesn't help a whole load with IntelliJ at the moment but I will run my gradle tasks as IntelliJ run configurations.
My build.gradle is as follows (dependent-project is being jarred and put in lib in this example):
import org.grails.gradle.plugin.GrailsTask
evaluationDependsOn(':dependent-project')
buildscript {
repositories {
mavenCentral()
mavenRepo name: "grails", url: 'http://repo.grails.org/grails/repo'
}
dependencies {
classpath "org.grails:grails-gradle-plugin:1.1.1-SNAPSHOT"
}
}
repositories {
mavenCentral()
mavenRepo name: "grails", url: 'http://repo.grails.org/grails/repo'
}
ext {
version = "1.0"
grailsVersion = "2.2.0.RC2"
grailsTaskPrefix = "grails-"
}
apply plugin: "grails"
dependencies {
['dependencies', 'resources', 'core', 'test', 'hibernate', 'plugin-datasource', 'plugin-domain-class', 'plugin-tomcat', 'plugin-services'].each { plugin ->
compile "org.grails:grails-$plugin:2.2.0.RC2"
}
bootstrap "org.codehaus.groovy:groovy-all:2.0.5"
}
// Get hold of the grails-run-app task (which is created on the fly) and add a dependency to the copyDependencies task
project.gradle.afterProject { p, ex ->
if (p == project) {
project.tasks.addRule("Grails dependencies") { String name ->
if (name.startsWith(grailsTaskPrefix)) {
tasks.getByName(name).dependsOn(copyDependencies)
}
}
}
}
// Build and copy any dependent jar files...
task copyDependencies(type: Sync) {
from project(':dependent-project').configurations.archives.allArtifacts.files
into "$projectDir/lib"
}

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.

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.

Grails - Reference Plugin Jar from the plugin's BuildConfig.groovy (Custom Resolver)

I have a plugin that loads a custom Ivy resolver. It uses ${basedir}
to locate the jar file containing the resolver so I can load it inside
BuildConfig (see the answer for context). That compiles the plugin, but unfortunately, when the plugin is installed in a project,basedir becomes the project directory so it can't find the jar. pluginBasedir doesn't seem to point to anything, even inside the plugin's BuildConfig.groovy.
Is there any way to figure out the plugin base directory from within a
plugin's BuildConfig.groovy?
Ultimately I just want my custom resolver (in an external jar) to work when compiling the plugin and when compiling any project the plugin is a part of. Any solution is welcome.
The best answer I could come up with was to get my jar into a public Maven repo and use #Grab. e.g.,
#Grab(group='com.noahsloan.atg',module="atg-resolver",version="1.0")
import com.noahsloan.atg.ivy.AtgModuleRepository
grails.project.dependency.resolution = {
resolver AtgModuleRepository.newResolver
From my plugin. I'd still like to know if there is a way to reference pluginBasedir from BuildConfig.

After installing the spring-security-core grails plugin, how do you get IDEA to know about the jar files?

I have installed spring-security-core in a grails project, but for some reason, IDEA didn't automatically pick up the jar files. I can deploy the app and run tests using the grails command line just fine... but IDEA still puts squiggly lines. Is there a way I can get IDEA to automatically pick up jars after I install plugins?
I'm not 100% what the real source of this problem is. To my knowledge, IntelliJ parses the BuildConfig.groovy inside plugins to configure dependencies. It seems, that it does not pickup the autogenerated dependencies.groovy. The packaged spring-security-core plugin just contains a dependencies.groovy and no BuildConfig.groovy.
I've added the following snippet to my app's BuildConfig.groovy
dependencies {
compile('org.springframework.security:org.springframework.security.core:3.0.2.RELEASE') {
excludes 'com.springsource.org.aopalliance',
'com.springsource.org.apache.commons.logging',
'org.springframework.beans',
'org.springframework.context',
'org.springframework.core'
}
compile('org.springframework.security:org.springframework.security.web:3.0.2.RELEASE') {
excludes 'com.springsource.javax.servlet',
'com.springsource.org.aopalliance',
'com.springsource.org.apache.commons.logging',
'org.springframework.aop',
'org.springframework.beans',
'org.springframework.context',
'org.springframework.core',
'org.springframework.web'
}
// specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes eg.
// runtime 'mysql:mysql-connector-java:5.1.5'
}
With this, IntelliJ "knows" about the Spring Security jars.
The problem in my case (9.0.2) was that the grailsPlugins did not have grails library on it's classpath. So the compile dependencies would not work, and your fix fabien is a bit to verbose :-)
IDEA normally has a great auto error fix (alt + enter) and it works in this case to. It will ask you to "Add grails user library to classpath". Works like a charm but should offcourse not be necessary
Open Project Structure Window
in Project Settings choose Modules
then choose the plugin module. It should be named "myproject-grailsPlugin"
then select the tab dependencies
Inside the dependent modules, select Grails User Library (if it does not exist, create it with Add -> Global Library)
EDIT this library and inside the Configure Module Library dialog, choose "Attach JAR directories"
Select the lib directory of the spring-security-code plugin. O windows it should be located ate : C:\Users\myuser.grails\1.3.1\projects\myproject\plugins\spring-security-core-0.3.1\lib
That's it !
why not just right click on your project root directory and click 'synchronize grails dependencies'...
It seems like intellij 9.0.3 does not correctly pick up dependencies from plugins when you change the plugin directory to something other other than default one in BuildConfig.groovy
grails.project.plugins.dir = "./plugins" //breaks plug-ins in intellij
Class not found errors started appearing after changing the plugin directory. I added the dependencies from the modules (namely spring-security-core and springcache) and now intellij adds the jar dependencies to 'Grails user library' and I can compile my app. Using the command line never had any issue.
Here's what I added to BuildConfig for springcache.
dependencies {
//for spring-security-core
// for springcache
compile("net.sf.ehcache:ehcache-web:2.0.0") {
excludes "ehcache-core" // ehcache-core is provided by Grails
}
}

Resources