Gradle build dependancy throwing ClassNotFoundException

那年仲夏 提交于 2019-12-12 09:45:45

问题


I am currently writing my first Gradle build script to help structure a simple Java app to be used from the command line.

Below is the full build.gradle code

apply plugin: 'java'
apply plugin: 'eclipse'

defaultTasks 'clean', 'build'

repositories {
    mavenCentral()
}

jar {
baseName = 'napier-deploy'
version =  '1'
manifest {attributes 'Main-Class': 'com.Main'}
}

dependencies {
    compile 'org.apache.httpcomponents:fluent-hc:4.3.3'
    compile 'org.apache.httpcomponents:httpclient-cache:4.3.3'
    compile 'org.apache.httpcomponents:httpcore:4.1'
    compile 'org.apache.httpcomponents:httpmime:4.3.3'
    compile 'org.apache.httpcomponents:httpclient:4.3.3'

    testCompile 'junit:junit:4.11'
    testCompile 'org.mockito:mockito-all:1.9.5'
}

task wrapper(type: Wrapper) {
    gradleVersion = '1.11'
}

Running the gradle assemble --info command shows the dependancies being downloaded from Maven, but when I try to launch the Jar from the commandline I get the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/HttpEntity
    at com.Main.main(Main.java:17)
Caused by: java.lang.ClassNotFoundException: org.apache.http.HttpEntity
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    ... 1 more

HttpEntity is included in the httpcomponents.httpcore artefact, so I do not see why the JVM is having trouble finding the class.

Any comments are appreciated.


回答1:


After some research I have used the following build script to build my Jar successfully.

apply plugin: 'java'
apply plugin: 'eclipse'

defaultTasks 'clean', 'build'

repositories {
    mavenCentral()
}

jar {
    from {
        configurations.compile.collect {
            it.isDirectory() ? it : zipTree(it)
        }
        configurations.runtime.collect {
            it.isDirectory() ? it : zipTree(it)
        }
    }
    manifest {
        attributes 'Implementation-Title': 'ndeploy',
            'Implementation-Version': '0.1.0',
            'Built-By': System.getProperty('user.name'),
            'Built-Date': new Date(),
            'Built-JDK': System.getProperty('java.version'),
            'Main-Class': 'com.Main'
    }
}

dependencies {
    compile 'org.apache.httpcomponents:fluent-hc:4.3.3'
    compile 'org.apache.httpcomponents:httpclient-cache:4.3.3'
    compile 'org.apache.httpcomponents:httpcore:4.1'
    compile 'org.apache.httpcomponents:httpmime:4.3.3'
    compile 'org.apache.httpcomponents:httpclient:4.3.3'
    testCompile 'junit:junit:4.11'
    testCompile 'org.mockito:mockito-all:1.9.5'
}

buildscript {
    dependencies {
        classpath fileTree(dir: '../../build/libs', include: '*.jar', excludes: ['*javadoc.jar', '*sources.jar'])
    }
}

Comments appreciated




回答2:


Since the runtime configuration also contains the compile configuration, then you can exclude a snippet of code which collecting compile config from the example above.



来源:https://stackoverflow.com/questions/22911190/gradle-build-dependancy-throwing-classnotfoundexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!