diff --git a/README.md b/README.md index 73d560316..3545cc85d 100644 --- a/README.md +++ b/README.md @@ -47,14 +47,7 @@ oomphIde { } ``` -See the [plugin's javadoc](https://diffplug.github.io/goomph/javadoc/3.4.0/com/diffplug/gradle/oomph/OomphIdePlugin.html) for more details. - -Examples (submit a PR with yours here!) - -- [Gradle and Eclipse RCP talk](https://github.com/diffplug/gradle_and_eclipse_rcp/blob/master/ide/build.gradle) (multi-project Eclipse RCP project) -- [ls-api](https://github.com/TypeFox/ls-api/blob/61a3089569acbe159f043534f282401452a34bc3/ide/build.gradle) (xtend IDE example) -- [Spotless](https://github.com/diffplug/spotless/blob/master/build.gradle) (single-project Gradle plugin) -- (your example here) +See the [plugin's javadoc](https://diffplug.github.io/goomph/javadoc/3.4.0/com/diffplug/gradle/oomph/OomphIdePlugin.html) for a quickstart, and [HOW_TO_AUTOMATE_IDE.md](HOW_TO_AUTOMATE_IDE.md) for examples and more in-depth details. ## Blog posts diff --git a/build.gradle b/build.gradle index 52bf371c6..7e126e437 100644 --- a/build.gradle +++ b/build.gradle @@ -62,6 +62,8 @@ dependencies { compileOnly 'p2:org.eclipse.equinox.common:3.7.0.v20150402-1709' compileOnly 'p2:org.eclipse.ui.workbench:3.107.1.v20160120-2131' compileOnly 'p2:org.eclipse.pde.core:3.10.102.v20160128-0556' + compileOnly 'p2:org.eclipse.jdt.launching:3.8.0.v20150527-0946' + compileOnly 'p2:org.eclipse.emf.ecore:2.11.2.v20160208-0816' // testing testCompile "junit:junit:${VER_JUNIT}" } diff --git a/src/main/java/com/diffplug/gradle/oomph/ConventionJdt.java b/src/main/java/com/diffplug/gradle/oomph/ConventionJdt.java index 0ff6352a6..c9a5455f3 100644 --- a/src/main/java/com/diffplug/gradle/oomph/ConventionJdt.java +++ b/src/main/java/com/diffplug/gradle/oomph/ConventionJdt.java @@ -15,10 +15,55 @@ */ package com.diffplug.gradle.oomph; +import java.util.HashSet; +import java.util.Set; + +import org.gradle.api.Action; + +/** + * Adding the JDT convention to your project + * adds the following features: + * + * - `org.eclipse.platform.ide` + * - `org.eclipse.jdt` + * - `org.eclipse.ui.views.log` + * + * You can set the installed JRE as follows: + * + * ```gradle + * oomphIde { + * jdt { + * installedJre { + * version = '1.6.0_45' + * installedLocation = new File('C:/jdk1.6.0_45') + * markDefault = true // or false + * executionEnvironments = ['JavaSE-1.6'] // any execution environments can be specified here. + * } + * } + * } + * ``` + */ public class ConventionJdt extends OomphConvention { ConventionJdt(OomphIdeExtension extension) { super(extension); requireIUs(IUs.IDE, IUs.JDT, IUs.ERROR_LOG); setPerspectiveOver(Perspectives.JAVA, Perspectives.RESOURCES); } + + final Set installedJres = new HashSet<>(); + + /** Adds an installed JRE with the given content. */ + public void installedJre(Action action) { + InstalledJre instance = new InstalledJre(); + action.execute(instance); + installedJres.add(instance); + } + + @Override + public void close() { + // add installed jres + if (!installedJres.isEmpty()) { + extension.addSetupAction(new InstalledJreAdder(installedJres)); + } + } } diff --git a/src/main/java/com/diffplug/gradle/oomph/ConventionPde.java b/src/main/java/com/diffplug/gradle/oomph/ConventionPde.java index ac4ef2948..5a3769cc8 100644 --- a/src/main/java/com/diffplug/gradle/oomph/ConventionPde.java +++ b/src/main/java/com/diffplug/gradle/oomph/ConventionPde.java @@ -23,6 +23,27 @@ import com.diffplug.gradle.OrderingConstraints; +/** + * Adding the PDE convention to your project + * adds the following features: + * + * - `org.eclipse.platform.ide` + * - `org.eclipse.jdt` + * - `org.eclipse.pde` + * + * You can set the targetplatform as follows: + * + * ```gradle + * oomphIde { + * pde { + * targetplatform { + * it.installation '../target.maven/build' + * it.installation '../target.p2/build/p2asmaven/p2runnable/eclipse-deps' + * } + * } + * } + * ``` + */ public class ConventionPde extends OomphConvention { ConventionPde(OomphIdeExtension extension) { super(extension); diff --git a/src/main/java/com/diffplug/gradle/oomph/InstalledJre.java b/src/main/java/com/diffplug/gradle/oomph/InstalledJre.java new file mode 100644 index 000000000..5b54b8fef --- /dev/null +++ b/src/main/java/com/diffplug/gradle/oomph/InstalledJre.java @@ -0,0 +1,62 @@ +/* + * Copyright 2016 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.gradle.oomph; + +import java.io.File; +import java.io.Serializable; +import java.util.List; + +/** Simple representation of a JRE */ +public class InstalledJre implements Serializable { + private static final long serialVersionUID = 8530657374964977698L; + + private String version; + private File installedLocation; + private boolean markDefault; + private List executionEnvironments; + + public String getVersion() { + return version; + } + + public void setVersion(String name) { + this.version = name; + } + + public File getInstalledLocation() { + return installedLocation; + } + + public void setInstalledLocation(File location) { + this.installedLocation = location; + } + + public boolean isMarkDefault() { + return markDefault; + } + + public void setMarkDefault(boolean markDefault) { + this.markDefault = markDefault; + } + + public List getExecutionEnvironments() { + return executionEnvironments; + } + + public void setExecutionEnvironments(List executionEnvironments) { + this.executionEnvironments = executionEnvironments; + } +} diff --git a/src/main/java/com/diffplug/gradle/oomph/InstalledJreAdder.java b/src/main/java/com/diffplug/gradle/oomph/InstalledJreAdder.java new file mode 100644 index 000000000..496e25257 --- /dev/null +++ b/src/main/java/com/diffplug/gradle/oomph/InstalledJreAdder.java @@ -0,0 +1,48 @@ +/* + * Copyright 2016 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.gradle.oomph; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import com.diffplug.gradle.OrderingConstraints; + +/** Used for adding JRE/JDK installations to an Eclipse install. */ +public class InstalledJreAdder extends SetupAction { + private static final long serialVersionUID = -7101059764345094433L; + + final List installedJres; + + protected InstalledJreAdder(Collection jresToAdd) { + super("com.diffplug.gradle.oomph.InstalledJreAdderInternal"); + installedJres = new ArrayList<>(jresToAdd); + } + + @Override + protected void populateOrdering(OrderingConstraints> ordering) { + // we must add installed jre(s) before importing projects + ordering.before(ProjectImporter.class); + } + + /** + * @see com.diffplug.gradle.oomph.SetupAction#getDescription() + */ + @Override + public String getDescription() { + return "adding installed JRE's"; + } +} diff --git a/src/main/java/com/diffplug/gradle/oomph/InstalledJreAdderInternal.java b/src/main/java/com/diffplug/gradle/oomph/InstalledJreAdderInternal.java new file mode 100644 index 000000000..8d2b1f2e6 --- /dev/null +++ b/src/main/java/com/diffplug/gradle/oomph/InstalledJreAdderInternal.java @@ -0,0 +1,99 @@ +/* + * Copyright 2016 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.gradle.oomph; + +import java.io.File; +import java.util.HashSet; +import java.util.Set; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.emf.ecore.util.EcoreUtil; +import org.eclipse.jdt.launching.IVMInstall; +import org.eclipse.jdt.launching.IVMInstallType; +import org.eclipse.jdt.launching.JavaRuntime; +import org.eclipse.jdt.launching.VMStandin; +import org.eclipse.jdt.launching.environments.IExecutionEnvironment; + +import com.diffplug.gradle.oomph.SetupAction.Internal; + +public class InstalledJreAdderInternal extends Internal { + + InstalledJreAdderInternal(InstalledJreAdder host) { + super(host); + } + + @Override + protected void runWithinEclipse() throws Throwable { + IVMInstallType[] types = JavaRuntime.getVMInstallTypes(); + for (IVMInstallType type : types) { + if ("org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType".equals(type.getId())) { + for (InstalledJre jreToAdd : host.installedJres) { + IVMInstall realVM = addInstalledJre(type, jreToAdd); + if (jreToAdd.isMarkDefault()) { + JavaRuntime.setDefaultVMInstall(realVM, new NullProgressMonitor()); + } + linkWithExecutionEnvironments(realVM, jreToAdd); + } + } + } + } + + protected IVMInstall addInstalledJre(IVMInstallType type, InstalledJre jreToAdd) throws Exception { + IVMInstall retVal = findJre(jreToAdd.getVersion(), jreToAdd.getInstalledLocation()); + if (retVal == null) { + IStatus validationStatus = type.validateInstallLocation(jreToAdd.getInstalledLocation()); + if (!validationStatus.isOK()) { + throw new CoreException(validationStatus); + } + VMStandin vmStandin = new VMStandin(type, EcoreUtil.generateUUID()); + vmStandin.setInstallLocation(jreToAdd.getInstalledLocation()); + vmStandin.setName("JRE for " + jreToAdd.getVersion()); + IVMInstall realVM = vmStandin.convertToRealVM(); + retVal = realVM; + } + return retVal; + } + + protected void linkWithExecutionEnvironments(IVMInstall installedVm, InstalledJre jreToAdd) { + if (jreToAdd.getExecutionEnvironments().isEmpty()) { + return; + } else { + Set execEnvsToAdd = new HashSet<>(jreToAdd.getExecutionEnvironments()); + IExecutionEnvironment[] executionEnvironments = JavaRuntime.getExecutionEnvironmentsManager() + .getExecutionEnvironments(); + for (IExecutionEnvironment iExecutionEnvironment : executionEnvironments) { + if (execEnvsToAdd.contains(iExecutionEnvironment.getId())) { + iExecutionEnvironment.setDefaultVM(installedVm); + } + } + } + } + + private IVMInstall findJre(String version, File location) throws Exception { + for (IVMInstallType vmInstallType : JavaRuntime.getVMInstallTypes()) { + for (IVMInstall vmInstall : vmInstallType.getVMInstalls()) { + File installLocation = vmInstall.getInstallLocation(); + if (location.equals(installLocation)) { + return vmInstall; + } + } + } + + return null; + } +} diff --git a/src/main/java/com/diffplug/gradle/oomph/OomphConvention.java b/src/main/java/com/diffplug/gradle/oomph/OomphConvention.java index fb56148eb..507f6eff5 100644 --- a/src/main/java/com/diffplug/gradle/oomph/OomphConvention.java +++ b/src/main/java/com/diffplug/gradle/oomph/OomphConvention.java @@ -21,7 +21,7 @@ * Base class for implementing a DSL * around a specific part of the IDE. */ -public class OomphConvention { +public class OomphConvention implements AutoCloseable { protected final OomphIdeExtension extension; OomphConvention(OomphIdeExtension extension) { @@ -45,4 +45,14 @@ protected void setPerspectiveOver(String toSet, String... toTrump) { extension.perspective(toSet); } } + + /** + * This is called when the convention block ends. + * + * Usually it can just be empty, but if you've been accumulating + * values, this is your chance to smush them down into + * a setup action (see {@link ConventionJdt}. + */ + @Override + public void close() {} } diff --git a/src/main/java/com/diffplug/gradle/oomph/OomphIdeExtension.java b/src/main/java/com/diffplug/gradle/oomph/OomphIdeExtension.java index 2f9d66da9..5ad995e26 100644 --- a/src/main/java/com/diffplug/gradle/oomph/OomphIdeExtension.java +++ b/src/main/java/com/diffplug/gradle/oomph/OomphIdeExtension.java @@ -379,19 +379,22 @@ void ideClean() { ///////////////// /** Convenience methods for setting the style. */ public void style(Action action) { - ConventionStyle convention = new ConventionStyle(this); - action.execute(convention); + try (ConventionStyle convention = new ConventionStyle(this)) { + action.execute(convention); + } } /** Adds the java development tools. */ public void jdt(Action action) { - ConventionJdt convention = new ConventionJdt(this); - action.execute(convention); + try (ConventionJdt convention = new ConventionJdt(this)) { + action.execute(convention); + } } /** Adds the plugin-development environment, @see ConventionPde. */ public void pde(Action action) { - ConventionPde convention = new ConventionPde(this); - action.execute(convention); + try (ConventionPde convention = new ConventionPde(this)) { + action.execute(convention); + } } } diff --git a/src/main/java/com/diffplug/gradle/oomph/OomphIdePlugin.java b/src/main/java/com/diffplug/gradle/oomph/OomphIdePlugin.java index 05e0085eb..bdb633d9e 100644 --- a/src/main/java/com/diffplug/gradle/oomph/OomphIdePlugin.java +++ b/src/main/java/com/diffplug/gradle/oomph/OomphIdePlugin.java @@ -31,7 +31,7 @@ * * - `gradlew ide` launches the IDE, after running any required setup tasks. * - * To create an IDE for java projects: + * To create an IDE for java projects (see {@link ConventionJdt} for more JDT options). * * ```groovy * apply plugin: 'com.diffplug.gradle.oomph.ide' @@ -41,17 +41,12 @@ * } * ``` * - * For an Eclipse Plugin project with a target platform: + * For an Eclipse Plugin project (see {@link ConventionPde} for more JDT options). * * ```groovy * oomphIde { * repoEclipse '4.5.2' - * pde { - * targetplatform { - * installation 'target.maven/build' - * installation 'target.p2/build/p2asmaven/p2' - * } - * } + * pde {} * } * ``` *