我有一个gradle项目,它使用sqlite4java,我在eclipse中工作。
我的问题是,当我使用gradle生成eclipse项目文件时,项目类路径在类路径上而不是java.library.path上包含sqlite4java的本地库,这导致eclipse无法构建我的项目,因为它抱怨本地库不是格式良好的zip文件。此外,当我手动从类路径中删除本机库时,当我运行测试或应用程序时,它们会出错,因为它们无法加载sqlite4java本机库。
我如何让gradle在eclipse中为sqlite4java设置java.library.path,以便我的代码能够在eclipse中运行?
在此线程上找到了解决方案: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:Usersuser1.gradlecachesmodules-2files-2.1com.almworks.sqlite4javasqlite4java-win32-x641.0.392d20dc00abecc7e0bde38c68eee68f2e70c26df95"/>
</attributes>
</classpathentry>
我还遇到了gradle单元测试无法加载sqlite4java本机库的问题,因此我还需要将以下内容添加到我的gradle测试目标中,以便从gradle运行的测试能够加载sqlite4 java本机库。
test {
systemProperty "sqlite4java.library.path", getSqlLite4JavaNativeLibraryPath()
}