How can I set up a simple gradle project that uses sqlite4java?

后端 未结 4 1078
粉色の甜心
粉色の甜心 2020-12-20 12:08

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

4条回答
  •  悲哀的现实
    2020-12-20 13:00

    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();
            }
        }
    }
    

提交回复
热议问题