import org.apache.tools.ant.filters.ReplaceTokens import org.gradle.internal.os.OperatingSystem import org.gradle.plugins.ide.eclipse.model.* import groovy.transform.ExternalizeMethods plugins { id 'java' id 'application' id 'eclipse' id 'com.diffplug.gradle.oomph.ide' version '3.18.0' } repositories { jcenter() mavenCentral() mavenLocal() flatDir { dirs gradlePluginsDir } } mainClassName = launcherClass def classes = "$jalviewDir/$classesDir" // configure classpath/args for j8/j11 compilation def jalviewDirAbsolutePath = file(jalviewDir).getAbsolutePath() def libDir def libDistDir def compile_source_compatibility def compile_target_compatibility ext { buildProperties = jalviewDir + "/" + classesDir +"/" + buildPropertiesFile gitHash = "" gitBranch = "" jalviewjsServer = "" } def JAVA_INTEGER_VERSION //def additional_compiler_args = [] // this property is for the Java library used in eclipse def eclipse_java_runtime_name if (JAVA_VERSION.equals("1.8")) { JAVA_INTEGER_VERSION = "8" //libDir = j8libDir libDir = j11libDir libDistDir = j8libDir compile_source_compatibility = 1.8 compile_target_compatibility = 1.8 eclipse_java_runtime_name = "JavaSE-1.8" } else if (JAVA_VERSION.equals("11")) { JAVA_INTEGER_VERSION = "11" libDir = j11libDir libDistDir = j11libDir compile_source_compatibility = 11 compile_target_compatibility = 11 eclipse_java_runtime_name = "JavaSE-11" /* compile without modules -- using classpath libraries additional_compiler_args += [ '--module-path', ext.modules_compileClasspath.asPath, '--add-modules', j11modules ] */ } else if (JAVA_VERSION.equals("12") || JAVA_VERSION.equals("13")) { JAVA_INTEGER_VERSION = JAVA_VERSION libDir = j11libDir libDistDir = j11libDir compile_source_compatibility = JAVA_VERSION compile_target_compatibility = JAVA_VERSION eclipse_java_runtime_name = "JavaSE-11" /* compile without modules -- using classpath libraries additional_compiler_args += [ '--module-path', ext.modules_compileClasspath.asPath, '--add-modules', j11modules ] */ } else { throw new GradleException("JAVA_VERSION=$JAVA_VERSION not currently supported by Jalview") } sourceSets { main { java { srcDirs "$jalviewDir/$sourceDir" outputDir = file("$classes") } resources { srcDirs "$jalviewDir/$resourceDir" } compileClasspath = files(sourceSets.main.java.outputDir) compileClasspath += fileTree(dir: "$jalviewDir/$libDir", include: ["*.jar"]) runtimeClasspath = compileClasspath } } eclipse { project { name = eclipse_project_name natures 'org.eclipse.jdt.core.javanature', 'org.eclipse.buildship.core.gradleprojectnature' buildCommand 'org.eclipse.jdt.core.javabuilder' buildCommand 'org.eclipse.buildship.core.gradleprojectbuilder' } classpath { //defaultOutputDir = sourceSets.main.java.outputDir def removeThese = [] configurations.each{ if (it.isCanBeResolved()) { removeThese += it } } minusConfigurations += removeThese plusConfigurations = [ ] file { whenMerged { cp -> def removeTheseToo = [] HashMap addedSrcPath = new HashMap<>(); cp.entries.each { entry -> if (entry.kind == 'src') { if (addedSrcPath.getAt(entry.path) || !(entry.path == "src" || entry.path == "test")) { removeTheseToo += entry } else { addedSrcPath.putAt(entry.path, true) } } } cp.entries.removeAll(removeTheseToo) cp.entries += new Output(eclipse_bin_dir+"/main") cp.entries += new Library(fileReference(helpParentDir)) cp.entries += new Library(fileReference(resourceDir)) HashMap addedLibPath = new HashMap<>(); // changing from sourcesets.main.classpath to specific Java version lib //sourceSets.main.compileClasspath.each{ fileTree("$jalviewDir/$libDistDir").include("**/*.jar").include("*.jar").each { //don't want to add outputDir as eclipse is using its own output dir in bin/main if (it.isDirectory() || ! it.exists()) { // don't add dirs to classpath return } def itPath = it.toString() if (itPath.startsWith(jalviewDirAbsolutePath+"/")) { itPath = itPath.substring(jalviewDirAbsolutePath.length()+1) } if (addedLibPath.get(itPath)) { //println("Not adding duplicate entry "+itPath) } else { //println("Adding entry "+itPath) cp.entries += new Library(fileReference(itPath)) addedLibPath.put(itPath, true) } } } // whenMerged } // file containers 'org.eclipse.buildship.core.gradleclasspathcontainer' } // classpath jdt { // for the IDE, use java 11 compatibility sourceCompatibility = compile_source_compatibility targetCompatibility = compile_target_compatibility javaRuntimeName = eclipse_java_runtime_name /* file { withProperties { props -> def jalview_prefs = new Properties() def ins = new FileInputStream(jalviewDirAbsolutePath+"/"+eclipse_extra_jdt_prefs_file) jalview_prefs.load(ins) ins.close() jalview_prefs.forEach { t, v -> if (props.getAt(t) == null) { props.putAt(t, v) } } } } */ } // jdt //synchronizationTasks eclipseClasspath //autoBuildTasks eclipseClasspath } // eclipse /* compileJava { doFirst { sourceCompatibility = compile_source_compatibility targetCompatibility = compile_target_compatibility //options.compilerArgs = additional_compiler_args print ("Setting target compatibility to "+targetCompatibility+"\n") } } clean { delete sourceSets.main.java.outputDir } */ task jalviewjsUnzipFiles { def zipFiles = fileTree(dir: jalviewjs_utils_dir+"/"+jalviewjs_libjs_dir).include("*.zip") zipFiles += jalviewjs_utils_dir+"/"+jalviewjs_swingjs_zip doLast { zipFiles.each { file_zip -> copy { from zipTree(file_zip) into jalviewjs_site_dir } } } inputs.files zipFiles outputs.dir jalviewjs_site_dir } def eclipseBinary def eclipseDropinsDir def eclipsePluginsDir task jalviewjsEclipsePaths { def eclipseRoot = jalviewjs_eclipse_root if (eclipseRoot.startsWith("~")) { eclipseRoot = System.getProperty("user.home") + eclipseRoot.substring(1) } if (OperatingSystem.current().isMacOsX()) { eclipseRoot += "/Eclipse.app" eclipseDropinsDir = eclipseRoot+"/Contents/Eclipse/dropins" eclipsePluginsDir = eclipseRoot+"/Contents/Eclipse/plugins" eclipseBinary = eclipseRoot+"/Contents/MacOS/eclipse" } else if (OperatingSystem.current().isWindows()) { // check these paths!! eclipseDropinsDir = eclipseRoot+"/dropins" eclipsePluginsDir = eclipseRoot+"/plugins" eclipseBinary = eclipseRoot+"/eclipse" } else { // linux or unix eclipseDropinsDir = eclipseRoot+"/dropins" eclipsePluginsDir = eclipseRoot+"/plugins" eclipseBinary = eclipseRoot+"/eclipse" } println("ECLIPSE ROOT: "+eclipseRoot) } task jalviewjsEclipseCopyDropins (type: Copy) { dependsOn jalviewjsEclipsePaths def inputFiles = fileTree(jalviewjs_utils_dir+"/"+jalviewjs_eclipse_dropins_dir) def outputDir = eclipseDropinsDir from inputFiles into outputDir def outputFiles = [] rename { filename -> outputFiles += outputDir+"/"+filename null } outputs.files outputFiles inputs.files inputFiles } task jalviewjsEclipseCopyPlugins (type: Copy) { dependsOn jalviewjsEclipsePaths def inputFiles = fileTree(jalviewjs_utils_dir+"/"+jalviewjs_eclipse_plugins_dir) def outputDir = eclipsePluginsDir from inputFiles into outputDir def outputFiles = [] rename { filename -> outputFiles += outputDir+"/"+filename null } outputs.files outputFiles inputs.files inputFiles } def tempEclipseWorkspace = "" task jalviewjsSetTempEclipseWorkspace { tempEclipseWorkspace = file(jalviewjs_eclipse_workspace) if (!tempEclipseWorkspace.exists()) { tempEclipseWorkspace = File.createTempDir() tempEclipseWorkspace.deleteOnExit() } println("ECLIPSE WORKSPACE: "+tempEclipseWorkspace.getPath()) } oomphIde { repoEclipseLatest() jdt { } repo 'http://download.eclipse.org/buildship/updates/e45/milestones/3.x/' feature 'org.eclipse.buildship' } task jalviewjsEclipseSetup { dependsOn jalviewjsEclipseCopyDropins dependsOn jalviewjsEclipseCopyPlugins dependsOn jalviewjsSetTempEclipseWorkspace } task jalviewjsCreateJ2sSettings(type: WriteProperties) { outputFile (jalviewDir+"/"+jalviewjs_j2s_settings) def props = project.properties.sort { it.key } props.each { prop, val -> if (prop.startsWith("j2s.") && val != null) { property(prop,val) } } outputs.file(outputFile) } task jalviewjsCopyResources (type: Copy) { def inputFiles = fileTree(dir: jalviewjs_resource_dir) def outputDir = jalviewjs_site_dir+"/"+jalviewjs_j2s_subdir from inputFiles into outputDir def outputFiles = [] rename { filename -> outputFiles += outputDir+"/"+filename null } outputs.files outputFiles inputs.files inputFiles } task jalviewjsCopySiteResources (type: Copy) { def inputFiles = fileTree(dir: jalviewjs_utils_dir+"/"+jalviewjs_site_resource_dir) def outputDir = jalviewjs_site_dir from inputFiles into outputDir def outputFiles = [] rename { filename -> outputFiles += outputDir+"/"+filename null } outputs.files outputFiles inputs.files inputFiles } task cleanJalviewjs { /* delete jalviewDir+"/"+jalviewjs_site_dir delete jalviewDir+"/"+eclipse_bin_dir delete file(tempEclipseWorkspace.getAbsolutePath()+"/.metadata") delete jalviewDir+"/"+jalviewjs_j2s_settings */ } task jalviewjsProjectImport(type: Exec) { // work out how to do this! dependsOn eclipseProject dependsOn eclipseClasspath dependsOn eclipseJdt dependsOn jalviewjsEclipsePaths dependsOn jalviewjsEclipseSetup executable(eclipseBinary) args(["-nosplash", "--launcher.suppressErrors", "-application", "com.seeq.eclipse.importprojects.headlessimport", "-data", tempEclipseWorkspace.getPath(), "-import", jalviewDirAbsolutePath]) def projdir = tempEclipseWorkspace.getPath()+"/.metadata/.plugins/org.eclipse.core.resources/.projects/jalview/org.eclipse.jdt.core/" inputs.file(jalviewDir+"/.project") outputs.dir(projdir) } task jalviewjsTranspile(type: Exec) { dependsOn jalviewjsCreateJ2sSettings dependsOn jalviewjsProjectImport dependsOn jalviewjsEclipsePaths executable(eclipseBinary) args(["-nosplash", "--launcher.suppressErrors", "-application", "org.eclipse.jdt.apt.core.aptBuild", "-data", tempEclipseWorkspace, "-"+jalviewjs_eclipseBuildArg, eclipse_project_name ]) inputs.dir(sourceDir) outputs.dir(eclipse_bin_dir+"/main") outputs.file(eclipse_bin_dir+"/main/jalview/bin/Jalview.class") outputs.dir(jalviewjs_site_dir+"/"+jalviewjs_j2s_subdir) } task jalviewjsBuildSite { dependsOn jalviewjsUnzipFiles dependsOn jalviewjsCopyResources dependsOn jalviewjsCopySiteResources dependsOn jalviewjsTranspile } task jalviewjsSiteTar(type: Tar) { dependsOn jalviewjsBuildSite def outputFilename = "site.tar.gz" try { archiveFileName = outputFilename } catch (Exception e) { archiveName = outputFilename } compression Compression.GZIP from jalviewjs_site_dir into jalviewjs_site_dir // this is inside the tar file inputs.dir(jalviewjs_site_dir) } task jalviewjs { dependsOn jalviewjsBuildSite } def jalviewjsServer = null task jalviewjsServerStart { doLast { if (jalviewjsServer != null) { println("SERVER ALREADY RUNNING. Go to "+jalviewjsServer.getResourceUrl(jalviewjs_server_resource)+" . Run gradle jalviewjsServerStop to stop.") } else { SimpleHttpFileServerFactory factory = new SimpleHttpFileServerFactory() def port = Integer.valueOf(jalviewjs_server_port) def start = port def running = false while(port < start+1000 && !running) { try { jalviewjsServer = factory.start(new File(jalviewDirAbsolutePath+"/"+jalviewjs_site_dir), port) running = true println("SERVER STARTED. Go to "+jalviewjsServer.getResourceUrl(jalviewjs_server_resource)+" . Run gradle jalviewjsServerStop to stop.") //println("Ctrl-c to stop.");java.lang.Thread.sleep(Integer.valueOf(jalviewjs_server_wait)*1000); } catch (Exception e) { port++; } } } } } /* server persists in gradle daemon but reference is not accessible across builds task jalviewjsServerStop { doLast { if (jalviewjsServer != null) { println("SERVER ON PORT "+jalviewjsServer.getPort()+" STOPPING. Run gradle jalviewjsServerStart to start again.") jalviewjsServer.stop() } else { println("SERVER NOT RUNNING. Run gradle jalviewjsServerStart to start.") } } } */