2016-03-29 43 views
0

我有一個使用sqlite4java的gradle項目,我在eclipse中工作。如何讓Gradle爲eclipse生成java.library.path

我的問題是,當我得到gradle來生成eclipse項目文件時,項目classpath在類路徑中包含sqlite4java的本地庫而不是java.library.path,導致eclipse無法構建我的項目,因爲它抱怨本地庫不是格式正確的zip文件。另外,當我手動從類路徑中刪除本地庫時,那麼當我運行測試或應用程序時,它們會出錯,因爲它們無法加載sqlite4java本機庫。

如何獲取gradle在eclipse中爲sqlite4java設置java.library.path,以便我的代碼符合並在eclipse中運行?

回答

0

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

添加以下代碼到我的gradle這個build文件並重新運行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() 
} 
相關問題