Gradle-没有主要的清单属性

时间:2015-09-14 14:18:11

标签: java gradle

在运行Gradle构建的JAR文件时,我遇到了这个错误。错误读取“没有主要的清单属性,在RxJavaDemo.jar中”我试图操纵Manifest属性,但我想我忘了添加依赖项或其他东西。我究竟做错了什么?

apply plugin: 'java'
apply plugin: 'application'

mainClassName = 'demo.MainDashboard'

dependencies {

    compile files ("H:/Processes/Development/libraries/hikari-cp/HikariCP-2.4.1.jar")
    compile files ("H:/Processes/Development/libraries/controls-fx/controlsfx.jar")
    compile files ("H:/Processes/Development/libraries/database_connections/sqlite-jdbc-3.8.6.jar")
    compile files ("H:/Processes/Development/libraries/guava/guava-18.0.jar")
    compile files ("H:/Processes/Development/libraries/rxjava/rxjava-1.0.12.jar")
    compile files ("H:/Processes/Development/libraries/rxjava-extras/rxjava-extras-0.5.15.jar")
    compile files ("H:/Processes/Development/libraries/rxjavafx/RxJavaFX-1.0.0-RC1-SNAPSHOT.jar")
    compile files ("H:/Processes/Development/libraries/rxjavaguava/rxjava-guava-1.0.3.jar")
    compile files ("H:/Processes/Development/libraries/rxjava-jdbc/rxjava-jdbc-0.6.3.jar")
    compile files ("H:/Processes/Development/libraries/slf4j/slf4j-api-1.7.12.jar")
    compile files ("H:/Processes/Development/libraries/tom-commons/tom-commons.jar")

}

sourceSets {
    main.java.srcDir "src/main/java"
    main.resources.srcDir "src/main/resources"
}

    jar { 
  manifest {
    attributes(
      "Class-Path": configurations.compile.collect { it.getName() }.join(' '))
  }
    from configurations.compile.collect { entry -> zipTree(entry) }
}

3 个答案:

答案 0 :(得分:99)

尝试更改清单属性,例如:

jar {
  manifest {
    attributes(
      'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
      'Main-Class': 'hello.HelloWorld'
    )
  }
}

然后只需将'hello.helloWorld'更改为您的班级,该班级有一个主要方法。在这种情况下,您在清单中创建一个指向此类的属性,然后运行一个jar。

答案 1 :(得分:4)

要使jar文件可执行(以便java -jar命令起作用),请在Main-Class中指定MANIFEST.MF属性。

在Gradle中,您可以通过配置jar任务来实现。

  • 对于 Groovy DSL,请参见以下答案([1][2]
  • 对于 Kotlin DSL,您可以使用以下代码段:
tasks.withType<Jar> {
    manifest {
        attributes["Main-Class"] = "com.caco3.Main"
    }
}

为什么mainClassName无法正常工作?

还是为什么mainClassName没有在清单中指定属性?

mainClassName属性来自application plugin。插件:

可以轻松地在开发过程中在本地启动应用程序,以及将应用程序作为 TAR 和/或 ZIP打包打包 ,包括特定于操作系统的启动脚本

因此,application插件并非旨在产生可执行文件jar

设置了mainClassName属性后,

  1. $ ./gradlew run将在属性中指定的类中启动main方法
  2. 使用zip / tar任务构建的distZip / distTar归档文件将包含一个脚本,该脚本将启动先前指定类的main方法。

这是设置主类的shell脚本行:

$ grep Main2 gradletest
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLETEST_OPTS -classpath "\"$CLASSPATH\"" com.caco3.gradletest.Main2 "$APP_ARGS"

答案 2 :(得分:0)

FWIW-我使用以下jar任务将所有编译依赖项汇编到jar文件中,并使用上述建议来正确设置类路径

apply plugin: 'java-library'

jar {
  manifest {
    attributes(
      'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
      'Main-Class': 'your.main.class.goes.here'
    )
  }

  // You can reference any part of the dependency configurations,
  // and you can have as many from statements as you need
  from configurations.compile  
  // I just copied them into the top of the jar, so it looks like the eclipse exported 
  // runnable jar, but you could designate a lib directory, and reference that in the 
  // classpath as "lib/$it.name" instead of it.getName()
  into ''   
}