Grails - Maven - STS - grails

I am relatively new to this and still bit puzzled how the Maven dependencies (in pom.xml) and the Grails dependencies (in BuildConfig.groovy) hang together.
When I choose Convert to Grails Project... in STS (Springsource Tool Suite) it seems to list the different (i.e. the Grails) dependencies.
Do I have to keep them in synch if I wanna receive a WAR file with mvn package/install? Sometimes I get weird build error and I am always unsure where to look for a fix.
Can anyone shed some light on this please?
Regards
Jonas

It sounds like you are using a mavenized grails project. See this bug report:
https://issuetracker.springsource.com/browse/STS-596
When working with a mavenized grails dependency you can work with a grails project, but make sure to turn off dependency management from right-click -> Grails Tools -> Disable Dependency Management.
There is not much tooling support for these projects since the STS grails tooling relies on calling grails to determine project structure and dependencies. But, when using maven, grails is not called directly and the Grails tooling is not initialized.

Related

Grails 3 Application or Plugin

If I'm looking at project source code (in Intellij) how can I tell whether the Grails source code I'm looking at is an Application or a Plugin?
I get it that the output of a Grails Application build is a WAR, and a JAR for a Plugin but I can't figure out how to tell the difference by looking at the source code.
Bonus question: If it is a multi-module project, how do I tell which module is the Application and which modules are the Plugins? Or am I missing some important concept here?
Plugins can be run as applications as well and will often have an Application.groovy file. Plugins will have a <pluginname>GrailsPlugin.groovy file which sets up the plugin. In grails 3, this is in /src/main/groovy file structure.

Adding in-place plugin to grails 3 project

In grails 2.x, we were allowed to add an in place plugin by adding following in BuildConfig.groovy
grails.plugin.location."my-plugin" = "../my-plugin"
My question is, can we add our local plugins similarly in-place in grails3.0 as well or there is some other way to do this in grails.
Actual purpose is to test the plugin whether it's working properly or not before pushing it to bintray.
Yes, there is. Grails 3 is based on Gradle so multi-project gradle builds solve your issue.
Basically you add dependency as:
compile project(':../my-custom-plugin')
and has to modify settings.gradle to include plugin:
include '../my-custom-plugin'
Check Grails documentation on Plugins and Multi-Project Builds in http://grails.github.io/grails-doc/latest/guide/plugins.html
Other way is to install plugin in local maven repository using gradle publishToMavenLocal command and resolve if from there, before publishing to Bintray or other dependency repository.
Additionally since Grails 3.1.1, reloading is now supported for 'inline' plugins. Check https://github.com/grails/grails-core/releases/tag/v3.1.1 and http://grails.io/post/138665751278/grails-3-gradle-multi-project-builds
It is done using grails { plugins { syntax. Copied from docs:
grails {
plugins {
compile ":hibernate"
compile project(':myplugin')
}
}
This multi-project thing is a bit too big to answer in a short post. I just recently started with it, but, thankfully, I now have the hang of it. There's a tutorial on my site with a plugin handling the domain classes and services and all other sub-projects (just one, a web application in this example) using the plugin. The code is also downloadable. Here's the link: http://www.databaseapplications.com.au/grails-multi-app.jsp Make no mistake, there are a few things to watch out for.

How do I use Hibernate in a dependency of a Grails project (IntelliJ Module dependency)?

I have a Grail application that references a Java library (as an IntelliJ Module dependency). This works, but as soon as I add hibernate as a dependency of the Java library the Grails will no longer run.
Loading Grails 2.0.1
Error Error executing script RunApp: Provider for javax.xml.parsers.SAXParserFactory cannot be found (Use --stacktrace to see the full trace)
In my Java module, I am adding hibernate with the following:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.0.0.Final</version>
</dependency>
I can't see any good way around this. The Grails dependency-report does not show any libraries in conflict. The error occurs if the hibernate plugin is installed in the Grails application or not.
Bear in mind that (in this case) the Java library is not being incorporated via the BuildConfig.groovy. It is being incorporated as an IntelliJ Module dependency. If I incorporate the module as a jar via BuildConfig.groovy, everything works, but I lose the ability to step into the Java code.
Clarifying:
Per the JetBrains folks, the Java library is incorporated both as an IntelliJ Module dependency and in the BuildConfig.groovy. When executed from the command line, the project works, this is only an issue when starting from the IDE.
Suggestions?
Return dependency to java library to BuildConfig.groovy and use last version of IDEA: http://confluence.jetbrains.net/display/IDEADEV/IDEA+11.1+EAP . Navigation should work fine.
After experimentation, I stumbled upon the following which seems to be working very well (at least in IDEA 11.1):
Your Java library JAR should be referenced from the
BuildConfig.groovy (we use Maven, so we had to add the local Maven
repo as well)
Also reference your modules as module dependencies of the Grails module
(this is critical) in the run config for your grails project, uncheck the "add --classpath ..." option
The module dependency gives you:
Immediate awareness of the Java classes and their methods from the Grails project
Support for stepping into the Java code from your Grails project.
The BuildConfig reference gives you:
support for the grails commands, including run-app (which is how IntelliJ kicks things off when running/debugging a project)
If you leave the --classpath option checked, then you foul up the way that grails resolves its dependencies. There may be a better way to do this, but I haven't found it.
Additional Note
There's a bug in the interaction of grails and maven which causes grails to not pull in dependencies from local Maven 3 repositories if the pom.xml wasn't changed.
Therefore, our complete dependency refresh cycle looks like:
goto top
mvn clean install
find the relevant POM files in the repo and touch them
back to grails app directory and grails refresh-dependencies
run the app
You only need to do this when there are updates to the upstream Java libraries.
Hat tip to Sergey from Jet Brains for tracking that one down.

How can I easily link sources for dependencies in Grails project in IntelliJ IDEA?

Is there any way to get IDEA to automatically download sources for Grails, my plugins and all the dependencies? Alternately, is there an easy way to get IDEA to pickup sources downloaded by this plugin?
http://www.grails.org/plugin/eclipse-scripts
It puts them under ~/.ivy2/...
IDEA should automatically load all plugins for a Grails project, if the Grails/Griffon plugin is being used.
I see all the Grails plugins for a given project in my Grails View, under Plugins.
If you aren't seeing the files, try right-clicking on the project's name in the Grails View, and choosing Grails > Synchronize Grails settings.
Please note that if you are using the free (community) version of IntelliJ IDEA, it does not include the necessary plugins for working with Grails directly. You must pay for the full version to get it.

Grails dependencies or Maven

I'm using STS latest version (2.7) and latest grails as well. After I created a freshly grails project in STS it freezes in the updating grails dependencies for a long time and it comes out as a timeout error. And it follows by some GroovyObject error, but the project still runs fine in command-line. So I ditched STS for refreshing dependencies, and I included the dependency in BuildConfig.groovy and use grails compile but I still got ClassNotFound error when compiling, it's like grails didn't pick up the Ivy CLASSPATH. So, I'm thinking of changing it to Maven to resolve any dependencies.
My question would be which one is better and integrated seamlessly in STS or Eclipse?, but I still want to use grails commands, not maven.
If your project is just a grails project - use grails dependencies.
I've experience with mavenized grails project, and plain grails project. First is good when you have multimodule project, and only one of them is grails module. At this case you'll lost all grails commands, you need to use commands like mvn grails:run-app and it's complicated thing to pass arguments into commands :(

Resources