Skip to content

Add installed jre #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
}
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/com/diffplug/gradle/oomph/ConventionJdt.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<InstalledJre> installedJres = new HashSet<>();

/** Adds an installed JRE with the given content. */
public void installedJre(Action<InstalledJre> 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));
}
}
}
21 changes: 21 additions & 0 deletions src/main/java/com/diffplug/gradle/oomph/ConventionPde.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
62 changes: 62 additions & 0 deletions src/main/java/com/diffplug/gradle/oomph/InstalledJre.java
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> getExecutionEnvironments() {
return executionEnvironments;
}

public void setExecutionEnvironments(List<String> executionEnvironments) {
this.executionEnvironments = executionEnvironments;
}
}
48 changes: 48 additions & 0 deletions src/main/java/com/diffplug/gradle/oomph/InstalledJreAdder.java
Original file line number Diff line number Diff line change
@@ -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<InstalledJre> installedJres;

protected InstalledJreAdder(Collection<InstalledJre> jresToAdd) {
super("com.diffplug.gradle.oomph.InstalledJreAdderInternal");
installedJres = new ArrayList<>(jresToAdd);
}

@Override
protected void populateOrdering(OrderingConstraints<Class<? extends SetupAction>> 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";
}
}
Original file line number Diff line number Diff line change
@@ -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<InstalledJreAdder> {

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<String> 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;
}
}
12 changes: 11 additions & 1 deletion src/main/java/com/diffplug/gradle/oomph/OomphConvention.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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() {}
}
15 changes: 9 additions & 6 deletions src/main/java/com/diffplug/gradle/oomph/OomphIdeExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -379,19 +379,22 @@ void ideClean() {
/////////////////
/** Convenience methods for setting the style. */
public void style(Action<ConventionStyle> 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<ConventionJdt> 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<ConventionPde> action) {
ConventionPde convention = new ConventionPde(this);
action.execute(convention);
try (ConventionPde convention = new ConventionPde(this)) {
action.execute(convention);
}
}
}
Loading