我目前正在编写我的第一个Gradle构建脚本,以帮助构建一个从命令行使用的简单Java应用程序.
下面是完整的build.gradle代码
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'
}
运行gradle assemble –info命令显示从Maven下载的依赖项,但是当我尝试从命令行启动Jar时,我收到以下错误:
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包含在httpcomponents.httpcore artefact中,因此我不明白为什么JVM在查找类时遇到问题.
任何评论都表示赞赏.
解决方法:
经过一些研究后,我使用以下构建脚本成功构建了我的Jar.
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'])
}
}
评论赞赏
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。