Grails is able to configure dependencies when you specify them in the BuildConfig.groovy file. Usually when you add it there and call grails compile --refresh-dependencies it will resolve the dependency, and download to .grails/ivy-cache/..... (in my case). However, one time, after downloading the jar files, it failed to automatically add the jar to the classpath. Does anybody have any idea on how this will happen? It has worked for me before on many other Maven repository dependencies. The specific dependency I failed to add to my Grails project classpath is http://mvnrepository.com/artifact/javax.mail/mail/1.4.7
I can just manually add the lib to the classpath, but I'd rather have dependencies resolved automatically with the BuildConfig.groovy file. I also can't manually add jars into the "Grails Dependencies" library in the classpath; they can only be outside that library.
Related
I have a grails app and the groovy under /src/groovy has import statements for Java POI HSSF
for example
import org.apache.poi.hssf.usermodel.HSSFWorkbook
import org.apache.poi.hssf.usermodel.HSSFCellStyle
What is the best way to add POI to my grails app so its deployed with the war. I tried adding the jars to /lib and then adding them as runtime dependencies in BuilConfig.groovy.
dependencies {
runtime 'mysql:mysql-connector-java:5.1.22'
runtime 'poi-3.9-20121203'
runtime 'poi-ooxml-3.9'
runtime 'poi-ooxml-schemas-3.9'
runtime 'xml-apis-ext-1.3.04'
runtime 'xmlbeans-2.3.0'
runtime 'xmlpull-1.1.3.1'
runtime 'xstream-1.4.7'
}
I also tried adding it as a compile plugin this way
plugins {
runtime ":hibernate:3.6.10.13"
compile ":excel-import:1.0.0"
}
Both generate this error on grails war
java.lang.NoClassDefFoundError: _GrailsClasspath_groovy$_run_closure1
EDIT: my repositories has maven but still complains about missing class def Read jars from lib folder in grails
While adding any dependency or plugin in Grails, maven artifact semantics has to be followed instead if the plain jar name. For example,
runtime 'xmlbeans-2.3.0'
should be
runtime 'org.apache.xmlbeans:xmlbeans:2.3.0'
which corresponds to
runtime '<groupId>:<artifactId>:<version>'
Same is applicable for all the other dependencies and plugins. Rewrite those one by one and remove all jars from lib directory. Clean and compile the app subsequently.
I'm able to successfully build the Maven assembly plugin in my project and generate a jar file with all the needed dependencies. But now I also want to instruct Maven after building me the jar file with dependencies, go into the target folder where the jar file with dependencies is located and run my main program.
Should I consider looking into the Maven Exec Plugin for what I want to acheive?
Yes the exec-maven-plugin is the right choice. The question is if you like to start the assembled jar archive or just a java class with it's dependencies.
I use Grails 2.2.3. I have put jar file in lib directory, IDEA immediately resolved the dependency. But when I start app I get NullPointerException on class from this library. If I try it second time or more I get java.lang.NoClassDefFoundError. I found a lot of advice how to resolve this issue but none were useful in my case.
Library (mylib-1.jar) compiled in maven and added to lib dir. In BuildConfig.groovy, dependency is mentioned as:
dependencies {
compile 'com.mylib:mylib:1'
}
I tried
grails clean
grails compile --refresh-dependencies
grails refresh-dependencies
but nothing helps. In result war file I can see this library in WEB-INF/lib, but even if deploy this war I get the same error.
How can this be resolved?
You're confusing NoClassDefFoundError with ClassNotFoundException. ClassNotFoundException happens when a class you want isn't there, but you get a NoClassDefFoundError when the class is there, but a class it depends on isn't. So you're missing another jar file that this jar file depends on.
This is one of the many reasons why it's best to use dependency management instead of manually copying jar files to the lib directory. If you use a Maven repo where the jars have proper POM files, their dependencies are specified, and the resolver can download the entire tree of dependencies for you, rather than you having to find all of the jars yourself.
How do I reference a dependency by convention within my project build path? Allow me to elaborate...
I'm using the Groovy/Grails Tool Suite (GGTS 3.0). I'm referencing the dependency as such in my BuildConfig.groovy:
dependencies {
compile 'tgt:jaxb-utils:1.0'
}
The referenced jar file is successfully pulled down from the Artifactory repo - I can find the jar file on my local file system in the Ivy cache. When I run any grails targets (grails compile, grails run-app, grails run-tests), it works fine - meaning my code that imports the classes from the jar has no problems. When I run grails war, the referenced jar file is packed into the constructed war.
However, and here is the crux of this post - the project build path does not reference this jar file by default or convention, so my java or groovy code that imports the classes from the jar file reports a unable to resolve class ... error.
One solution is to simply add the jar file to the lib folder and update the build path accordingly, or modify the build path to reference the jar file in the Ivy cache folder. However, I'd have to do this for any/all dependencies pulled down in this way. Any jars in the lib folder will get saved to the team source code repository, and that seems like wasted space since the grails commands work fine with just the dependency reference in BuildConfig.groovy.
Am I just being too idealistic (ie - difficult) here, or is there a better way to clean up the unable to resolve class ... errors in my IDE without having to manually add the dependent jar files to my lib folder and update my build path? Ideas?
Eclipse / STS / GGTS : If you have Grails plugin installed, you can do the following :
Right click on your project -> Grails Tools -> Refresh dependencies (or shortcut "Alt+G, R")
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.