3

how do I get the jar task to set the Main-Class header from the mainClassName variable?

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$ 
Thufir
  • 8,216
  • 28
  • 125
  • 273
  • 1
    What about simply using `mainClassName` or `project.mainClassName`? – Lukas Körfer Oct 21 '17 at 14:36
  • 1
    Or even not doing anything like that, since the application plugin does that for you? Remove the jar configuration entirely: the classpath is also wrong (and redundant): it should contain the runtime dependencies, not the compile ones. – JB Nizet Oct 21 '17 at 14:43
  • 2
    Sorry, I take that back. The application plugin doesn't set the main class and classpath in the manifest. But still, they're useless if you plan to use the start scripts generated by the application plugin. – JB Nizet Oct 21 '17 at 14:52
  • @JBNizet no worries :) is there another plugin which will do that? – Thufir Oct 21 '17 at 15:14

1 Answers1

7
version '1.0'

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

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

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

This will result in a MANIFEST.MF of :

Manifest-Version: 1.0
Main-Class: blah.blah.blah.blah.myMain
Pod
  • 3,938
  • 2
  • 37
  • 45