Read jars from lib folder in grails - grails

I want to parse an excel file using the Apache POI library to boot strap some data in development mode into my grails 2.0.1 application.
I've tried to use the Grails excel-import plugin but the plugin uninstalls automatically when I execute run-app.
Because of that I decided to got ahead without the plugin for now. First I have copied the next jars into the grails application lib folder
$ls -la lib
poi-3.7-20101029.jar
poi-ooxml-3.7-20101029.jar
poi-ooxml-schemas-3.7-20101029.jar
xmlbeans-2.3.0.jar
I've read that I should declare the dependency in grails-app/conf/BuildConfig.groovy. So, I added the next:
dependencies {
// specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes eg.
compile ('org.apache.poi:poi:3.7', 'org.apache.poi:poi-ooxml:3.7')
// runtime 'mysql:mysql-connector-java:5.1.16'
}
However when I execute run-app the application is still not able to find the jars.
grails> run-app
| Compiling 76 source files
| Compiling 30 source files.
| Error Compilation error: startup failed:
UMEExcelImporter.groovy: 8: unable to resolve class org.apache.poi.xssf.usermodel.XSSFSheet
# line 8, column 1.
import org.apache.poi.xssf.usermodel.XSSFSheet
^
UMEExcelImporter.groovy: 7: unable to resolve class org.apache.poi.xssf.usermodel.XSSFWorkbook
# line 7, column 1.
import org.apache.poi.xssf.usermodel.XSSFWorkbook
^
UMEExcelImporter.groovy: 9: unable to resolve class org.apache.poi.xssf.usermodel.XSSFRow
# line 9, column 1.
import org.apache.poi.xssf.usermodel.XSSFRow
I am not using any IDE. Any feedback is welcome!

The POI jars are on public Maven repo's so try this:
Remove the jars from your lib folder
Clean your build with: grails clean
In you BuildConfig.groovy ensure that the "repositories" closure has "mavenCentral()" included/uncommented
Your dependency looks ok (I have not confirmed it) so now try the run-app
HTH

instead of adding compile dependency, try adding runtime dependency as follows.
dependencies {
// specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes eg.
runtime ('org.apache.poi:poi:3.7', 'org.apache.poi:poi-ooxml:3.7')
// runtime 'mysql:mysql-connector-java:5.1.16'
}

Related

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 = {
}
}
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...

Gradle cannot find plugin

I have the following in my build.gradle of my Grails 3 project.
dependencies {
...
compile "org.grails.plugins:spring-security-core:3.0.3"
// The following line was the true cause of my problem
compile("org.grails.plugins:spring-security-oauth-google:0.3.1")
...
}
Now when I run Gradle I get this error:
Error Error initializing classpath: Could not find spring-security-core.zip (org.grails.plugins:spring-security-core:3.0.3).
Searched in the following locations:
https://repo.grails.org/grails/core/org/grails/plugins/spring-security-core/3.0.3/spring-security-core-3.0.3.zip (Use --stacktrace to see the full trace)
If I go to the specified path I do see the plugin but just as reported there is no zip file.
What am I doing wrong here?
The root for this problem I found in a little bit different place than expected.
I am working on upgrading a Grails 2.5 project to Grails 3 and I had lead myself on a wrong track. I needed to include spring-security-oauth-google and when it did not work as expected I also included spring-security-core which then derailed me.
The cause of this problem is dependencies being loaded from dependencies I have in my own project. In order to avoid loading those dependencies - called transitive dependencies - the following has to be done:
plugins {
compile "org.grails.plugins:spring-security-core:3.0.3"
compile("org.grails.plugins:spring-security-oauth-google:0.3.1") {
exclude group: 'org.grails.plugins', module: 'spring-security-core'
}
}
This will load the spring-security-core 3.0.3 correctly ignoring the dependency from spring-security-oauth-google.

Grails 2.1.2 - UNRESOLVED DEPENDENCIES Error

Am using Grails 2.1.2 on Windows 7 64-Bit System, at the time of compile it gives below error, kindly help me how to fix this.
::::::::::::::::::::::::::::::::::::::::::::::
:: UNRESOLVED DEPENDENCIES ::
::::::::::::::::::::::::::::::::::::::::::::::
:: org.grails.plugins#mail;1.0-SNAPSHOT: not found
:: org.grails.plugins#tomcat;1.3.2: not found
:: org.grails.plugins#hibernate;1.3.2: not found
:: org.grails.plugins#csl-rest-lib;1.0: not found
::::::::::::::::::::::::::::::::::::::::::::::
In BuildConfig.groovy, try to declare the dependencies:
dependencies {
compile 'org.grails.plugins:mail:1-0-SNAPSHOT'
compile 'org.grails.plugins:tomcat:1.3.2'
...
}
If your application was updated from grails 1.3 (<2.) or create from grails 2., you need to check plugins version on the grails plugins
For example, current version of mail plugin is 1.0.1 mail plugin
And also you need to check this dependencies at conf/BuildConfig.groovy. It must have part like this:
plugins {
build ":tomcat:$grailsVersion"
runtime ":hibernate:${grailsVersion}"
runtime ":resources:1.1.6"
...
Describe your plugins at plugins part.
And you need to check latest libraries for plugins and describe it like this:
dependencies {
// specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes eg.
compile('org.apache.poi:poi:3.7',
'org.apache.poi:poi-contrib:3.6',
'org.apache.poi:poi-ooxml:3.7',
'antlr:antlr:2.7.6',
'org.apache.ant:ant-apache-log4j:1.7.1'
..

Trouble including external jar required for an import in a src/java/{somename}.java in a grails-app( grails2.0)

I need a DHTMLXCONNECTOR.jar (a connector executable jar) in my grails app to use with my application otherwise i get errors like :
error compiling in javac connot find symbol : import com.dhtmlx.connector
please help !I tried grails install-dependencies dhtmlxconnector .. Is it, that i need to edit the buildconfig file ? like something similar to uncommenting runtime for mysql ?
(grails 2.0.1)
Add your dependency to the dependencies section of BuildConfig.groovy.
For example to add the dependency with
groupId: org.jsoup
artifactId: jsoup
version: 1.6.1
dependencies {
compile 'org.jsoup:jsoup:1.6.1'
}

how to delete guilty jar file after grails dependency-report

when I tried to run grails -Dgrails.env=local run-app, I got the below error
Server failed to start: java.lang.LinkageError: loader constraint violation: loader (instance of ) previously initiated loading for a different type with name "javax/management/MBeanServer"
After analysing I understood that it has something to do with "Two dependencies link the same jar with different versions"
I ran grails dependency-report, and here is the observation:
commons-beanutils by commons-beanutils 1.8.3 release default false 227 kB
commons-beanutils by commons-beanutils 1.8.0 release default true 0 kB(evicted by 1.8.3)
How do I exclude this jar or remove the linkage?
The dependency report should show what is pulling in the problematic jar. Once you have that, explicitly exclude it from the dependencies in your BuildConfig.groovy, like so:
grails.project.dependency.resolution = {
dependencies {
runtime("i-depend-on-beanutils-1.8.3") {
excludes "beanutils"
}
}
}

Resources