Why is Grails taking the jar dependencies instead of the existing maven project dependencies? - grails

I'm using IntelliJ IDEA 10.0.2 (with groovy/grails support), maven 2.2.1 and grails 1.3.6.
We have a big maven project, which depends on many other maven projects. Let's say the workspace structure looks as follows:
backend-project (Java project, without further project dependencies)
output-project (Java project, without further project dependencies)
frontend-project (Grails project, which dependes on both, backend and output)
That means, within my frontend-project's pom.xml I have defined 2 Project Dependencies:
e.g.
<dependency>
<groupId>com.company.project</groupId>
<artifactId>backend-project</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.company.project</groupId>
<artifactId>output-project</artifactId>
<version>${project.version}</version>
</dependency>
Let's assume that I change some Java Source within the output or backend project. When I
run the grails application now, then it won't consider the changes. I have to publish the changed artifact locally and then resolve it again by the grails project before running the application in order to take effect.
This tells me that the grails project just depends on the project dependency jars within the maven repository and does not care about any existing project dependency "sources" within the workspace.
Does it have to be that complicated and if so, why?
Note that if my frontend project was a spring web project, the changes will be seen in IDEA and tomcat will even reload the change dynamically.
Note that when IDEA recognizes a mavenized grails project, it won't run the grails project with: "grail run-app" anymore but with a more complicated version of: "mvn grails:exec -Dcommand=run-app". Don't know if this is of any relevance..
Thanks!
Mr. Slash

Maven always picks up the jar files from the repositories (local and then remote etc depending on your pom.xml config).
Think about it: How would your main project know where the backend-project or the output-project files are located?
If you want a direct dependency then remove it from pom.xml and modify the project build path to directly add the projects' outputs to your main projects. In Eclipse open the properties page of the main project => build path => projects => add.

Related

How to use RatingStars Vaadin addon

How to use RatingStars Vaadin addon?
I want to use RatingStars addon on all our vaadin pages so that we can get quick feedback from user.
Is there any example available which shows how to use that?
Using widgets with Vaadin (with or without Maven) usually means you have to do 3 things before starting your application:
adding the widget dependency to your classpath
compiling the widgetst
specifying your widgetset
From your description you've already done the first but you had some trouble, so let's take them separately and since you're already using Maven we'll just continue down this road:
1: Adding widgets
I used dependency in project but maven couldn't find this jar so it's failing
<dependency>
<groupId>org.vaadin.addons</groupId>
<artifactId>ratingstars</artifactId>
<version>2.1</version>
</dependency>
Some of the widgets, also known as add-ons, may not be available in the central maven repo, so make sure you add the Vaadin dedicated repo in your POM:
<repositories>
<repository>
<id>vaadin-addons</id>
<url>http://maven.vaadin.com/vaadin-addons</url>
</repository>
...
</repositories>
Now it should resolve and download your dependency, adding it to the classpath.
2: Compiling the widgetset
Then I manually downloaded that jar, unzipped it and kept that my source location. But it's still failing for
Failed to execute goal com.vaadin:vaadin-maven-plugin:7.6.1:compile (default) on project vaadin-widgetset: GWT Module org.vaadin.teemu.ratingstars.RatingStars not found in project sources or resources
Now, before you can run your app, you also need to compile the widgetset. If you've generated your project skeleton using the vaadin-archetype-application everything should be already configured and you simply need to run mvn package (or mvn vaadin:compile for just the compilation process).
Otherwise take a look at this sample project for a standard vaadin-maven-plugin configuration.
P.S. Depending on you IDE, you can also use dedicated IDE plugins for these tasks, but that's up to you.
3: Specifying the widgetset
Finally, depending on your servlet version and app setup you can define your widgetset location by:
using the web.xml file:
<init-param>
<description>Application widgetset</description>
<param-name>widgetset</param-name>
<param-value>com.example.MyWidgetSet</param-value>
</init-param>
annotating your UI with #Widgetset("com.example.MyWidgetSet")
Now you should be able to run your application and see the widget on your screen :-)

Grails Project missing Java library at runtime

I was trying to add some Java Libraries (AWS SDK for Java, Apache Commons Math, etc.) to my Grails project since some of my Java source code (placed in src/java) had dependencies. By following this answer, I was able to resolve compile errors by adding the jar files to the /lib folder and add it to the build path, as answered here: Add Java Libraries to a Netbeans Grails Project
However, when I call my Java source code from my controller during runtime, it is unable to find the Java libraries that I added, showing a NoClassDefFoundError. Should I be adding something to the BuildConfig.groovy file? I'm not sure what the name convention for the jar files to be added to the dependencies.
The question you refer to is 5 years old. You should use newer resources :)
The preferred approach now is to use dependencies in BuildConfig.groovy, and let Grails (via Ivy or Maven) download the jars for you once and reuse them for various projects.
It's not always obvious what the syntax is, and I find that http://mvnrepository.com/ is a great resource. For example if you search for "commons math" and click through to http://mvnrepository.com/artifact/org.apache.commons/commons-math you'll see a few versions. Click on version 2.2 and you'll see the Maven dependency XML but you can click on the Gradle tab and it's going to be similar to what you need for Grails. So I'd add
dependencies {
compile 'org.apache.commons:commons-math:2.2'
}
and if necessary change compile to runtime, build, etc. depending on what you need the jar in the build process.
In the rare case that you do have a jar that isn't available in a Maven repo (e.g. a shared library at your company) then you can put the jar file in the lib directory. As you've seen, Grails doesn't auto-detect it (this is as of version 2.0). But you can run grails compile --refresh-dependencies to get your jar added to the classpath.
My issue turns out to be the fact that AWS Java SDK had dependencies (Apache HTTP Client) that were not installed yet and that I was unaware of.
This is what I had to configure this for my BuildConfig.groovy file
dependencies {
runtime 'org.apache.httpcomponents:httpclient:4.2.5'
runtime 'com.amazonaws:aws-java-sdk:1.4.7'
}
All the dependencies for AWS Java SDK 1.4.7 can be found here: http://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk/1.4.7. All the dependencies outside of HTTP client were already installed for me, but may not be for your Grails setup.

