如何让Gradle为eclipse生成java.library.path

时间:2016-03-29 13:16:38

标签: eclipse gradle gradle-eclipse nativelibrary sqlite4java

我有一个使用sqlite4java的gradle项目,我在eclipse中工作。

我的问题是,当我得到gradle生成eclipse项目文件时,项目类路径在类路径上包含sqlite4java的本机库而不是java.library.path,这导致eclipse无法构建我的项目因为它抱怨本机库不是很好的zip文件。此外,当我从类路径手动删除本机库时,然后当我运行测试或应用程序时,它们会因为无法加载sqlite4java本机库而出错。

如何让edle为sqlite4java设置java.library.path以便我的代码符合并在eclipse中运行?

1 个答案:

答案 0 :(得分:0)

在此主题上找到解决方案:https://discuss.gradle.org/t/is-it-possible-to-set-eclipses-java-library-path-from-build-gradle/6511/6

将以下代码添加到我的gradle构建文件并重新运行gradle eclipse解决了这个问题:

的build.gradle:

def getSqlLite4JavaNativeLibraryPath() {
    return configurations.runtime.resolve().findResult { entry ->
        String absolutPath = entry.getAbsolutePath();

        if(absolutPath.contains("sqlite4java-win32-x64")){
            // return the directory that contains the native library
            return entry.getParent()
        }
    }
} 

eclipse.classpath.file.whenMerged { classpath ->
    //remove the all native libraries as direct dependencies
    classpath.entries.removeAll { 
        entry -> entry.kind == 'lib' && (entry.path.endsWith('.dll') 
            || entry.path.endsWith('.so')
            || entry.path.endsWith('.dylib'))
    }
    //but add them as native libraries
    def sqlite4java = classpath.entries.findResult { entry ->
        if (entry.kind == 'lib' && entry.path.contains('sqlite4java')) {
            return entry
        }
    }
    sqlite4java.setNativeLibraryLocation(getSqlLite4JavaNativeLibraryPath())
}

生成的.classpath文件现在包含本机库目录的属性。

的.classpath:

<classpathentry sourcepath="C:/Users/user1/.gradle/caches/modules-2/files-2.1/com.almworks.sqlite4java/sqlite4java/1.0.392/2efe18f7bea6fa9536802dd4ea54d948117216c6/sqlite4java-1.0.392-sources.jar" kind="lib" path="C:/Users/user1/.gradle/caches/modules-2/files-2.1/com.almworks.sqlite4java/sqlite4java/1.0.392/d6234e08ff4e1607ff5321da2579571f05ff778d/sqlite4java-1.0.392.jar" exported="true">
    <attributes>
        <attribute name="org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" value="C:\Users\user1\.gradle\caches\modules-2\files-2.1\com.almworks.sqlite4java\sqlite4java-win32-x64\1.0.392\d20dc00abecc7e0bde38c68eee68f2e70c26df95"/>
    </attributes>
</classpathentry>

我还遇到了gradle单元测试无法加载sqlite4java本机库的问题,所以我还需要将以下内容添加到我的gradle测试目标中,以便从gradle运行的测试能够加载sqlite4java本土图书馆。

test {
    systemProperty "sqlite4java.library.path", getSqlLite4JavaNativeLibraryPath()
}