使用mainClassName中的gradle设置Main-Class标头

时间:2017-10-21 14:33:22

标签: java gradle jar build.gradle manifest.mf

如何让jar任务从Main-Class变量设置mainClassName标题?

thufir@dur:~/NetBeansProjects/props$ 
thufir@dur:~/NetBeansProjects/props$ gradle clean assembleDist;java -jar build/libs/props.jar 

BUILD SUCCESSFUL in 1s
6 actionable tasks: 6 executed

Publishing build scan...
https://gradle.com/s/qwirajeu4guhq

no main manifest attribute, in build/libs/props.jar
thufir@dur:~/NetBeansProjects/props$ 
thufir@dur:~/NetBeansProjects/props$ cat build.gradle 
plugins {
    id 'com.gradle.build-scan' version '1.8' 
    id 'java'
    id 'application'
}

buildScan {
    licenseAgreementUrl = 'https://gradle.com/terms-of-service'
    licenseAgree = 'yes'
}

buildScan {
    publishAlways()
}

sourceCompatibility = 1.9
targetCompatibility = 1.9

mainClassName = 'net.bounceme.dur.props.Main'

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

repositories {
    jcenter()
}

dependencies {
    // This dependency is found on compile classpath of this component and consumers.
  //  compile 'com.google.guava:guava:22.0'

    // Use JUnit test framework
//    testCompile 'junit:junit:4.12'
}


thufir@dur:~/NetBeansProjects/props$ 

1 个答案:

答案 0 :(得分:2)

version '1.0'

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

mainClassName = 'blah.blah.blah.blah.myMain'

jar {
  manifest {
    attributes(
      'Main-Class': mainClassName
    )
  }
}

这将导致MANIFEST.MF的:

Manifest-Version: 1.0
Main-Class: blah.blah.blah.blah.myMain