How to add Java JAR files to Grails project?

24,666

Solution 1

Putting the jar in the lib folder should do the trick. We are using the same approach currently without problems.

Solution 2

If Grails by default don't take the local .jar libraries located in <GRAILS-APP-DIR>/lib (seems that with Grails 3.X the /lib folder default configuration is removed) the easy way to enforce it is modifying build.gradle to add a local directory dependency for it.

For almost all cases is of course better to use the maven repos, however it's possible to have some db-vendor-drivers or other libraries which aren't in a public repo. To do so for this libraries modify the <GRAILS-APP-DIR>/build.gradle and add something like:

dependencies {
    ...
    compile fileTree(dir: './lib', include: ['*.jar'])
    ...
}

If you want you can use another directory (not /lib) since you're specifying it. Of course use the correct scope (for example for db-vendor-drivers which probably already are in your application container class path the scope in gradle will be runtime instead of compile).

UPDATE

Starting from gradle 7.0 use of compile and runtime are removed. Insted you can use compileOnly, runtimeOnly or implementation depending if you want to put the dependency only in the complie classpath, runtime classpath or on both, in gradle 7.0 and above you can use :

dependencies {
    ...
    implementation fileTree(dir: './lib', include: ['*.jar'])
    ...
}

Solution 3

Adding the jar to the "lib" folder wasn't enough for me. I had to run:

grails compile --refresh-dependencies

After what it worked.

Share:
24,666
Eric Wilson
Author by

Eric Wilson

Software developer, experienced in Java and Python in Linux environments, formerly a professor of mathematics. I'm a father of five children, and a husband of one wife.

Updated on April 21, 2020

Comments

  • Eric Wilson
    Eric Wilson about 4 years

    I have a few Java/Maven projects that I want to use in a Grails 2.0.4 project. I have tried various approaches, such as:

    1. Installing the JAR files in my local maven repository, and executing grails install-dependency com.foo:my-project:0.0.1-SNAPSHOT
    2. Copying the JAR files into the lib folder of grails.
    3. Adding compile com.foo:my-project:0.0.1-SNAPSHOT to the dependencies section of BuildConfig.groovy
    4. Uncommenting mavenLocal() and passing /home/me/.m2/repository

    I may have tried a few other things, such as superstitiously throwing in a grails clean whenever convenient. In every case grails compile yields the following result: compilation fails, unable to resolve every reference to any of the classes in either of the JARs.

    Any idea what I'm missing?

  • Dem Pilafian
    Dem Pilafian about 9 years
    The default lib folder is gone as of Grails 3.0. grails.github.io/grails-doc/3.0.x/guide/single.html#upgradin‌​g --> "Dependency resolution should be used to resolve JAR files"
  • Eric Wilson
    Eric Wilson about 8 years
    I'm not sure why you give such careful answers to ancient questions. I hope this helps someone, but I won't try to evaluate it. I haven't done any Grails in over three years.
  • albciff
    albciff about 8 years
    @EricWilson I'm just trying Grails for first time..., I see that this question has only answers with the thing to do but not explanation about how it works. So I simply try to argue how it works. Don't worry I'm not looking that you check the answer :), I only post it if it helps someone.