Java – gradle build dependency throws classnotfoundexception

I am currently writing my first gradle build script to help build a simple java application from the command line

Here is the complete 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 assembly – info command shows the dependencies downloaded from maven, but when I try to start jar from the command line, I receive 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 httpcomponents HttpCore artefact, so I don't understand why the JVM has problems finding classes

Any comments are appreciated

Solution

After some research, I successfully built my jar using the following build script

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'])
    }
}

Comment appreciation

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>