Search code examples
grailsimportjar

How to import a proprietary library in Grails 4.0.3


I am trying to import a proprietary jar lib (ICOMConector.jar) in my grails 4.0.3 project, but it is sending me an error.

In IntelliJ, I right-clicked the project folder and accessed Open Module Settings. Then I clicked in Libraries and chose my jar. Right after, clicked in Module / Dependencies and chose that jar in a compilation scope.

In build.gradle file I put this flatDir, because the jar in my project grails stardard structure:

repositories {
        maven { url "https://repo.grails.org/grails/core" }

        flatDir {
            dirs 'lib'
        }
    }

And this in the same build.gradle, I've tried all of that, including those now commented, but the error persists.

dependencies {
    //implementation name: 'lib/ICOMConector.jar'
    //runtime files('lib/ICOMConector.jar')
    //runtime fileTree(dir: 'lib', include: '*.jar')
    compile fileTree(dir: 'lib', include: ['*.jar'])
}

when I send grails run-app in the command line, the error is:

| Running application...
startup failed:
/media/alfredo/1TBHDD/CMB/Code projects/Grails 4/detran-mspid/grails-app/init/detran/mspid/BootStrap.groovy: 5: unable to resolve class com.workers.icom.ICOMConector
 **@ line 5, column 1.
   import com.workers.icom.ICOMConector
   ^

1 error**


FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileGroovy'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

I tried to instantiate the class in Bootstrap.groovy, to make it easier to verify if it is working as expected.

Could anyone help me to import this library. A friend of mine did this in a Spring boot project and it worked, so the problem seems to be a wrong configuration in Grails.

package detran.mspid


import com.workers.icom.ICOMConector


class BootStrap {

    def init = { servletContext ->
        ICOMConector icom = new ICOMConector()

    }
    def destroy = {
    }
}

Solution

  • This is what I have in my build.gradle for using a library in grails 4.0.3 (been this way since at least grails 3, though). This is almost identical to one you tried with runtime scope.

    compile files("lib/opencsv-2.3.jar")
    runtimeOnly fileTree(dir: './lib', include: ['*.jar'])
    

    It's entirely possible that either one or both of those is unnecessary...