Grails 2.0 depedencies NoClassDefFound issue - grails

I've got a problem with NoClassDefFound exception in Grails 2.0 when I tried to use library from external JAR.
I've checked that declared JARs are inside of created WAR, also grials dependecies-report do not marks any issues with that.
Locally added JARs or downloaded from Maven repo seems no difference. I've also tried to clean IVY cache and clean grails project without success.
Did you got any ideas how to fix it?
BuildConfig.groovy (part of)
grails.project.dependency.resolution = {
inherits("global") {
// uncomment to disable ehcache
// excludes 'ehcache'
}
log "error" // log level of Ivy resolver, either 'error', 'warn', 'info', 'debug' or 'verbose'
checksums true // Whether to verify checksums on resolve
repositories {
inherits true // Whether to inherit repository definitions from plugins
grailsPlugins()
grailsHome()
grailsCentral()
mavenCentral()
mavenLocal()
mavenRepo "http://snapshots.repository.codehaus.org"
mavenRepo "http://repository.codehaus.org"
mavenRepo "http://download.java.net/maven/2/"
mavenRepo "http://repository.jboss.com/maven2/"
}
dependencies {
compile ( "javax:activation:1.0",
"javax:mail:1.0",
"com.google.gdata:gdata-core:1.0",
"com.google.gdata:gdata-client:1.0",
"com.google.gdata:gdata-media:1.0",
"com.google.gdata:gdata-youtube:2.0"
)
runtime ( "javax:activation:1.0",
"javax:mail:1.0",
"com.google.gdata:gdata-core:1.0",
"com.google.gdata:gdata-client:1.0",
"com.google.gdata:gdata-media:1.0",
"com.google.gdata:gdata-youtube:2.0"
)
}
...
}
LibraryController.groovy
import com.google.gdata.client.youtube.YouTubeService
import com.google.gdata.data.youtube.VideoEntry
import com.google.gdata.util.ServiceException
class LibraryController {
private YouTubeService service
private static final API_URL = "http://gdata.youtube.com/feeds/api/videos/"
def index = {
service = new YouTubeService("app")
}
}
Exception
Class
java.lang.NoClassDefFoundError
Message
Could not initialize class com.google.gdata.client.youtube.YouTubeServiceClass
java.lang.NoClassDefFoundError
Message
Could not initialize class com.google.gdata.client.youtube.YouTubeService

NoClassDefFoundError is not the same as ClassNotFoundException. Getting a ClassNotFoundException means the class isn't there, so you have a straightforward jar/dependency problem. NoClassDefFoundError means that the specified class was found, but that a class that it references wasn't found. It's a much more frustrating issue to track down because the JVM doesn't tell you what's missing.
You need to make sure that you have all of the dependencies of the class that's failing to load, and all of their dependencies, etc.

You have all your dependencies declared both in compile and runtime scope. Each dependency should be declared only once. If you declare a dependency in compile scope, it will also be available runtime. Since you need this class for compilation, you should keep com.google.gdata:gdata-youtube:2.0 under 'compile', and remove it from 'runtime'
A description of the available scopes, taken from the user documentation:
build: Dependencies for the build system only
compile: Dependencies for the compile step
runtime: Dependencies needed at runtime but not for compilation (see above)
test: Dependencies needed for testing but not at runtime (see above)
provided: Dependencies needed at development time, but not during WAR deployment

Related

Grails plugin dependency pulled into maven cache, but main project can't resolve classes

