java Gradle -无主清单属性

lnxxn5zx  于 2023-04-19  发布在  Java
关注(0)|答案(4)|浏览(129)

我正在用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) }
}
aelbi1ox

aelbi1ox1#

尝试更改清单属性,如:

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

然后只需将'hello.helloWorld'更改为'<your packagename>.<the name of your Main class>'(其中Main类有一个main方法)。在这种情况下,您在清单中创建一个属性,该属性指向此类,然后jar正在运行。

z31licg0

z31licg02#

要使jar文件可执行(以便java -jar命令工作),请在MANIFEST.MF中指定Main-Class属性。
在Gradle中,您可以通过配置jar任务来完成此操作。

  • 对于GroovyDSL,请参见这些答案(1(https://stackoverflow.com/a/32567525/6486622),2(https://stackoverflow.com/a/43088446/6486622))
  • 对于KotlinDSL,可以使用以下代码片段:
tasks.withType<Jar> {
    manifest {
        attributes["Main-Class"] = "com.caco3.Main"
    }
}

为什么mainClassName不能正常工作?

或者为什么mainClassName没有在清单中指定属性?
mainClassName属性来自application plugin。插件:
在开发过程中可以轻松地在本地启动应用程序,并将应用程序打包 * 为TAR和/或ZIP***,包括操作系统特定的启动脚本 *。

  • 所以application插件的目的不是生成可执行的jar s*

mainClassName属性设置时,则:

  1. $ ./gradlew run将在属性中指定的类中启动main方法
    1.使用distZip/distTar任务构建的zip/tar存档将包含一个脚本,该脚本将启动指定的先前类的main方法。
    下面是设置主类的shell脚本行:
$ grep Main2 gradletest
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLETEST_OPTS -classpath "\"$CLASSPATH\"" com.caco3.gradletest.Main2 "$APP_ARGS"
ovfsdjhp

ovfsdjhp3#

为了补充Denis Zavedeev answer,这里有更多的Kotlin DSL(build.gradle.kts)方法:

tasks.jar {
    manifest.attributes["Main-Class"] = "com.example.MyMainClass"
}

另一个符号:

tasks.jar {
    manifest {
        attributes["Main-Class"] = "com.example.MyMainClass"
    }
}

旁注:要创建可运行的fat JAR(也称为uber JAR),请参阅this post

vpfxa7rd

vpfxa7rd4#

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

相关问题