How do I reference my own classes in a build file


(Paul Bennett) #1

I want to write a task that uses a class that I have defined e.g.

task myTask {
    def t
= new MyOwnClass()
    t.doSomething()
}

MyOwnClass is in a local jar, but I cannot find out how to get that on to the classpath of the build script. Is this possible, and how would I set up the classpath to make it happen? Or do I have to write a custom task to do this?

Thanks!


(Peter Niederwieser) #2
buildscript {
  dependencies {
    classpath files("path/to/jar")
  }
}

For more information, see the Gradle User Guide.


(Paul Bennett) #3

I seem to be missing something, as that doesn’t seem to work. Here is my build file:

buildscript {
 dependencies {
  def fastHome='../Faster/build/install/Faster'
  def fastLibs = new File("$fastHome/lib").list().findAll { it[0..2] != 'ant' }
  println fastLibs
  classpath files(fastLibs)
 }
}
  task buildApp {
 def modelPath="../${modelName}"
   def model = new com.metaficient.fast.core.FolderDomainModel(modelPath)
}

The class I am using is FolderDomainModel, and is in the Faster jar on the classpath, but I still get a class not found error:

gradle -Dserver=localhost -Dmodel=PMS buildApp
[castor-1.2.jar, commons-beanutils-1.8.3.jar, commons-codec-1.5.jar, commons-collections-3.2.1.jar, commons-digester-2.1.jar, commons-exec-1.1.jar, commons-io-2.4.jar, commons-logging.jar, datecalc-common-1.2.0.jar, datecalc-jdk-1.2.0.jar, datecalc-joda-1.2.0.jar, Dios-0.0.1-dev.2013.01.07.jar, dom4j-1.6.1.jar, Faster-0.0.1-dev.2013.01.07.jar, groovy-all-1.8.6.jar, itext-2.1.7.jar, jasperreports-4.1.1.jar, joda-time-2.0.jar, jollyday-0.4.4.jar, log4j-1.2.16.jar, MetaWeb-0.0.1-dev.2013.01.07.jar, MetaWebDeploymentBase-0.0.1-dev.2013.01.07.war, miglayout-4.0.jar, opencsv-2.3.jar, poi-3.9.jar, poi-ooxml-3.9.jar, poi-ooxml-schemas-3.9.jar, prevayler-2.3.jar, rsyntaxtextarea-2.0.2.jar, servlet-api-2.5.jar, stax-api-1.0.1.jar, swingx-core-1.6.2.jar, vaadin-6.8.6.jar, vaadin-chameleon-theme-1.1.0.jar, xml-apis-1.0.b2.jar, xmlbeans-2.3.0.jar, xpp3_min-1.1.4c.jar, xstream-1.3.1.jar]
  FAILURE: Build failed with an exception.
  * Where:
Build file '/Users/paul/ws/DeploymentDef/build.gradle' line: 14
  * What went wrong:
Could not compile build file '/Users/paul/ws/DeploymentDef/build.gradle'.
> startup failed:
  build file '/Users/paul/ws/DeploymentDef/build.gradle': 21: unable to resolve class com.metaficient.fast.core.FolderDomainModel
    @ line 21, column 14.
      def model = new com.metaficient.fast.core.FolderDomainModel(modelPath)
                  ^
      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.523 secs

I print out the calculated classpath, and the jar is there


(Paul Bennett) #4

OK, Peter I found it. I’m leaving out the leading relative pathname on the jar name. Duh! I tried adding the lib dir as a repository, but it still wouldn’t work - the documentation on ScriptHandler suggests it would, I’m wondering why it didn’t

Thanks!