How can I use the classes which are in my maven dependencies?


(fabien perie) #1

Hello,

I am trying to migrate gradle from version 1.0-milestone-1 to 1.0-milestone-7 however i am exeperiencing the following problem using classes in my maven dependencies…

The following script reproduce the problem :

apply plugin: 'java'
apply plugin: 'maven'
  // Tache par defaut
// qui sera execute sans le parametre -q <nomTache>
defaultTasks 'main'
  repositories {
    // mavenCentral()
      maven {
 url "http://repository.XXXXX.fr:8081/nexus/content/groups/public/"
    }
}
  dependencies {
    compile(group: 'commons-lang', name: 'commons-lang', version: '2.6')
    compile(group: 'commons-io', name: 'commons-io', version: '2.0.1')
}
  // import org.apache.commons.lang.SystemUtils;
// import org.apache.commons.lang.StringUtils;
  task doTestResolve << {
        configurations.compile.files.each { println "resolved dependency: $it" }
}
  task doTestRetrieve(type: Copy) {
        from configurations.compile
        into "myDependencies"
        eachFile {
            println "retrieving dependency: $it.name"
        }
}
   task main << {
    display("================================================================")
    display("hello world....")
    display("================================================================")
      if (org.apache.commons.lang.SystemUtils.IS_OS_WINDOWS == false)
    {
 display("OS linux...")
    }
    else
    {
 display("OS windows...")
    }
}
  private void display(String message)
{
    ant.echo(message: message)
}

When running the script i have the following error :

[root@myServer /]# gradle -b test.gradle
:main
[ant:echo] ================================================================
[ant:echo] hello world....
[ant:echo] ================================================================
  FAILURE: Build failed with an exception.
  * Where:
Build file '/test.gradle' line: 41
  * What went wrong:
Execution failed for task ':main'.
Cause: Could not find property 'org' on task ':main'.
  * Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
  BUILD FAILED
  Total time: 5.954 secs

When running the script with the « doTestResolve » task, I can see that the maven dependencies are well downloaded :

[root@myServer /]# gradle -b test.gradle -q doTestResolve
resolved dependency: /root/.gradle/caches/artifacts-7/artifacts/4a100ce6d3ddaaebf915c9163900b580/commons-lang/commons-lang/2.6/jar/commons-lang-2.6.jar
resolved dependency: /root/.gradle/caches/artifacts-7/artifacts/4a100ce6d3ddaaebf915c9163900b580/commons-io/commons-io/2.0.1/jar/commons-io-2.0.1.jar
[root@myServer /]#

If I uncomment the line :

import org.apache.commons.lang.SystemUtils;

the following error occurs :

[root@myServer /]# gradle -b test.gradle
  FAILURE: Build failed with an exception.
  * Where:
Build file '/test.gradle' line: 21
  * What went wrong:
Could not compile build file '/test.gradle'.
Cause: startup failed:
build file '/test.gradle': 21: unable to resolve class org.apache.commons.lang.SystemUtils
 @ line 21, column 1.
   import org.apache.commons.lang.SystemUtils;
   ^
  1 error
    * Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
  BUILD FAILED
  Total time: 3.823 secs

Any idea ? Is it a problem with the classloader ? I would like to know how to resolve this problem.

Thanks a lot for you help. F.


(Benjamin Muschko) #2

You will have to specify the dependencies you want to use in your build script within the buildscript closure. Apply your dependencies to the configuration classpath. Check out this example: http://stackoverflow.com/questions/7716642/executing-sql-in-a-gradle-task/7719782#7719782.


(fabien perie) #3

Thanks a lot…

The solution :

buildscript
 {
 repositories {
     // mavenCentral()
       maven {
  url "http://repository.XXXXX.fr:8081/nexus/content/groups/public/"
     }
 }
   dependencies {
     classpath(group: 'commons-lang', name: 'commons-lang', version: '2.6')
     classpath(group: 'commons-io', name: 'commons-io', version: '2.0.1')
 }
}