How to make Grails get latest local jars from Maven repository?

In my Maven repo (.m2). there is one my local jar like sub_app-0.1.jar.
I have same copy of the it in ivy-cache.
I run maven install on sub_app then new sub_app-0.1.jar file created.
After, I run grails clean and it not getting my new sub_app-0.1.jar from .m2.
But, if I remove (delete) sub_app-0.1.jar file from ivy-cache and run the grails clean then it is getting new sub_app-0.1.jar file into ivy-cache.
If I change the version in sub-app pom and grails pom and Grails is taking latest one. install again it not taking.
I tried by adding SNAPSHOT to sup-app jar even. Same result, first time it is taking, after not.
ie. Grails considering only jar name and version in ivy-cache, if having - it not take. If not having - it taking from .m2.
But it not considering new/old build.
How can I get the same behavior (step 5) in step 4 also?
UPDATED
You can try adding changing=true to your dependency in the BuildConfig.groovy, as specified in the Grails Guide
compile ('YOUR_GROUP_ID:YOUR_SUB_AP:0.1') {
changing = true
}
Not sure if this is the same problem as you, but I use Spring Source Tool Suite (STS) with a 'grails' project having a dependency on an 'interfaces' project (which just contains interfaces, beans, pojos, etc).
If I run a maven install on interfaces in STS, then maven correctly updated with latest jar (I use '-1.0-SNAPSHOT' as my version number).
If I then run a grails clean on my 'grails' project in STS, Grails correctly identifies the change of the interfaces jar (I have {changing=true} in BuildConfig.groovy), downloads the pom, but fails to download the jar as it cannot delete the jar from the ivy-cache. Looks like STS has a handle on the ivy-cache which prevents this.
As I inherited this project from another developer who informed me it was a bug/feature of Grails and/or STS, I've bowed to his knowledge and workaround this by either:
maven package instead of install on 'interfaces' then copy the latest jar to the ivy-cache. It seems STS will let me replace the contents of the jar. Then grails clean uses the latest jar from ivy-cache (no attempt to download from maven).
maven install on 'interfaces', close down STS, delete jar from ivy-cache, re-open STS, grails clean which downloads latest jar from maven.
Both workarounds as a pain, so I'd be interested if anyone has any ideas?
Grails not supports to get latest of same (group-id, artifact-id, version) jar even SNAPSHOT jar also.
Solutions:
Every-time, delete the sub-app-version.jar from ivy-cache. or
Every-time, change the version to next value(should be greater than current value) of the sub-app jar in pom.xml.
4 Configuration - Reference Documentation
Please read:
4.7.6 Snapshots and Other Changing Dependencies

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.

GRAILS + Eclipse Project Dependencies

I am working on a Grails project that consists of a master grails-app and several grails plugins. One of the grails plugins is “Core” and contains several groovy and java domain and utility classes. Currently the core project is a grails plugin, however I’d like to pull the sources out of src/groovy and src/java into a Groovy class library that I’ll eventually package into a jar file.
I’d like to understand how to get this set up properly in Eclipse so that the plugins reference the new Groovy library and the application references the plugins and everything builds ok.
If I spin up a grails plugin, and then add the groovy project to the build path using eclipse, I can get the plugin to build fine. The issue is, now I add a plugin reference from the grails web application to this plugin and the grails application won’t build. I have added the Groovy library to the build path of the web application, but when grails tries to add the plugin it complains that it doesn’t know about the classes in my groovy library.
Here’s the project structure
server-core ( groovy project )
ia-security-plugin ( grails plugin project ) ( server-core is on the build path , builds fine )
server-core-web ( grails app project) ( references ia-security-plugin in Build.config ) ( won’t build )
During development add the following line to your BuildConfig.groovy
grails.plugin.location.'plugin-project'="../PluginProject"
where PluginProject is the eclipse project relative to your current project and plugin-project is the name of the plugin project. This takes the pain of rebuilding your plugins and all reference problems. You can even step debug through your main project into the plugin project.
For deployment time I have setup Artifactory with Maven Repository ID (on the plugin project) and [Main Project] BuildConfig.groovy to
compile (":plugin-name:latest.release")
along with
mavenRepo "http://location-of-local-artifactory/
Use http://grails.org/plugin/release for release management and repository setup.
Hudson automatically picks up the plugin and builds the war file on the build machine.
Alternatively you could simply build the war file and deploy to the server if your project is composed of just few developers.

Resources