Grails global config/dependency resolution - grails

I'm behind a firewall that doesn't allow me to download from grailsCentral or mavenCentral. But there is one specific public maven repository that IT lets me download from (say 'repo.maven.apache.org'). Is there a way to configure grails with a global configuration? Or more specifically to use a repository globally?
Excerpt from BuildConfig.groovy
grails.project.dependency.resolution = {
inherits 'global'
log 'warn'
repositories {
grailsCentral()
mavenLocal()
mavenCentral()
mavenRepo 'http://repo.grails.org/grails/libs-releases'
// Normally I would add this line to every project
//mavenRepo 'http://repo.maven.apache.org/maven2/'
}
...
Is there a way that I can create a file like ~/.grails/BuildConfig.groovy that has content like the following?
grails.global.dependency.resolution = {
repositories {
mavenRepo 'http://repo.maven.apache.org/maven2/'
}
}
Then this repo would be used for every project.:)

I was able to do this using the method posted by #ak-tech (Thank you!)
in ~/.grails/settings.groovy
grails.project.dependency.resolution = {
repositories {
mavenRepo 'http://repo.maven.apache.org/maven2'
}
}

I believe that if you change BuildConfig.groovy in {your-grails-installation-dir}\src\grails\grails-app\conf\BuildConfig.groovy the way you want, then every time you run
grails> create-app myApp
you should get the BuildConfig.groovy with the changes you want.

Related

Add custom jar grails 2.4.3

I'm adding a downloaded jar to my lib folder, but when I try to use it doesn't work.
Here is the code:
// make sure the ClassLoader has the MonetDB JDBC driver loaded
Class cls = Class.forName("nl.cwi.monetdb.jdbc.MonetDriver");
// request a Connection to a MonetDB server running on 'localhost'
Connection con = DriverManager.getConnection("jdbc:monetdb://localhost/testDB", "monetdb", "monetdb");
Statement st = con.createStatement();
There is no code problems because I created a java app with same code and it works, the problem is grails is not taking the jar into the class path.
So finaly here is my buildConfig.groovy
grails.project.dependency.resolver = "maven" // or ivy
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 {
grailsCentral()
mavenLocal()
mavenCentral()
// uncomment the below to enable remote dependency resolution
// from public Maven repositories
//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.27'
}
plugins {
build ":tomcat:8.0.22"
build(":release:3.0.1",
":rest-client-builder:1.0.3") {
export = false
}
}
Take a look at this document:
http://grails.github.io/grails-doc/latest/guide/conf.html#dataSource
Define the monetdb as a dependency using BuildConfig.groovy
by adding this 'monetdb:monetdb-jdbc:2.8'
Then update the entries in the Datasource.groovy file.
Now from your controller yuo have to inject the datasource.
SampleController{
def dataSource
def index(){
def sql = new Sql(dataSource)
sql.executeUpdate('select * from testdb.something')
}
}

Grails can't load maven-publisher

I am trying to add maven-publisher to a Grails (2.3.6) plugin like so:
dependencies {
compile 'org.mongodb.morphia:morphia:0.107'
compile ":maven-publisher:0.8.1"
}
When I run grails compile I get:
| Error There was an error loading the BuildConfig: Bad artifact coordinates
:maven-publisher:0.8.1, expected format is <groupId>:<artifactId>[:<extension>[
:<classifier>]]:<version> (Use --stacktrace to see the full trace)
What's going on here?
Do not use the maven-publisher plugin. It's old and deprecated. Use the release plugin - it should already be in your plugin's BuildConfig.groovy. If not, here's how it should look (after removing unnecessary cruft):
grails.project.work.dir = 'target'
grails.project.dependency.resolution = {
inherits 'global'
log 'warn'
repositories {
grailsCentral()
mavenLocal()
mavenCentral()
}
dependencies {
compile 'org.mongodb.morphia:morphia:0.107'
}
plugins {
build ':release:3.0.1', ':rest-client-builder:1.0.3', {
export = false
}
}
}
As #dmahapatro said in his comment, jar dependencies go in the dependencies block, and plugin dependencies go in the plugins block.
Note also that you should keep the export = false setting so the plugin is available locally for your use, but doesn't leak into the containing application as an unnecessary transitive dependency.

IntelliJ mavenLocal Warning in Grails BuildConfig

This warning started showing in IntelliJ :
'mavenLocal' in 'org.codehaus.groovy.grails.resolve.config.RepositoriesConfigurer' cannot be applied to '()'
Here's the relevant snippet from BuildConfig.groovy.
repositories {
inherits true // Whether to inherit repository definitions from plugins
grailsPlugins()
grailsHome()
grailsCentral()
mavenLocal()
mavenRepo "https://mycompany.artifactoryonline.com/mycompany/repo"
mavenCentral()
}
Environment:
OSX 10.6.8
Grails 2.0.3
IntelliJ 11.1.2
The mavenLocal method in RepositoriesConfigurer is defined like this:
void mavenLocal(String repoPath) {
...
}
So yes, mavenLocal expects a path, but is happy with getting null, which means to use the default repository path (.m2/repository under users home directory).
As I understand it, mavenLocal() resolves to calling mavenLocal(null) in this case.
mavenLocal should probably be changed to
void mavenLocal(String repoPath = null) {
...
}
to make it more obvious that the repoPath is optional, and get rid of the warning in IntelliJ

Grails 2.0 depedencies NoClassDefFound issue

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

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