How to add a "non-mavenized" jar dependency to a grails project - grails

I'm a grails newby. I am using grails 2.0.3 from the command line (no IDE) on windows 7.
I am just running the 'hello world' example from the grails documentation.
I have a custom jar that is not in maven that I would like to use in my project. I added it to the lib folder and did a refresh-dependencies. I modified the controller to import a class from the jar. When I did 'run-app', the compiler complained that it could not find the class.
I saw several example online where people had to add a line to their BuildConfig.groovy like this: runtime 'httpclient:httpclient:3.0.1' . But since I'm not using maven or ivy, how do I get that jar on my classpath?
The example I am referring to above is http://grails.1312388.n4.nabble.com/External-Jar-in-Grails-td4388010.html

Grails 2.0-2.2
Ok, I finally figured out what I need to do. Suppose my jar is named 'mylib.jar'. I need to rename it to mylib-1.0.jar and move it to the lib directory. Then I need to add the following to BuildConfig.groovy:
dependencies {
// specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes eg.
// runtime 'mysql:mysql-connector-java:5.1.16'
runtime 'mylib:mylib:1.0'
}
I guess that was obvious to everyone but me. Hope this helps someone else.
Grails 2.3-...
Just put a jar into the lib/. Do not modify BuildConfig.groovy (c) kaskelotti

if your jar is in lib folder and also entry in buildconfig.groovy and if you can't find jar in your build path then try to use below command.
grails compile --refresh-dependencies

Related

correct location for putting the runtime jar file dependency

the dependencies specified in the build config file of my grails application is as follows:
dependencies {
// specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes eg.
runtime 'mysql:mysql-connector-java:5.1.19'
runtime 'org.imgscalr:imgscalr-lib:4.2'
runtime 'net.authorize:anet-java-sdk:1.4.6'
//runtime 'net.authorize:anet-java-sdk:1.8.1'
runtime 'org.apache.httpcomponents:httpclient:4.0.1'
}
I need to upgrade a library i.e replace this line
runtime 'net.authorize:anet-java-sdk:1.4.6'
with
runtime 'net.authorize:anet-java-sdk:1.8.1'
I think i also need to put the jar file at the correct location since it is specified as a runtime dependency. I have downloaded the correct jar file i.e anet-java-sdk-1.8.1.jar. I only need to know the correct location to put this file into. Can you tell me the correct location in the grails project structure where i should put this jar file and also if there is anything else i need to do for correct upgrade of this library? I appreciate your help! Thanks!
when you put in BuildConfig.groovy the following
runtime 'net.authorize:anet-java-sdk:1.8.1'
grails will search the library in the repositories you have defined (generally just above) in the part grails.project.dependency.resolution.repositories so you do not have to do anything and if you're using a standard library, it will be found in maven central and be downloaded for you.
If you do not use a open-source library or it is not pushed to any repo you can access, you need to download the jar file and put it under the lib/ folder and grails will add this file in the classpath
You can read more about dependency resolution (be sure to change the version with the one you're using, link is for 2.5.0) and all the settings that can be changed

How do I add a JAR to the classpath of a Grails project only for test scope?

Normally, I'd declare a test dependency in BuildConfig.groovy for the test scope and know it'll only be in the classpath for testing, and not packaged into the deployed application:
test "org.seleniumhq.selenium:selenium-htmlunit-driver:2.27.0"
I have another JAR I'd like to include at test time, but it's not available in a maven repository, so I can't declare a dependency like I normally would.
How can I add the JAR to my project, check it in, but declare it only available at test time?
I'm currently working in Grails 2.1.1, but 2.3.x is in the future.
Thanks.
I'm pretty sure that all jars in the lib folder are compile scoped, so you cannot change it to test.
What you can do is exclude them in the build process, using the config grails.war.resources in BuildConfig.groovy:
// Remove the JDBC jar before the war is bundled
grails.war.resources = { stagingDir ->
delete(file:"${stagingDir}/WEB-INF/lib/jdbc2_0-stdext.jar")
}
This was taken from the Palmer's post.

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.

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
}
}

jar file in Grails project lib directory not found

I have a grails project and a jar file in the lib directory of the grails project. The problem is when i run the app I get class not found errors on classes within the jar file. Why can these not be seen by the grails app?
From your last response, I believe you're missing some more required jar files. You should probably check if there are any other jars required for Esendex. Hope it helps.
The lib directory is the right place for JAR files. With only the information provided above, I've no way of guessing why it's not working for you. All I can suggest is:
Run "grails war" on the command line
Unzip the created .war file and check that the JARs are in the WEB-INF\lib directory
If you're running Grails 1.2 you might want to check that the JAR isn't excluded by the new dependency resolution DSL.
Depends on how the classes are used. Once you've added them to your lib directory, try updating the dependencies config in BuildConfig.groovy. Here's mine:
dependencies {
// specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes eg.
runtime 'org.apache:commons-exec:1.1'
runtime 'org.apache:httpcore:4.1.3'
runtime 'org.apache:httpclient:4.1.2'
runtime 'org.json:json:20080701'
// runtime 'mysql:mysql-connector-java:5.1.16'
}

Resources