/* * Copyright (c) 2012, Danno Ferrin * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of Danno Ferrin nor the * names of contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */packageorg.bitbucket.shemnon.javafxpluginimportorg.gradle.api.GradleExceptionimportorg.gradle.api.JavaVersionimportorg.gradle.api.Projectimportorg.gradle.api.Pluginimportorg.bitbucket.shemnon.javafxplugin.tasks.JavaFXDeployTaskimportorg.bitbucket.shemnon.javafxplugin.tasks.JavaFXJarTaskimportorg.gradle.api.plugins.JavaPluginConventionimportorg.gradle.api.file.FileCollectionimportorg.gradle.api.tasks.SourceSetimportorg.gradle.api.tasks.JavaExecimportorg.gradle.api.artifacts.ConfigurationContainerimportorg.gradle.api.artifacts.Configurationimportorg.gradle.api.plugins.JavaPluginimportorg.bitbucket.shemnon.javafxplugin.tasks.JavaFXCSSToBinTaskimportorg.bitbucket.shemnon.javafxplugin.tasks.JavaFXSignJarTaskimportorg.bitbucket.shemnon.javafxplugin.tasks.GenKeyTaskimportorg.gradle.api.DefaultTaskimportorg.gradle.api.artifacts.ResolvedArtifactimportorg.gradle.internal.os.OperatingSystemclassJavaFXPluginimplementsPlugin<Project>{publicstaticfinalStringPROVIDED_COMPILE_CONFIGURATION_NAME="providedCompile";publicstaticfinalStringPROVIDED_RUNTIME_CONFIGURATION_NAME="providedRuntime";staticStringgetOSProfileName(){defcurrentOS=OperatingSystem.current();if(currentOS.isWindows()){return'windows'}if(currentOS.isLinux()){return'linux'}if(currentOS.isMacOsX()){return'macosx'}returnnull;}privateProjectproject@LazyprivateString[]profiles=([]+project.getProperties().profiles?.split(',')+getOSProfileName()).flatten().findAll{project.javafx.getProfile(it)!=null}protectedbasicExtensionMapping={prop,convention=null,aware=null->JavaFXPluginExtensionext=project.javafx;for(profileinprofiles){Mapoverride=ext.getProfile(profile)defval=override[prop]if(val!=null){returnval;}}returnext[prop]}@Overridevoidapply(Projectproject){this.project=projectproject.getPlugins().apply(JavaPlugin)project.extensions.create('javafx',JavaFXPluginExtension)configureConfigurations(project.configurations)defjfxrtJarFile=project.files(findJFXJar())project.javafx{jfxrtJar=jfxrtJarFileantJavaFXJar=project.files(findAntJavaFXJar())appName=project.name//FIXME capatalizepackaging='all'signingMode='release'}project.dependencies{providedCompilejfxrtJarFile}project.sourceSets{'package'{resources{srcDir'src/deploy/package'}}}configureJavaFXCSSToBinTask(project)configureJavaFXJarTask(project)configureGenerateDebugKeyTask(project)configureJavaFXSignJarTask(project)configureJFXCopyLibsTask(project)configureJFXDeployTask(project)configureScenicViewTask(project)configureRunTask(project)configureDebugTask(project)defmains=[]project.convention.getPlugin(JavaPluginConvention).sourceSets[SourceSet.MAIN_SOURCE_SET_NAME].allJava.visit{if(it.relativePath.lastName=='Main.java'){mains.add(it.relativePath.replaceLastName('Main').pathString.replace('/','.'))}}if(mains.size()==1){project.javafx.mainClass=mains[0]}}privateconfigureJavaFXCSSToBinTask(Projectproject){deftask=project.task("cssToBin",type:JavaFXCSSToBinTask,description:"Converts CSS to Binary CSS.",group:'Build')task.conventionMapping.distsDir={convention,aware->convention.getPlugin(JavaPluginConvention).sourceSets.main.output.resourcesDir}task.conventionMapping.inputFiles={convention,aware->convention.getPlugin(JavaPluginConvention).sourceSets.main.resources}project.tasks.getByName("classes").dependsOn(task)task.dependsOn(project.tasks.getByName("processResources"))}privateconfigureJavaFXJarTask(Projectproject){deftask=project.task("jfxJar",type:JavaFXJarTask,description:"Adds JavaFX specific packaging to the jar.",group:'Build',dependsOn:'jar')project.afterEvaluate{project.configurations.archives.artifacts*.builtBytask}['mainClass','embedLauncher','arguments'].each{prop->task.conventionMapping[prop]=basicExtensionMapping.curry(prop)}task.conventionMapping.jarFile={convention,aware->project.tasks.getByName("jar").archivePath}task.conventionMapping.classpath={convention,aware->FileCollectioncompileClasspath=project.convention.getPlugin(JavaPluginConvention).sourceSets[SourceSet.MAIN_SOURCE_SET_NAME].compileClasspath;ConfigurationprovidedCompile=project.configurations[PROVIDED_COMPILE_CONFIGURATION_NAME];FileCollectionoutput=compileClasspath-providedCompile;}}privateconfigureGenerateDebugKeyTask(Projectproject){deftask=project.task("generateDebugKey",type:GenKeyTask,description:"Generates the JavaFX Debug Key.",group:'Build')project.afterEvaluate{task.enabled=task.enabled&&project.javafx.debugKey!=null}task.conventionMapping.alias={convention,aware->project.javafx.debugKey?.alias}task.conventionMapping.keyPass={convention,aware->project.javafx.debugKey?.keyPass}task.conventionMapping.keyStore={convention,aware->project.javafx.debugKey?.keyStore}task.conventionMapping.storePass={convention,aware->project.javafx.debugKey?.storePass}task.conventionMapping.storeType={convention,aware->project.javafx.debugKey?.storeType}task.conventionMapping.dname={convention,aware->'CN=JavaFX Gradle Plugin Default Debug Key, O=JavaFX Debug'}task.conventionMapping.validity={convention,aware->((365.25)*25asint)/* 25 years */}}privateconfigureJavaFXSignJarTask(Projectproject){deftask=project.task("jfxSignJar",type:JavaFXSignJarTask,description:"Signs the JavaFX jars the JavaFX way.",group:'Build',dependsOn:'jfxJar')project.afterEvaluate{project.configurations.archives.artifacts*.builtBytasktask.enabled=task.enabled&&(project.javafx.debugKey!=null||project.javafx.releaseKey!=null)}['alias','keyPass','storePass','storeType'].each{prop->task.conventionMapping[prop]={convention,aware->defjfxc=project.javafx;defprops=project.propertiesdefmode=props['javafx.signingMode']?:jfxc.signingModereturnprops?."javafx.${mode}Key.$prop"?:jfxc?."${mode}Key"?."$prop"}}task.conventionMapping.keyStore={convention,aware->defjfxc=project.javafx;defprops=project.propertiesdefmode=props['javafx.signingMode']?:jfxc.signingModeStringkeyFile=props?."javafx.${mode}Key.keyStore"returnkeyFile==null?jfxc?."${mode}Key"?.keyStore:newFile(project.projectDir,keyFile)}task.conventionMapping.outdir={convention,aware->project.libsDir}task.conventionMapping.inputFiles={convention,aware->FileCollectionruntimeClasspath=project.convention.getPlugin(JavaPluginConvention).sourceSets[SourceSet.MAIN_SOURCE_SET_NAME].runtimeClasspath;ConfigurationprovidedRuntime=project.configurations[PROVIDED_RUNTIME_CONFIGURATION_NAME];project.files(runtimeClasspath-providedRuntime,project.configurations.archives.artifacts.files.collect{it})}task.dependsOn(project.tasks.getByName("jfxJar"))}privateconfigureJFXCopyLibsTask(Projectproject){deftask=project.task("jfxCopyLibs")task.doLast{FileCollectionruntimeClasspath=project.convention.getPlugin(JavaPluginConvention).sourceSets[SourceSet.MAIN_SOURCE_SET_NAME].runtimeClasspath;ConfigurationprovidedRuntime=project.configurations[PROVIDED_RUNTIME_CONFIGURATION_NAME];project.files(runtimeClasspath-providedRuntime,project.configurations.archives.artifacts.files.collect{it}).findAll{!it.directory}.each{ant.copy(file:it,toDir:project.libsDir,overwrite:false)}}task.dependsOn(project.tasks.getByName("jfxSignJar"))task.dependsOn(project.tasks.getByName("jfxJar"))}privateconfigureJFXDeployTask(Projectproject){deftask=project.task("jfxDeploy",type:JavaFXDeployTask,description:"Processes the JavaFX jars and generates webstart and native packages.",group:'Build')['antJavaFXJar','appID','appName','arguments','category','codebase','copyright','description','embedJNLP','height','iconInfos','id','installSystemWide','javaRuntime','jvmArgs','licenseType','mainClass','menu','offlineAllowed','packaging','shortcut','systemProperties','updateMode','vendor','width',].each{prop->task.conventionMapping[prop]=basicExtensionMapping.curry(prop)}// version is specialtask.conventionMapping.version={convention,aware->('unspecified'==project.version)?'0.0.0':project.version}task.conventionMapping.inputFiles={convention,aware->project.fileTree(project.libsDir).include("*.jar")}task.conventionMapping.resourcesDir={convention,aware->defrd=project.sourceSets['package'].output.resourcesDirif(!rd.exists())rd.mkdirs()rd}task.conventionMapping.distsDir={convention,aware->project.distsDir}task.dependsOn(project.tasks.getByName("jfxCopyLibs"))task.dependsOn(project.tasks.getByName("packageClasses"))project.tasks.getByName("assemble").dependsOn(task)}privatevoidconfigureRunTask(Projectproject){JavaExectask=project.task("run",type:JavaExec,description:'Runs the application.',group:'Execution')configureRunParams(project,task)}protectedvoidconfigureRunParams(Projectproject,JavaExectask){task.classpath=project.sourceSets.main.runtimeClasspathtask.conventionMapping.main=basicExtensionMapping.curry('mainClass')task.doFirst{task.jvmArgsbasicExtensionMapping('jvmArgs')task.systemPropertiesbasicExtensionMapping('systemProperties')if(!task.args)task.args=basicExtensionMapping('arguments')}}privatevoidconfigureDebugTask(Projectproject){JavaExectask=project.task("debug",type:JavaExec,description:'Runs the applicaiton and sets up debugging on port 5005.',group:'Execution')configureRunParams(project,task)task.debug=true}privatevoidconfigureScenicViewTask(Projectproject){deftask=project.task("scenicview",type:DefaultTask,description:'Adds the ScenicView agent to all Execution Tasks.',group:'Tools')task.doLast{project.configurations{scenicview}project.repositories{ivy{url'https://repository-javafx-gradle-plugin.forge.cloudbees.com/release'}}project.dependencies{if(JavaVersion.current().java8Compatible){scenicview'com.fxexperience.scenicview:scenicview:8.0-dp1'}else{scenicview'com.fxexperience.scenicview:scenicview:1.3.0'}}project.tasks.findAll{it.group=='Execution'&&itinstanceofJavaExec}.each{JavaExecexecTask->project.configurations.getByName('scenicview').resolvedConfiguration.resolvedArtifacts.each{ResolvedArtifactra->execTask.jvmArgs=["-javaagent:$ra.file.canonicalPath"]+execTask.jvmArgs}}}}publicvoidconfigureConfigurations(ConfigurationContainerconfigurationContainer){ConfigurationprovideCompileConfiguration=configurationContainer.add(PROVIDED_COMPILE_CONFIGURATION_NAME).setVisible(false).setDescription("Additional compile classpath for libraries that should not be part of the WAR archive.");ConfigurationprovideRuntimeConfiguration=configurationContainer.add(PROVIDED_RUNTIME_CONFIGURATION_NAME).setVisible(false).extendsFrom(provideCompileConfiguration).setDescription("Additional runtime classpath for libraries that should not be part of the WAR archive.");configurationContainer.getByName(JavaPlugin.COMPILE_CONFIGURATION_NAME).extendsFrom(provideCompileConfiguration);configurationContainer.getByName(JavaPlugin.RUNTIME_CONFIGURATION_NAME).extendsFrom(provideRuntimeConfiguration);}privateFilesearchFile(Map<String,Closure>places,List<String>searchPaths,StringsearchID){Fileresult=null;places.each{k,v->if(result!=null)return;project.logger.debug("Looking for $searchID in $k")defdir=v()if(dir==null){project.logger.debug("$k not set")}else{project.logger.debug("$k is $dir")try{result=project.fileTree(dir:dir,include:searchPaths).singleFileproject.logger.debug("found $searchID as $result")}catch(IllegalStateExceptionignore){// no file or two filesproject.logger.debug("either zero files or more than one file matched $searchID, ignoring");}}}if(!result?.file){println(""" Could not find $searchID, please set one of $places.keys""")thrownewGradleException("$searchID not found.\n ${log.join('\n')}");}else{project.logger.info("$searchID: ${result}")returnresult}}publicFilefindJFXJar(){returnsearchFile(['JFXRT_HOME in System Environment':{System.env['JFXRT_HOME']},'JAVA_HOME in System Environment':{System.env['JAVA_HOME']},'java.home in JVM properties':{System.properties['java.home']}],['jfxrt.jar','lib/jfxrt.jar','lib/ext/jfxrt/jar','jre/lib/jfxrt.jar','jre/lib/ext/jfxrt.jar'],'JavaFX Runtime Jar')}publicFilefindAntJavaFXJar(){returnsearchFile(['JFXRT_HOME in System Environment':{System.env['JFXRT_HOME']},'JAVA_HOME in System Environment':{System.env['JAVA_HOME']},'java.home in JVM properties':{System.properties['java.home']}],['lib/ant-javafx.jar'],'JavaFX Packager Tools')}}