import org.apache.tools.ant.filters.ReplaceTokens import org.gradle.internal.os.OperatingSystem import org.gradle.plugins.ide.eclipse.model.* import groovy.transform.ExternalizeMethods //import org.eclipse.osgi.* import com.diffplug.gradle.GoomphCacheLocations /* buildscript { repositories { repos mavenCentral() jcenter() } dependencies { //eclipseDeps.each { compile "p2:${it}:+" } //classpath 'org.eclipse.platform:org.eclipse.osgi:3.15.0' //compile group: 'org.eclipse.platform', name: 'org.eclipse.osgi', version: '3.15.0' //classpath 'biz.aQute.bnd:biz.aQute.bnd.gradle:4.4.0' } } */ plugins { id 'java' id 'application' id 'eclipse' id 'com.diffplug.gradle.oomph.ide' version '3.18.1' id 'com.diffplug.gradle.equinoxlaunch' version '3.18.1' //id 'com.diffplug.gradle.p2.asmaven' version '3.18.1' //id 'org.eclipse.osgi' version '3.15.0' //id 'osgi' } /* def eclipseDeps = [ // The dependencies we actually use 'org.eclipse.jdt.core', 'org.eclipse.text', // Their transitives 'org.eclipse.core.contenttype', 'org.eclipse.core.jobs', 'org.eclipse.core.runtime', 'org.eclipse.core.resources', 'org.eclipse.equinox.common', 'org.eclipse.equinox.preferences', //'org.eclipse.osgi' ] */ /* p2AsMaven { group 'p2', { repoEclipse goomph_eclipse_version eclipseDeps.each { p2.addIU(it) } eclipseDeps.each { p2.addIU(it + '.source') } } } */ /* dependencies { //eclipseDeps.each { compile "p2:${it}:+" } //implementation 'org.eclipse.platform:org.eclipse.osgi:3.15.0' //compile 'org.eclipse.platform:org.eclipse.osgi:3.15.0' //classpath 'biz.aQute.bnd:biz.aQute.bnd.gradle:4.4.0' } */ repositories { jcenter() mavenCentral() 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) if (file(eclipse_bin_dir+"/main").isDirectory()) { cp.entries += new Output(eclipse_bin_dir+"/main") } if (file(helpParentDir).isDirectory()) { cp.entries += new Library(fileReference(helpParentDir)) } if (file(resourceDir).isDirectory()) { 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 } */ GoomphCacheLocations.override_workspaces = file(jalviewDirAbsolutePath+"/"+goomph_workspace) oomphIde { repoEclipse goomph_eclipse_version jdt { /* installedJre { //markDefault = true //executionEnvironments = [ eclipse.jdt.javaRuntimeName ] } */ //compilerComplianceLevel( JAVA_INTEGER_VERSION ) } //repo 'http://download.eclipse.org/buildship/updates/e45/milestones/3.x/' repo goomph_repo_buildship feature goomph_feature_buildship } equinoxLaunch { headlessAppSetup { launchTask 'ideJalviewjsBuild', { //it.args = ["-nosplash", "--launcher.suppressErrors", "-application", "org.eclipse.jdt.apt.core.aptBuild", "-data", tempEclipseWorkspace, "-"+jalviewjs_eclipseBuildArg, eclipse_project_name ] it.args = ["-nosplash", "--launcher.suppressErrors", "-application", "org.eclipse.jdt.apt.core.aptBuild", "-"+jalviewjs_eclipseBuildArg, eclipse_project_name ] } } } task ideCopyDropins (type: Copy) { dependsOn ideSetupP2 def inputFiles = fileTree(jalviewjs_utils_dir+"/"+jalviewjs_eclipse_dropins_dir) def outputDir = oomphIde.ideDir + "/" + com.diffplug.gradle.FileMisc.macContentsEclipse() + "/dropins" from inputFiles into outputDir def outputFiles = [] rename { filename -> outputFiles += outputDir+"/"+filename println("COPYING ${filename} to ${outputFiles}") null } outputs.files outputFiles inputs.files inputFiles } ide.dependsOn eclipseProject ide.dependsOn eclipseClasspath ide.dependsOn eclipseJdt ide.dependsOn ideCopyDropins ideJalviewjsBuild.dependsOn ideSetupWorkspace ideJalviewjsBuild.dependsOn ideCopyDropins def jalviewjsBuildDir def jalviewjsSiteDir task jalviewjsSitePath { if (jalviewjs_site_dir.startsWith("/")) { jalviewjsSiteDir = jalviewjs_site_dir } else { def relativeBuildDir = file(jalviewDirAbsolutePath).toPath().relativize(buildDir.toPath()) jalviewjsBuildDir = "${relativeBuildDir}/jalviewjs" jalviewjsSiteDir = jalviewjsBuildDir + "/" + jalviewjs_site_dir } } def tempEclipseWorkspace task jalviewjsSetTempEclipseWorkspace { tempEclipseWorkspace = file(jalviewjs_eclipse_workspace) if (!tempEclipseWorkspace.exists()) { tempEclipseWorkspace = file("${buildDir}/tmp/eclipse-workspace") //tempEclipseWorkspace.deleteOnExit() } println("ECLIPSE WORKSPACE: "+tempEclipseWorkspace.getPath()) } task jalviewjsUnzipFiles { dependsOn jalviewjsSitePath 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 jalviewjsSiteDir } } } inputs.files zipFiles outputs.dir jalviewjsSiteDir } def eclipseDropinsDir def eclipseBinary 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" eclipseBinary = eclipseRoot+"/Contents/MacOS/eclipse" } else if (OperatingSystem.current().isWindows()) { // check these paths!! eclipseDropinsDir = eclipseRoot+"/dropins" eclipseBinary = eclipseRoot+"/eclipse" } else { // linux or unix eclipseDropinsDir = eclipseRoot+"/dropins" eclipseBinary = eclipseRoot+"/eclipse" } } 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 jalviewjsEclipseSetup { dependsOn jalviewjsEclipseCopyDropins dependsOn jalviewjsSetTempEclipseWorkspace } task jalviewjsCreateJ2sSettings(type: WriteProperties) { dependsOn jalviewjsSitePath outputFile (jalviewDir+"/"+jalviewjs_j2s_settings) def props = project.properties.sort { it.key } def siteDirProperty = "j2s.site.directory" def setSiteDir = false props.each { prop, val -> if (prop.startsWith("j2s.") && val != null) { if (prop == siteDirProperty) { if (!(val.startsWith("/") || val.startsWith("file://") )) { val = jalviewjsSiteDir+"/"+val } setSiteDir = true } property(prop,val) } if (!setSiteDir) { property(siteDirProperty,"${jalviewjsSiteDir}") } } outputs.file(outputFile) } task jalviewjsCopyResources (type: Copy) { dependsOn jalviewjsSitePath def inputFiles = fileTree(dir: jalviewjs_resource_dir) def outputDir = jalviewjsSiteDir+"/"+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) { dependsOn jalviewjsSitePath def inputFiles = fileTree(dir: jalviewjs_utils_dir+"/"+jalviewjs_site_resource_dir) def outputDir = jalviewjsSiteDir from inputFiles into outputDir def outputFiles = [] rename { filename -> outputFiles += outputDir+"/"+filename null } outputs.files outputFiles inputs.files inputFiles } task cleanJalviewjs { dependsOn jalviewjsSitePath /* delete jalviewDir+"/"+jalviewjsSiteDir 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) outputs.upToDateWhen { file(projdir).exists() } } 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 ]) def stdout def stderr doFirst { stdout = new ByteArrayOutputStream() stderr = new ByteArrayOutputStream() standardOutput = new org.apache.tools.ant.util.TeeOutputStream(new FileOutputStream("${jalviewjsBuildDir}/${jalviewjs_j2s_stdout}"), stdout); errorOutput = new org.apache.tools.ant.util.TeeOutputStream(new FileOutputStream("${jalviewjsBuildDir}/${jalviewjs_j2s_stderr}"), stderr); } doLast { if (stdout.toString().contains("Error processing ")) { // j2s did not complete transpile throw new TaskExecutionException("Error during transpilation:\n${stderr}\n") } } inputs.dir(sourceDir) outputs.dir(eclipse_bin_dir+"/main") outputs.dir(jalviewjsSiteDir+"/"+jalviewjs_j2s_subdir) outputs.file(jalviewjsSiteDir+jalviewjs_server_resource) outputs.upToDateWhen { file(jalviewjsSiteDir+jalviewjs_server_resource).exists() } } task jalviewjsBuildSite { dependsOn jalviewjsSitePath 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 jalviewjsSiteDir into jalviewjs_site_dir // this is inside the tar file inputs.dir(jalviewjsSiteDir) } task jalviewjs { dependsOn jalviewjsBuildSite } def jalviewjsServer = null task jalviewjsServerStart { dependsOn jalviewjsSitePath 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 { def doc_root = new File(jalviewDirAbsolutePath +"/"+ jalviewjsSiteDir) jalviewjsServer = factory.start(doc_root, port) running = true println("SERVER STARTED with document root ${doc_root}.\nGo 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.") } } } */ project.afterEvaluate { tasks.findByName('ideJalviewjsBuild').dependsOn eclipseProject tasks.findByName('ideJalviewjsBuild').dependsOn eclipseClasspath tasks.findByName('ideJalviewjsBuild').dependsOn eclipseJdt }