Using Grails 2.5.4
I have a plugin that uses the Twilio SDK (7+).
The plugin packages without error.
When I run the main project it throws an error saying that the it
cannot resolve the import com.twilio.Twilio class in the plugin
controllers/services.
I confirm that the Twilio SDK has been pulled into the local Maven
cache when the plugin was pulled in, so it knows to get the dependency.
The main project BuildConfig includes mavenLocal() in the dependency
resolution.
Where do I look next to find the cause of this dependency resolution problem?
plugin dependencies.groovy
grails.project.dependency.resolver = "maven"
grails.project.dependency.resolution = {
// inherit Grails' default dependencies
inherits("global") {
// excludes 'ehcache'
}
log "warn" // log level of Ivy resolver, either 'error', 'warn', 'info', 'debug' or 'verbose'
legacyResolve false // whether to do a secondary resolve on plugin installation, not advised and here for backwards compatibility
repositories {
grailsCentral()
// uncomment the below to enable remote dependency resolution
// from public Maven repositories
mavenLocal()
mavenCentral()
}
dependencies {
compile 'com.twilio.sdk:twilio:7.8.0'
}
in plugin.xml
<dependencies>
<compile>
<dependency group='com.twilio.sdk' name='twilio' version='7.8.0' />
</compile>
</dependencies>
In main project BuildConfig.gorrcy
plugins{
compile ":twilio-ext:1.0"
}
running grails dependency-report I see
org.grails.plugins:twilio-ext:1.0
But it has no sub dependencies listed.

Grails - Database Reverse Engineer plugin not found

Environment
Grails 2.4.4
PostgreSQL 9.4
JDK 1.7
I've been trying to get this plugin to work. (I'm still a grails newbie, not to mention programming.)
I tried to do everything I can find out there but still get this untimate error message.
Loading Grails 2.4.4
|Configuring classpath
|Running pre-compiled script
|Script 'DbReverseEngineer' not found, did you mean:
1) SetVersion
2) GenerateRestfulController
3) GenerateViews
4) GenerateController
5) DbmGenerateChangelog
Please make a selection or enter Q to quit:
This is what I have right not in my config files. (I've tried to change a lot of them, such as Hibernate, plugin version etc, but always end up with that message.)
grails.project.dependency.resolver = "maven"
grails.project.dependency.resolution = {
// inherit Grails' default dependencies
inherits("global") {
// specify dependency exclusions here; for example, uncomment this to disable ehcache:
// excludes 'ehcache'
}
log "warn" // log level of Ivy resolver, either 'error', 'warn', 'info', 'debug' or 'verbose'
checksums true // Whether to verify checksums on resolve
legacyResolve false // whether to do a secondary resolve on plugin installation, not advised and here for backwards compatibility
repositories {
inherits true // Whether to inherit repository definitions from plugins
grailsPlugins()
grailsHome()
mavenLocal()
grailsCentral()
mavenCentral()
// uncomment these (or add new ones) to enable remote dependency resolution from public Maven repositories
mavenRepo "http://repository.codehaus.org"
//mavenRepo "http://download.java.net/maven/2/"
mavenRepo "http://repo.grails.org/grails/repo/"
mavenRepo "http://repository.jboss.com/maven2/"
}
dependencies {
// specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes e.g.
// runtime 'mysql:mysql-connector-java:5.1.29'
test "org.grails:grails-datastore-test-support:1.0.2-grails-2.4"
runtime "org.postgresql:postgresql:9.4-1205-jdbc41"
compile "org.grails.plugins:db-reverse-engineer:4.0.0"
}
plugins {
// plugins for the build system only
build ":tomcat:7.0.55"
// plugins for the compile step
compile ":scaffolding:2.1.2"
compile ':cache:1.1.8'
compile ":asset-pipeline:1.9.9"
compile ":postgresql-extensions:4.6.1"
compile ":jquery:1.11.1"
compile ":joda-time:1.5"
compile "org.grails.plugins:db-reverse-engineer:4.0.0"
// plugins needed at runtime but not for compilation
runtime ":hibernate4:4.3.6.1"// or ":hibernate:3.6.10.18"
runtime ":database-migration:1.4.0"
runtime ":jquery:1.11.1"
runtime ":db-reverse-engineer:4.0.0"
}
Thanks in advance.
EDIT 1
So, I changed DbReverseEngineer.groovy under Script folder like this.
mergedConfig.driverClassName = dsConfig.driverClassName ?: 'org.postgresql.Driver'
mergedConfig.password = dsConfig.password ?: ''
mergedConfig.username = dsConfig.username ?: 'postgres'
mergedConfig.url = dsConfig.url ?: 'jdbc:postgresql://localhost:5432/myApp'
And I changed plugin and dependency per Burt's and Emmanuel's advice. And when I run grails db-reverse-engineer command I get this error.
Compilation error: startup failed:
Compile error during compilation with javac.
/home/Documents/Grails_Workspace/myApp/target/work/plugins/cache-1.1.8/src/java/grails/plugin/cache/web/GenericResponseWrapper.java:203: error: method does not override or implement a method from a supertype
#Override
^
/home/Documents/Grails_Workspace/myApp/target/work/plugins/cache-1.1.8/src/java/grails/plugin/cache/web/filter/PageFragmentCachingFilter.java:389: error: cannot find symbol
contentType = response.getContentType();
^
symbol: method getContentType()
location: variable response of type HttpServletResponse
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
What are those errors mean and how can I get rid of it?
Thanks.
You've got the same plugin set up twice:
compile 'org.grails.plugins:db-reverse-engineer:4.0.0'
runtime ':db-reverse-engineer:4.0.0'
According to the plugin's page it should be configured as a compile time dependency:
compile "org.grails.plugins:db-reverse-engineer:4.0.0"
The plugin does have the command that's claimed to be missing. You can see it here. So you may need to refresh the dependencies by running:
grails refresh-dependencies

Geb driver issue after upgrading to Grails 2.4.5

Upon upgrading my Grails version from 2.3.6 to 2.4.5, I am now getting an exception when running Geb Spock tests.
failed to create driver from callback 'script14328041759692122350870$_run_closure1#5fcdf5ea'
geb.driver.DriverCreationException: failed to create driver from callback 'script14328041759692122350870$_run_closure1#5fcdf5ea'
at geb.driver.CallbackDriverFactory.getDriver(CallbackDriverFactory.groovy:35)
at geb.driver.CachingDriverFactory.getDriver_closure3(CachingDriverFactory.groovy:85)
at geb.driver.CachingDriverFactory$SimpleCache.get(CachingDriverFactory.groovy:32)
at geb.driver.CachingDriverFactory.getDriver(CachingDriverFactory.groovy:84)
at geb.Configuration.createDriver(Configuration.groovy:361)
at geb.Configuration.getDriver(Configuration.groovy:350)
at geb.Browser.getDriver(Browser.groovy:105)
at geb.Browser.clearCookies(Browser.groovy:496)
at geb.spock.GebSpec.methodMissing(GebSpec.groovy:54)
at AuthorizationAdminSpec.setupSpec(AuthorizationAdminSpec.groovy:21)
Caused by: java.lang.NoClassDefFoundError: org/apache/http/conn/SchemePortResolver
at org.openqa.selenium.remote.internal.ApacheHttpClient$Factory.getDefaultHttpClientFactory(ApacheHttpClient.java:234)
at org.openqa.selenium.remote.internal.ApacheHttpClient$Factory.<init>(ApacheHttpClient.java:211)
at org.openqa.selenium.remote.HttpCommandExecutor.getDefaultClientFactory(HttpCommandExecutor.java:88)
at org.openqa.selenium.remote.HttpCommandExecutor.<init>(HttpCommandExecutor.java:62)
at org.openqa.selenium.remote.HttpCommandExecutor.<init>(HttpCommandExecutor.java:57)
at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnection.java:93)
at org.openqa.selenium.firefox.FirefoxDriver.startClient(FirefoxDriver.java:246)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:114)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:191)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:186)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:182)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:99)
at script14328041759692122350870.run_closure1(script14328041759692122350870.groovy:12)
at geb.driver.CallbackDriverFactory.getDriver(CallbackDriverFactory.groovy:29)
... 9 more
Caused by: java.lang.ClassNotFoundException: org.apache.http.conn.SchemePortResolver
at org.codehaus.groovy.tools.RootLoader.findClass(RootLoader.java:175)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at org.codehaus.groovy.tools.RootLoader.loadClass(RootLoader.java:147)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 23 more
I remember encountering this exception before during my initial Geb setup in Grails 2.3.6, in which the BuildConfig and GebConfig files were not properly configured. However, upon re-checking the necessary plugins and dependencies required of Geb, I did not notice anything different for Grails 2.4.5 Also, I switched my dependency resolution from Ivy to Maven, so I double checked my maven resources to make sure the driver was loaded.
Some more info...
BuildConfig.groovy
grails.project.dependency.resolver = "maven"
grails.project.dependency.resolution = {
// inherit Grails' default dependencies
inherits("global") {
// specify dependency exclusions here; for example, uncomment this to disable ehcache:
// excludes 'ehcache'
}
log "warn" // log level of Ivy resolver, either 'error', 'warn', 'info', 'debug' or 'verbose'
checksums true // Whether to verify checksums on resolve
repositories {
grailsPlugins()
grailsHome()
grailsCentral()
mavenLocal()
mavenCentral()
mavenRepo "http://repository.jboss.com/maven2/"
mavenRepo "http://google-api-client-libraries.appspot.com/mavenrepo"
mavenRepo "http://mvnrepository.com/artifact/"
mavenRepo "http://repo.jenkins-ci.org/repo"
mavenRepo "http://repo.grails.org/grails/repo"
}
dependencies {
....
compile "org.springframework:spring-test:4.0.9.RELEASE"
test "org.gebish:geb-spock:0.10.0"
test "org.seleniumhq.selenium:selenium-support:2.45.0"
test "org.seleniumhq.selenium:selenium-firefox-driver:2.45.0"
}
plugins {
....
test ":geb:0.10.0"
}
GebConfig.groovy
import org.openqa.selenium.firefox.FirefoxDriver
import org.openqa.selenium.firefox.FirefoxProfile
reportsDir = "target/geb-reports"
baseUrl = "http://localhost:8090/adverity/"
driver = {
//set the firefox locale to 'en-us' since the tests expect english
//see http://stackoverflow.com/questions/9822717 for more details
FirefoxProfile profile = new FirefoxProfile()
profile.setPreference("intl.accept_languages", "en-us")
def driverInstance = new FirefoxDriver(profile)
driverInstance.manage().window().maximize()
driverInstance
}
baseNavigatorWaiting = true
atCheckWaiting = true
autoClearCookies = false
quitCachedDriverOnShutdown = false
Any simple spec that I run will throw the same error.I tried doing the same with Chrome to test if this is a driver issue and got the same results. Both firefox and Chrome drivers work with Grails 2.3.6, but not 2.4.5.
OS: Fedora 20
Browser: Firefox 38
Places I have looked already...
Book of Geb
GebGrails git hub example
StackOverflow
test 'org.apache.httpcomponents:httpclient:4.3.2'
Added to the dependencies section in BuildConfig.groovy
I was not aware of this dependency as I did not see it in any documentation, but a deeper look inside the stack trace told me otherwise.

Error loading BuildConfig (Grails 2.1.0)

I've been racking my brain for hours trying to figure this out. Whenever I try to perform any kind of action to my newly created Grails project (with a fresh Grails install), I get this error message:
Error There was an error loading the BuildConfig: ivy pattern must be absolute:
${HOME}/.m2/alpha/repository/[organisation]/[module]/[revision]/[module]-[revision](-
[classifier]).pom (Use --stacktrace to see the full trace)
I can deduce that there's a problem with my installation, but its a fresh install as I said so I'm not sure what could have caused this problem already.
I'm running Win7. Any help would be much appreciated.
EDIT:
grails.servlet.version = "2.5" // Change depending on target container compliance (2.5 or 3.0)
grails.project.class.dir = "target/classes"
grails.project.test.class.dir = "target/test-classes"
grails.project.test.reports.dir = "target/test-reports"
grails.project.target.level = 1.6
grails.project.source.level = 1.6
//grails.project.war.file = "target/${appName}-${appVersion}.war"
grails.project.dependency.resolution = {
// inherit Grails' default dependencies
inherits("global") {
// specify dependency exclusions here; for example, uncomment this to disable ehcache:
// excludes 'ehcache'
}
log "error" // log level of Ivy resolver, either 'error', 'warn', 'info', 'debug' or 'verbose'
checksums true // Whether to verify checksums on resolve
repositories {
inherits true // Whether to inherit repository definitions from plugins
grailsPlugins()
grailsHome()
grailsCentral()
mavenLocal()
mavenCentral()
// uncomment these (or add new ones) to enable remote dependency resolution from public Maven repositories
//mavenRepo "http://snapshots.repository.codehaus.org"
//mavenRepo "http://repository.codehaus.org"
//mavenRepo "http://download.java.net/maven/2/"
//mavenRepo "http://repository.jboss.com/maven2/"
}
dependencies {
// specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes eg.
// runtime 'mysql:mysql-connector-java:5.1.20'
}
plugins {
runtime ":hibernate:$grailsVersion"
runtime ":jquery:1.7.1"
runtime ":resources:1.1.6"
// Uncomment these (or add new ones) to enable additional resources capabilities
//runtime ":zipped-resources:1.0"
//runtime ":cached-resources:1.0"
//runtime ":yui-minify-resources:0.1.4"
build ":tomcat:$grailsVersion"
runtime ":database-migration:1.1"
compile ':cache:1.0.0.RC1'
}
}
Problem solved for now.
It was a Maven problem, which I didn't consider initially. For some reason, the localRepository tag in /Users/USERNAME/.mp2/settings.xml couldn't resolve the path to HOME, so replacing ${HOME} with the C:/Users/USERNAME did the trick. Strange but it works for now. If anyone has a better solution let me know!
My solution was a little different:
(1) Create a variable that points to your .m2 repository location:
def localMavenRepo = "file://" + new File(System.getProperty('user.home'), '.m2/repository').absolutePath
(2) Create a repository entry:
repositories {
...
mavenRepo name: 'Local', root: localMavenRepo
}
I also got the same problem.
I think it's not Maven problem, it's Apache Ivy's problem.
I think Apache Maven 2/3 works well, it's just that Apache Ivy didn't pick the correct Maven folder. The Apache Ivy didn't expand the ${user.home} as it should.
I solved this by setting my M2_REPO environment variable to an absolute path. On linux:
export M2_HOME=/home/chris/.m2/repository/
On windows you can set environment variables in the System Properties dialog (shortcut to System Properties is WindowsKey+PauseKey). Go to System Properties -> Advanced -> Environment Variables.

Grails very slow to resolve certain dependencies

My 'resolving dependencies' is up around 30 seconds for a lightening fast computer and network.
I've looked at this question Grails: Very slow deploy time. 'Resolving Dependencies...' takes 10+ seconds, and I don't appear to have any 'snapshot' dependencies. I've cleared out my ivy cache and forced a redownload of everything, no help. Enabling logging, these few lines are the offenders. Nothing is being downloaded, but it appears that attempts are made to look for something remotely every time, given my network traffic. Note these are the ONLY entries that have the [some number] version before them, and then a different one after - that can't be coincidence - what does it mean? How can I stop grails from trying to find these slightly different versions?
.... these lines take 20+seconds, everything else is in the milliseconds ...
found commons-logging#commons-logging;1.1.1 in grailsPlugins
[1.1.1] commons-logging#commons-logging;[1.1, 2.0)
found org.apache.httpcomponents#httpclient;4.1.2 in default
[4.1.2] org.apache.httpcomponents#httpclient;[4.1, 5.0)
found org.apache.httpcomponents#httpcore;4.1.2 in default
found org.codehaus.jackson#jackson-core-asl;1.9.1 in default
[1.9.1] org.codehaus.jackson#jackson-core-asl;[1.4,)
found javax.mail#mail;1.4.4 in default
[1.4.4] javax.mail#mail;[1.4,)
I am using two plugins that must be the offenders, and have listed their dependencies.groovy below. I've tried commenting out any explicit remote URLs. 
SimpleDB:
grails.project.class.dir = "target/classes"
grails.project.test.class.dir = "target/test-classes"
grails.project.test.reports.dir = "target/test-reports"
grails.project.dependency.resolution = {
inherits "global"
log "warn"
String datastoreVersion = "1.0.0.M9"
repositories {
grailsPlugins()
grailsHome()
grailsCentral()
mavenRepo "http://repo.grails.org/grails/core" //tried commenting this out, no help
mavenLocal()
mavenCentral()
mavenRepo 'http://repository.codehaus.org' //tried commenting this out, no help
}
dependencies {
def excludes = {
transitive = false
}
compile("org.grails:grails-datastore-gorm-simpledb:$datastoreVersion",
"org.grails:grails-datastore-gorm-plugin-support:$datastoreVersion",
"org.grails:grails-datastore-gorm:$datastoreVersion",
"org.grails:grails-datastore-core:$datastoreVersion",
"org.grails:grails-datastore-simpledb:$datastoreVersion",
"org.grails:grails-datastore-web:$datastoreVersion") {
transitive = false
}
runtime("stax:stax:1.2.0", excludes)
runtime('com.amazonaws:aws-java-sdk:1.2.0')
test("org.grails:grails-datastore-gorm-test:$datastoreVersion",
"org.grails:grails-datastore-simple:$datastoreVersion") {
transitive = false
}
}
plugins {
build ":release:1.0.0.RC3", {
exported = false
}
}
}
Ajax uploader:
grails.project.class.dir = "target/classes"
grails.project.test.class.dir = "target/test-classes"
grails.project.test.reports.dir = "target/test-reports"
grails.release.scm.enabled=false
grails.project.dependency.resolution = {
// inherit Grails' default dependencies
inherits("global") {
// uncomment to disable ehcache
// excludes 'ehcache'
}
log "warn" // log level of Ivy resolver, either 'error', 'warn', 'info', 'debug' or 'verbose'
repositories {
grailsPlugins()
grailsHome()
grailsCentral()
// uncomment the below to enable remote dependency resolution
// from public Maven repositories
mavenLocal()
mavenCentral()
//mavenRepo "http://snapshots.repository.codehaus.org"
//mavenRepo "http://repository.codehaus.org"
//mavenRepo "http://download.java.net/maven/2/"
//mavenRepo "http://repository.jboss.com/maven2/"
}
dependencies {
// specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes eg.
test 'org.gmock:gmock:0.8.1'
}
plugins {
//build ':release:1.0.0.RC3'
}
}
and I've found the culprit. If a plugin you use relies on a library in maven that has 'open ended' dependencies, grails will go and look each time if there are newer versions to download in the range. I have no idea why anyone would specify it like this. It seems it would lead to unreliable behaviour. For me, the culprit is Amazon's java aws library, naturally required by the simpledb plugin that talks to Amazon's cloud.
http://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk/1.2.10
note how some of its dependencies are like this
org.apache.httpcomponents httpclient [4.1, 5.0)
it appears that every time, grails is looking for a newer version (and downloading if it exists, I just noticed 4.2-alpha1 of httpclient come down when I ran this time).
By removing that dependency from the plugin and manually adding the required libraries to my .lib folder, I reduced my startup time from >30sec to <1sec
If you run grails --offline run-app, then dependency resolution gets (at least partially) disabled and startup time is much quicker.
Of course you have to be sure that you don't need any new dependencies to be downloaded - I've had spent time scrounging around for a solution for a problem which turned out to be because I was running grails offline. I learnt quickly though :)
I agree that Grails startup times are horribly slow, luckily the rest of the framework improves productivity - mostly to the point where you're more productive than plain old java :-)

Resources