Java Sqlite Gradle

我非常喜欢gradle和
java ..

我有一个使用sqlite的项目,它通过intellij想法运行良好,但我不能从终端运行它,它抛出一个异常:

java.lang.ClassNotFoundException: org.sqlite.JDBC

我试图在运行项目时获得intellij idea产生的进程:

/usr/lib/jvm/java-7-openjdk-amd64/bin/java -agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=42986 -Dfile.encoding=UTF-8 -cp /home/user/my_project/target/classes/main:/home/user/my_project/target/resources/main:/home/user/.gradle/caches/modules-2/files-2.1/org.xerial/sqlite-jdbc/3.7.2/7a3d67f00508d3881650579f7f228c61bfc1b196/sqlite-jdbc-3.7.2.jar Main

正如我在这里看到的想法指定classpath这是解决方案,但如何在不指定classpath的情况下运行项目的.jar文件?我的意思是可以自动gradle生成清单(因为它成功从maven org.xerial存储库中检索sqlite-jdbc)并将有效的类路径放在那里?
我想从终端成功运行它.

UPD:我不能仅通过gradle run命令从终端运行,但是我无法通过java -jar myproject.jar运行该项目.

这是我的build.gradle:

apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'idea'
apply plugin: 'application'

group = 'myproject'
version = '0.3'

description = """my project description"""

buildDir = "target"

mainClassName = "Main"

repositories {
     maven { url "http://repo.maven.apache.org/maven2" }
}

dependencies {
    testCompile group: 'junit', name: 'junit', version:'3.8.1'
    compile 'org.xerial:sqlite-jdbc:3.7.2'
}


jar {
    manifest.attributes("Main-Class": "Main")
}

/* Overwriting distribution tasks: */
task distZip(type:Zip, overwrite:true) {
    archiveName = "$project.name-$version" + '.zip'
    from "target/libs"
}

task distTar(type:Tar, overwrite:true) {
    archiveName = "$project.name-$version" + '.tar'
    from "target/libs"
}

抛出异常的Java源代码:

try {
    Class.forName("org.sqlite.JDBC");

    this.connection = DriverManager.getConnection(databaseName);
} catch (Exception e) {
    e.printStackTrace();
}

最佳答案 我通过修改我的jar部分,在项目的分发jar文件的类路径中添加sqlite来解决这个问题:

task copyDependenciesToTarget(type: Copy) {
    println 'Copying dependencies to target...'

    configurations.compile.collect().each { compileDependency ->
        copy {
            with from (compileDependency.getPath()) {
                include '*'
            }
            into 'target/libs/libs'
        }
    }
}

build.dependsOn(copyDependenciesToTarget)


jar {
    manifest.attributes(
            "Main-Class": "Main",
            "Class-Path": configurations.compile.collect { 'libs/' + it.getName()}.join(' ')
    )
}

现在gradle下载依赖项并将它们复制到libs /目录.

点赞