Skip to content

Commit

Permalink
allow configuring the main class in application.mainClass
Browse files Browse the repository at this point in the history
  • Loading branch information
siordache committed Jun 12, 2020
1 parent fc8bb0e commit d3dcd8f
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 14 deletions.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.4.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip
14 changes: 6 additions & 8 deletions src/main/groovy/org/beryx/runtime/data/JPackageData.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
*/
package org.beryx.runtime.data

import org.beryx.runtime.util.Util
import org.gradle.api.plugins.ApplicationPlugin
import org.gradle.api.plugins.ApplicationPluginConvention
import org.gradle.api.plugins.JavaPlugin

import static org.beryx.runtime.util.Util.EXEC_EXTENSION

import groovy.transform.CompileStatic
Expand Down Expand Up @@ -82,7 +87,7 @@ class JPackageData {

@Input
String getMainClass() {
this.@mainClass ?: defaultMainClass
this.@mainClass ?: Util.getMainClass(project)
}

@Input
Expand All @@ -106,13 +111,6 @@ class JPackageData {
}


private String getDefaultMainClass() {
def mainClass = project['mainClassName'] as String
int pos = mainClass.lastIndexOf('/')
if(pos < 0) return mainClass
mainClass.substring(pos + 1)
}

private static String getDefaultJPackageHome() {
def value = System.properties['badass.runtime.jpackage.home']
if(value) return value
Expand Down
29 changes: 27 additions & 2 deletions src/main/groovy/org/beryx/runtime/util/Util.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.logging.Logger
import org.gradle.api.logging.Logging
import org.gradle.api.plugins.ApplicationPluginConvention
import org.gradle.api.plugins.JavaPlugin
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Property
Expand Down Expand Up @@ -138,7 +139,7 @@ class Util {
}

@CompileDynamic
public static File getArchiveFile(Project project) {
static File getArchiveFile(Project project) {
Jar jarTask = (Jar)project.tasks.getByName(JavaPlugin.JAR_TASK_NAME)
if(GradleVersion.current() < GradleVersion.version('5.1')) {
return jarTask.archivePath
Expand All @@ -147,12 +148,36 @@ class Util {
}
}

public static File getMainDistJarFile(Project project) {
static File getMainDistJarFile(Project project) {
File jarFile = getArchiveFile(project)
if(project.tasks.findByName('installShadowDist')) {
def baseName = jarFile.name - '.jar'
jarFile = new File(jarFile.parent, "$baseName-all.jar")
}
jarFile
}

static String getMainClass(Project project) {
def mainClass = getRawMainClass(project)
if(!mainClass) throw new GradleException("mainClass not configured")
int pos = mainClass.lastIndexOf('/')
if(pos < 0) return mainClass
mainClass.substring(pos + 1)
}

@CompileDynamic
static String getRawMainClass(Project project) {
String mainClass
if(GradleVersion.current() < GradleVersion.version('6.4')) {
def convention = project.convention.plugins['application'] as ApplicationPluginConvention
mainClass = convention.mainClassName
if(!mainClass) {
mainClass = project['mainClassName'] as String
}
} else {
mainClass = project.tasks.run.mainClass.get()
}
mainClass
}

}
2 changes: 1 addition & 1 deletion src/test/groovy/org/beryx/runtime/RuntimePluginSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,6 @@ class RuntimePluginSpec extends Specification {
['java.naming'] | true | false | '4.8'
['java.naming', 'java.xml'] | true | true | '5.4.1'
['java.naming', 'java.xml', 'java.logging'] | true | true | '6.3'
['java.naming', 'java.xml', 'foo.bar'] | false | false | '6.4.1'
['java.naming', 'java.xml', 'foo.bar'] | false | false | '6.5'
}
}
7 changes: 5 additions & 2 deletions src/test/resources/hello-log4j-2.9.0/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ dependencies {
compile "javax.xml.bind:jaxb-api:2.3.0"
}

mainClassName = 'org.example.runtime.hello.Hello'
application {
mainClass = 'org.example.runtime.hello.Hello'
}

jar {
manifest {
attributes 'Implementation-Title': "hello",
'Main-Class': mainClassName
'Main-Class': 'org.example.runtime.hello.Hello'
}
}

Expand Down
1 change: 1 addition & 0 deletions src/test/resources/hello-logback/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dependencies {
}

mainClassName = 'org.example.runtime.Hello'

jar {
manifest {
attributes 'Implementation-Title': "runtime-hello",
Expand Down

0 comments on commit d3dcd8f

Please sign in to comment.