In Grails, how do I specify jars needed by an ivy resolver?

*爱你&永不变心* 提交于 2019-12-02 03:56:31

I had the same problem and got this answer from the Ian Roberts on the Grails user list which works for me:

def myCL = new URLClassLoader([new File(
            "${basedir}/lib/the.jar"
        ).toURI().toURL()] as URL[],
        org.apache.ivy.plugins.repository.AbstractRepository.classLoader)
resolver myCL.loadClass('com.whatever.MyResolver').newInstance()

Passing the class loader that loaded Ivy as the parent is important so that it can resolve Ivy classes.

If you need to load multiple jars (because the class you are loading depends on them), then put all the files in the list like so:

def myCL = new URLClassLoader([
        "${basedir}/lib/jar1",
        "${basedir}/lib/jar2", // etc.
    ].collect { new File(it).toURI().toURL() } as URL[],
    org.apache.ivy.plugins.repository.AbstractRepository.classLoader)
resolver myCL.loadClass('com.whatever.MyResolver').newInstance()

Another option that seems to work is to use @Grab. Something like:

@Grab(group="com.jcraft",module="jsch",version="0.1.42")
import org.apache.ivy.plugins.resolver.SshResolver

def sshResolver = new SshResolver()
['libraries', 'builds'].each {
    sshResolver.addArtifactPattern("/home/ivy/[organisation]/[revision]/[artifact].[ext]")
    sshResolver.addIvyPattern("/home/ivy/[organisation]/[revision]/[artifact].[ext]")
}
sshResolver.name = "ssh"
sshResolver.settings = ivySettings

resolver sshResolver

If your jar isn't in a public repository, it may be a little trickier.

Grails uses ivy to implement it's dependency management. All you should need to do is declare the extra libraries you need. Have you tried something like this:

dependencies {
    ..
    compile 'com.jcraft:jsch:0.1.42'
    ..
}   

Apparently

grails -cp ./lib/jsch.jar 

was the answer, not the -classpath or --classpath that I had initially tried.

If anyone has a better answer I will certainly accept it over mine.

I tried placing the jar in grails/lib but it gets loaded after the resolvers are processed.

I tried this in PreInit.groovy but no luck there either.

grails.compiler.dependencies = { fileset(file:'${basedir}/lib/jsch.jar') } 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!