Is it possible to set Eclipse's java.library.path from build.gradle?


(Daniel Halperin) #1

I’m using Gradle with the java and eclipse plugins. My project includes sqlite4java, which uses a local JNI library to run.

Following this thread, I’ve been able to get “gradle test” to complete successfully. However, when I run “gradle eclipse”, the generated .classpath file does not include the Eclipse-specific annotation to include the sqlite4java JNI pointer. (The “attributes” tag and its contents in the below.)

<classpathentry exported="true" kind="lib" path="/Users/dhalperi/.gradle/caches/artifacts-14/filestore/com.almworks.sqlite4java/sqlite4java/0.282/jar/745a7e2f35fdbe6336922e0d492c979dbbfa74fb/sqlite4java-0.282.jar" sourcepath="/Users/dhalperi/.gradle/caches/artifacts-14/filestore/
                <attributes>
                       <attribute name="org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" value="myriad/lib/sqlite4java-282"/>
                </attributes>
        </classpathentry>

Is there a way to set up the build.gradle file so that the Eclipse plugin can propagate this information?

tasks.withType(Test) {
 systemProperty "java.library.path", file("lib/sqlite4java-282")
}

Thanks!


(Szczepan Faber) #2

I think the only way to include those custom attributes in eclipse classpath entries is to use eclipse’s withXml hook. Take a look in the dsl reference for the eclipse.classpath.xml.withXml hook.

Hope that helps!


(Daniel Halperin) #3

Interesting. The following works, but is pretty ugly. I’ve never used Groovy syntax before… Suggestions?

(inside eclipse.classpath)

file.withXml {
      provider ->
      provider.asNode().findAll { it.@path.contains("sqlite") }.each {
        it.children().each {
          it.appendNode('attribute', [name: 'org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY', value:'myriad/lib/sqlite4java-282'])
        }
      }
    }

Thanks!


(William Lichtenberger) #4

I was able to solve this problem by using the below code.

def jacob = classpath.entries.findResult { entry ->

if (entry.kind == ‘lib’ && entry.path.contains(‘jacob’)) {

return entry

}

}

File file = findFileInConfiguration(configurations.runtime, ‘jacob-1.17-M2-x64’)

jacob.setNativeLibraryLocation(file.getParent())

I feel this is better than modifying the resulting XML.


(Daniel Halperin) #5

Hi @w25r,

Where did you put this code? I can’t seem to figure out which subsection it goes in.


(William Lichtenberger) #6

I can’t seem to edit my post, let’s see if I can do mark up syntax in a comment.

eclipse.classpath.file.whenMerged { classpath ->

//remove the dlls as direct dependencies

classpath.entries.removeAll { entry -> entry.kind == ‘lib’ && entry.path.contains(‘dll’) }

//but add them as native libraries

def jacob = classpath.entries.findResult { entry ->

if (entry.kind == ‘lib’ && entry.path.contains(‘jacob’)) {

return entry

}

}

File file = findFileInConfiguration(configurations.runtime, ‘jacob-1.17-M2-x64’)

jacob.setNativeLibraryLocation(file.getParent())

}