将Gradle与本地依赖关系一起使用 [英] Using Gradle with native dependencies

查看:783
本文介绍了将Gradle与本地依赖关系一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Gradle项目中使用 Sigar 。 Sigar发行版默认提供两种类型的文件:
$ b


  • 包含类的JAR

  • 一些本地文件(.so,dylib,.dll)



我的目的是重新打包这些文件,以便我可以将它们用作依赖关系部署和从个人Maven仓库中按需下载。



我的第一个尝试是将依赖关系定义为文件,以便检查我的应用程序在重新打包之前是否按预期工作。以下是我用于第一次测试的Gradle代码:

$ p code $ dependencies {
compile files($ { (dir:$ {rootDir} / lib / sigar /,不包括:* .jar)
}

然后,我将Sigar本机文件重新打包成一个JAR,并重命名另一个匹配Maven工件的规则,因为我想将它们部署到Maven存储库中。以下是我得到的:

$ ul

  • sigar-1.6.4.jar(包含.class文件)
  • sigar-1.6.4-native.jar(在根目录中包含.dylib,.so和.dll文件)



  • 下一步是将这些文件部署到我的定制存储库中。然后,我更新了我的 build.gradle ,如下所示:

     依赖关系{
    compile'sigar:sigar:1.6.4'
    runtime'sigar:sigar:1.6.4:native'
    }

    不幸的是,当我执行 gradle clean build 时,会提取新的依赖关系,但不能再找到本地库从现在开始,我得到以下异常:

      postRegister方法抛出的错误:rethrowing< java.lang.UnsatisfiedLinkError:org .hyperic.sigar.Sigar.getCpuInfoList()[Lorg / Hyperic公司/ SIGAR内/ cpuinfo;> 

    因此,我正在寻找一种解决方案来获取并链接原生文件到我的Java应用程序其他依赖关系。任何建议,评论,建议,帮助,解决方案等等都是可以接受的;)
    解决方案配置,将JAR文件解压到所需位置:

      project.ext.set('nativeLibsDir',$ buildDir / libs / )

    配置{
    nativeBundle
    }

    依赖关系{
    nativeBundle'sigar:sigar:1.6.4:native'


    任务extractNativeBundle(类型:Sync){
    from {
    configurations.nativeBundle.collect {zipTree(it)}
    }
    (project.nativeLibsDir)
    }

    dist.dependsOn extractNativeBundle

    然后,对于依赖本地库的任务,此位置必须放在 java.library.path 中:

      systemPropertyjava.library.path,project.nativeLibsDir 


    I am trying to use Sigar in a Gradle project. Sigar distribution is by default provided with 2 types of files:

    • a JAR that contains classes
    • some native files (.so, dylib, .dll)

    My purpose is to repackage these files so that I can use them as dependencies deployed and downloaded on-demand from a personal Maven repository.

    My first try was to define dependencies as files in order to check that my application is working as expected before to repackage. Below is the Gradle code I used for my first test that works:

    dependencies {
      compile files("${rootDir}/lib/sigar/sigar.jar")
      runtime fileTree(dir: "${rootDir}/lib/sigar/", exclude: "*.jar")
    }
    

    Then, I have repackaged Sigar native files into a JAR and renamed the other one to match rules for maven artifacts since I want to deploy them in a Maven repository. Below is what I get:

    • sigar-1.6.4.jar (contains .class files)
    • sigar-1.6.4-native.jar (contains .dylib, .so, and .dll files at the root)

    The next step was to deploy these files in my custom repository. Then, I have updated my build.gradle as follows:

    dependencies {
      compile 'sigar:sigar:1.6.4'
      runtime 'sigar:sigar:1.6.4:native'
    }
    

    Unfortunately, when I do a gradle clean build, new dependencies are fetched but native libraries can no longer be found at runtime since now I get the following exception:

    Error thrown in postRegister method: rethrowing <java.lang.UnsatisfiedLinkError: org.hyperic.sigar.Sigar.getCpuInfoList()[Lorg/hyperic/sigar/CpuInfo;>
    

    Consequently, I am looking for a solution to fetch and to link native files to my Java app like for other dependencies. Any advice, comment, suggestion, help, solution, etc. are welcome ;)

    解决方案

    A solution is to define a new gradle configuration that unzips JAR files at the desired location:

    project.ext.set('nativeLibsDir', "$buildDir/libs/natives")
    
    configurations {
        nativeBundle
    }
    
    dependencies {
        nativeBundle 'sigar:sigar:1.6.4:native'
    }
    
    task extractNativeBundle(type: Sync) {
        from {
            configurations.nativeBundle.collect { zipTree(it) }
        }
        into file(project.nativeLibsDir)
    }
    
    dist.dependsOn extractNativeBundle
    

    Then, this location must be put in java.library.path for tasks that depend on native libraries:

    systemProperty "java.library.path", project.nativeLibsDir
    

    这篇关于将Gradle与本地依赖关系一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