Gradle输出罐没有Main-Class [英] Gradle Output Jar has no Main-Class
问题描述
我有以下简单的build.gradle文件:
I have the following simple build.gradle file:
apply plugin: 'application'
apply plugin: 'java'
mainClassName = 'com.kurtis.HelloGradle'
以及位于src/main/java/com/kurtis/HelloGradle.java
的以下单个Java文件:
And the following single java file located at src/main/java/com/kurtis/HelloGradle.java
:
package com.kurtis;
public class HelloGradle {
public static void main(String[] args) {
System.out.println("Hello gradle");
}
}
但是,如果我运行gradle build
,我会在build/lib
目录中得到一个没有设置主类的jar.它的清单文件没有Main-Class
条目.为什么会这样?
However, if I run gradle build
I get a jar in the build/lib
directory that has no main class set. It's manifest file has no Main-Class
entry. Why is this?
推荐答案
这是由于应用程序插件的工作方式所致. mainClassName
是application
插件使用的属性,当它创建可执行jar时,清单中不需要Main-Class
,因为它不使用它.
It's due to the way application plugin works. mainClassName
is a property used by application
plugin, when it creates an executable jar, it doesn't need to have a Main-Class
in the manifest, since it doesn't use it.
如果需要在jar中添加Main-Class
,则必须按java
插件的jar
配置提供它,如下所示:
If you need to have a Main-Class
in your jar, then you have to provide it vie jar
configuration of the java
plugin as follows:
jar {
manifest {
attributes(
'Main-Class': 'com.kurtis.HelloGradle'
)
}
}
否则,只需使用由应用程序插件生成的可执行文件,无论是通过run
任务还是通过它可以为您创建的发行版,都可以使用脚本来运行您的应用程序及其所有依赖项.
Otherwise, just use an executable, which is generated by application plugin, whether via run
task or via distributions it can create for you, with scripts to run your application and all it's dependencies.
这篇关于Gradle输出罐没有Main-Class的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!