我如何设置一个简单的使用sqlite4java的gradle项目? [英] How can I set up a simple gradle project that uses sqlite4java?

查看:149
本文介绍了我如何设置一个简单的使用sqlite4java的gradle项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我可以很容易地下载核心sqlite4java库,但我不确定什么是最好的(任何!)方式来获取gradle来下载本地库并将它们放在正确的位置。



这是我的build.gradle文件:

  apply plugin:'java'

/ *我们使用Java 1.7 * /
sourceCompatibility = 1.7
targetCompatibility = 1.7
version ='1.0'

仓库{
mavenCentral()
}

sourceSets {
main {
java.srcDir'src'
output.classesDir ='build / main'
}
test {
java.srcDir'test'
output.classesDir ='build / test'
}
}


依赖关系{
testCompile组:'junit',name:'junit ',版本:'4.11'
编译com.almworks.sqlite4java:sqlite4java:1.0.392
编译com.almworks.sqlite4java:libsqlite4java-osx:1.0.392
}

但是当我运行一个简单的测试时,我得到:

  TestTest> testSqlite4Basic FAILED 
com.almworks.sqlite4java.SQLiteException在TestTest.java:15
引起:java.lang.UnsatisfiedLinkError在TestTest.java:15

(我是从IntelliJ内部构建的,但是使用了gradle构建选项 - 所以我不认为它是在运行测试时填充类路径的ItelliJ ...)



我非常确定第一次尝试构建时,我收到了一些关于无法解压libsqlite4java-osx ZIP文件的消息(不奇怪因为maven central说它是一个dylib文件)。

我需要做什么才能让gradle做正确的事情?



ASIDE :我可以通过手动将下载的 .dylib 从maven缓存复制到列出的某个位置来运行代码在我的 java.library.path (想我在我的Mac上使用 $ HOME / Library / Java / Extensions )。但是这似乎与将 .gradle 文件中的所有设置和依赖关系打包在一起,并且不会让我稍后轻松分发任何东西。

解决方案

以下是基于Stanislav评论的完整答案。 $ b

 应用插件:'java'

/ *我们使用Java 1.8 * /
sourceCompatibility = 1.8
targetCompatibility = 1.8
version ='1.0'

repositories {mavenCentral()}

dependencies {
testCompile group:'junit',name:'junit',version:'4.11'
compilecom.almworks .sqlite4java:sqlite4java:1.0.392
compilecom.almworks.sqlite4java:libsqlite4java-osx:1.0.392


sourceSets {
main {
java.srcDir'src'
output.classesDir ='build / main'
}
test {
java.srcDir'test'
输出。 classesDir ='build / test'
}
}

复制原始文件* /
任务copyNativeDeps(类型:复制){
from(configurations.compile + configurations.testcompile){
include* .dylib

到'build / libs'
}

/ *确保我们设置了测试来实际复制
*本机文件并设置正确的路径。 * /
test {
dependsOn copyNativeDeps
systemPropertyjava.library.path,'build / libs'
}

以及运行它的示例测试源代码:

  import com.almworks.sqlite4java.SQLiteConnection; 
import com.almworks.sqlite4java.SQLiteStatement;
import org.junit.Test;

import java.io.File;

public class SqliteTest {

@Test public void aTest()throws Exception {
SQLiteConnection db = new SQLiteConnection(new File(/ tmp / database) );
db.open(true);

SQLiteStatement st = db.prepare(SELECT name FROM dummy);
while(st.step()){
System.err.printf(name =%s\,st.columnString(1));
}
} finally {
st.dispose();
}
}
}


I'm starting a simple java test project using sqlite4java and building using java.

I can get the core sqlite4java library downloaded easily, but I'm not sure what the best (any!) way to get gradle to download the native libraries and put them in the right place.

This is my build.gradle file:

apply plugin: 'java'

/* We use Java 1.7 */
sourceCompatibility = 1.7
targetCompatibility = 1.7
version = '1.0'

repositories {
    mavenCentral()
}

sourceSets {
    main {
        java.srcDir 'src'
        output.classesDir = 'build/main'
    }
    test {
        java.srcDir 'test'
        output.classesDir = 'build/test'
    }
}


dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
    compile "com.almworks.sqlite4java:sqlite4java:1.0.392"
    compile "com.almworks.sqlite4java:libsqlite4java-osx:1.0.392"
}

But when I run a simple test I get:

TestTest > testSqlite4Basic FAILED
    com.almworks.sqlite4java.SQLiteException at TestTest.java:15
        Caused by: java.lang.UnsatisfiedLinkError at TestTest.java:15

(I'm building from within IntelliJ, but am using the gradle build options - so I don't think it's ItelliJ stuffing up the class path when running the tests...)

I'm pretty sure the first time I tried to build I got some message about being unable to unpack the libsqlite4java-osx ZIP file, (not surprisingly as maven central says its a dylib file).

What do I need to do to make gradle do the right thing?

ASIDE: I'm able to get the code to run by manually copying the downloaded .dylib from the maven cache into somewhere listed in my java.library.path (Think I used $HOME/Library/Java/Extensions on my mac). But this seems contrary to the whole point of packaging all setup and dependencies in the .gradle file, and wont let me distribute anything easily later.

解决方案

Here's a complete answer based on Stanislav's comments.

apply plugin: 'java'

/* We use Java 1.8 */
sourceCompatibility = 1.8
targetCompatibility = 1.8
version = '1.0'

repositories { mavenCentral() }

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
    compile "com.almworks.sqlite4java:sqlite4java:1.0.392"
    compile "com.almworks.sqlite4java:libsqlite4java-osx:1.0.392"
}

sourceSets {
    main {
        java.srcDir 'src'
        output.classesDir = 'build/main'
    }
    test {
        java.srcDir 'test'
        output.classesDir = 'build/test'
    }
}

/* Copy the native files */
task copyNativeDeps(type: Copy) {
    from (configurations.compile+configurations.testCompile) {
        include "*.dylib"
    }
    into 'build/libs'
}

/* Make sure we setup the tests to actually copy 
 * the native files and set the paths correctly. */
test {
    dependsOn copyNativeDeps
    systemProperty "java.library.path", 'build/libs'
}

And example test source to run for it:

import com.almworks.sqlite4java.SQLiteConnection;
import com.almworks.sqlite4java.SQLiteStatement;
import org.junit.Test;

import java.io.File;

public class SqliteTest {

    @Test public void aTest() throws Exception {
        SQLiteConnection db = new SQLiteConnection(new File("/tmp/database"));
        db.open(true);  

        SQLiteStatement st = db.prepare("SELECT name FROM dummy");
        try {
            while(st.step()) {
                System.err.printf("name = %s\n", st.columnString(1));
            }
        } finally {
            st.dispose();
        }
    }
}

这篇关于我如何设置一个简单的使用sqlite4java的gradle项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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