Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a “non-mavenized” jar dependency to a grails project (Grails 3.x)

Tags:

jar

grails

I'm trying to find documentation and code samples on how to add a local / non-maven jar file to my Grails 3.x project?

I found the separate thread How to add a non-maven jar to grails - but that's only to grails 2.3, and the file structure and configuration has undergone a big overhaul in 3.x.

Any help and (especially) code samples would be wonderful! The .jar is in the local project directory, and I intend to package with the .war for deployment.

Additionally, once i add the dependency, should i just be able to call it's methods from the controller & service files? or do i need to include them in those as well?

thx!

like image 446
Michael Kim Avatar asked Sep 01 '15 19:09

Michael Kim


1 Answers

Grails 3 uses Gradle, so there's nothing Grails specific about including a local jar. It's as easy as adding a file dependency to the dependencies block of your build.gradle file.

Per the Gradle documentation on File Dependencies:

To add some files as a dependency for a configuration, you simply pass a file collection as a dependency:

dependencies {

    ...

    compile files('libs/a.jar', 'libs/b.jar')
    // or
    compile fileTree(dir: 'libs', include: '*.jar')
}

The above example shows two ways to include jars that exist in a local libs/ directory; you can do either/or. The jar(s) can be anywhere on the filesystem, just make sure you point to the correct path.

To use the classes from the dependency in your application, you'll include them in your services, controllers and all other classes like you normally would. Say libs/a.jar has a class org.example.Something, you'd add an import to the top of your Grails class like so:

import org.example.Something
like image 50
dpcasady Avatar answered Oct 11 '22 08:10

dpcasady