Skip to content

Commit a5ad8f3

Browse files
authored
Merge pull request #16 from scottresnik/add-installed-jre
Add installed jre
2 parents 5de7bb1 + e86e016 commit a5ad8f3

File tree

10 files changed

+301
-23
lines changed

10 files changed

+301
-23
lines changed

README.md

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,7 @@ oomphIde {
4747
}
4848
```
4949

50-
See the [plugin's javadoc](https://diffplug.github.io/goomph/javadoc/3.4.0/com/diffplug/gradle/oomph/OomphIdePlugin.html) for more details.
51-
52-
Examples (submit a PR with yours here!)
53-
54-
- [Gradle and Eclipse RCP talk](https://github.com/diffplug/gradle_and_eclipse_rcp/blob/master/ide/build.gradle) (multi-project Eclipse RCP project)
55-
- [ls-api](https://github.com/TypeFox/ls-api/blob/61a3089569acbe159f043534f282401452a34bc3/ide/build.gradle) (xtend IDE example)
56-
- [Spotless](https://github.com/diffplug/spotless/blob/master/build.gradle) (single-project Gradle plugin)
57-
- (your example here)
50+
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.
5851

5952
## Blog posts
6053

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ dependencies {
6262
compileOnly 'p2:org.eclipse.equinox.common:3.7.0.v20150402-1709'
6363
compileOnly 'p2:org.eclipse.ui.workbench:3.107.1.v20160120-2131'
6464
compileOnly 'p2:org.eclipse.pde.core:3.10.102.v20160128-0556'
65+
compileOnly 'p2:org.eclipse.jdt.launching:3.8.0.v20150527-0946'
66+
compileOnly 'p2:org.eclipse.emf.ecore:2.11.2.v20160208-0816'
6567
// testing
6668
testCompile "junit:junit:${VER_JUNIT}"
6769
}

src/main/java/com/diffplug/gradle/oomph/ConventionJdt.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,55 @@
1515
*/
1616
package com.diffplug.gradle.oomph;
1717

18+
import java.util.HashSet;
19+
import java.util.Set;
20+
21+
import org.gradle.api.Action;
22+
23+
/**
24+
* Adding the JDT convention to your project
25+
* adds the following features:
26+
*
27+
* - `org.eclipse.platform.ide`
28+
* - `org.eclipse.jdt`
29+
* - `org.eclipse.ui.views.log`
30+
*
31+
* You can set the installed JRE as follows:
32+
*
33+
* ```gradle
34+
* oomphIde {
35+
* jdt {
36+
* installedJre {
37+
* version = '1.6.0_45'
38+
* installedLocation = new File('C:/jdk1.6.0_45')
39+
* markDefault = true // or false
40+
* executionEnvironments = ['JavaSE-1.6'] // any execution environments can be specified here.
41+
* }
42+
* }
43+
* }
44+
* ```
45+
*/
1846
public class ConventionJdt extends OomphConvention {
1947
ConventionJdt(OomphIdeExtension extension) {
2048
super(extension);
2149
requireIUs(IUs.IDE, IUs.JDT, IUs.ERROR_LOG);
2250
setPerspectiveOver(Perspectives.JAVA, Perspectives.RESOURCES);
2351
}
52+
53+
final Set<InstalledJre> installedJres = new HashSet<>();
54+
55+
/** Adds an installed JRE with the given content. */
56+
public void installedJre(Action<InstalledJre> action) {
57+
InstalledJre instance = new InstalledJre();
58+
action.execute(instance);
59+
installedJres.add(instance);
60+
}
61+
62+
@Override
63+
public void close() {
64+
// add installed jres
65+
if (!installedJres.isEmpty()) {
66+
extension.addSetupAction(new InstalledJreAdder(installedJres));
67+
}
68+
}
2469
}

src/main/java/com/diffplug/gradle/oomph/ConventionPde.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,27 @@
2323

2424
import com.diffplug.gradle.OrderingConstraints;
2525

26+
/**
27+
* Adding the PDE convention to your project
28+
* adds the following features:
29+
*
30+
* - `org.eclipse.platform.ide`
31+
* - `org.eclipse.jdt`
32+
* - `org.eclipse.pde`
33+
*
34+
* You can set the targetplatform as follows:
35+
*
36+
* ```gradle
37+
* oomphIde {
38+
* pde {
39+
* targetplatform {
40+
* it.installation '../target.maven/build'
41+
* it.installation '../target.p2/build/p2asmaven/p2runnable/eclipse-deps'
42+
* }
43+
* }
44+
* }
45+
* ```
46+
*/
2647
public class ConventionPde extends OomphConvention {
2748
ConventionPde(OomphIdeExtension extension) {
2849
super(extension);
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2016 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.gradle.oomph;
17+
18+
import java.io.File;
19+
import java.io.Serializable;
20+
import java.util.List;
21+
22+
/** Simple representation of a JRE */
23+
public class InstalledJre implements Serializable {
24+
private static final long serialVersionUID = 8530657374964977698L;
25+
26+
private String version;
27+
private File installedLocation;
28+
private boolean markDefault;
29+
private List<String> executionEnvironments;
30+
31+
public String getVersion() {
32+
return version;
33+
}
34+
35+
public void setVersion(String name) {
36+
this.version = name;
37+
}
38+
39+
public File getInstalledLocation() {
40+
return installedLocation;
41+
}
42+
43+
public void setInstalledLocation(File location) {
44+
this.installedLocation = location;
45+
}
46+
47+
public boolean isMarkDefault() {
48+
return markDefault;
49+
}
50+
51+
public void setMarkDefault(boolean markDefault) {
52+
this.markDefault = markDefault;
53+
}
54+
55+
public List<String> getExecutionEnvironments() {
56+
return executionEnvironments;
57+
}
58+
59+
public void setExecutionEnvironments(List<String> executionEnvironments) {
60+
this.executionEnvironments = executionEnvironments;
61+
}
62+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2016 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.gradle.oomph;
17+
18+
import java.util.ArrayList;
19+
import java.util.Collection;
20+
import java.util.List;
21+
22+
import com.diffplug.gradle.OrderingConstraints;
23+
24+
/** Used for adding JRE/JDK installations to an Eclipse install. */
25+
public class InstalledJreAdder extends SetupAction {
26+
private static final long serialVersionUID = -7101059764345094433L;
27+
28+
final List<InstalledJre> installedJres;
29+
30+
protected InstalledJreAdder(Collection<InstalledJre> jresToAdd) {
31+
super("com.diffplug.gradle.oomph.InstalledJreAdderInternal");
32+
installedJres = new ArrayList<>(jresToAdd);
33+
}
34+
35+
@Override
36+
protected void populateOrdering(OrderingConstraints<Class<? extends SetupAction>> ordering) {
37+
// we must add installed jre(s) before importing projects
38+
ordering.before(ProjectImporter.class);
39+
}
40+
41+
/**
42+
* @see com.diffplug.gradle.oomph.SetupAction#getDescription()
43+
*/
44+
@Override
45+
public String getDescription() {
46+
return "adding installed JRE's";
47+
}
48+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright 2016 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.gradle.oomph;
17+
18+
import java.io.File;
19+
import java.util.HashSet;
20+
import java.util.Set;
21+
22+
import org.eclipse.core.runtime.CoreException;
23+
import org.eclipse.core.runtime.IStatus;
24+
import org.eclipse.core.runtime.NullProgressMonitor;
25+
import org.eclipse.emf.ecore.util.EcoreUtil;
26+
import org.eclipse.jdt.launching.IVMInstall;
27+
import org.eclipse.jdt.launching.IVMInstallType;
28+
import org.eclipse.jdt.launching.JavaRuntime;
29+
import org.eclipse.jdt.launching.VMStandin;
30+
import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
31+
32+
import com.diffplug.gradle.oomph.SetupAction.Internal;
33+
34+
public class InstalledJreAdderInternal extends Internal<InstalledJreAdder> {
35+
36+
InstalledJreAdderInternal(InstalledJreAdder host) {
37+
super(host);
38+
}
39+
40+
@Override
41+
protected void runWithinEclipse() throws Throwable {
42+
IVMInstallType[] types = JavaRuntime.getVMInstallTypes();
43+
for (IVMInstallType type : types) {
44+
if ("org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType".equals(type.getId())) {
45+
for (InstalledJre jreToAdd : host.installedJres) {
46+
IVMInstall realVM = addInstalledJre(type, jreToAdd);
47+
if (jreToAdd.isMarkDefault()) {
48+
JavaRuntime.setDefaultVMInstall(realVM, new NullProgressMonitor());
49+
}
50+
linkWithExecutionEnvironments(realVM, jreToAdd);
51+
}
52+
}
53+
}
54+
}
55+
56+
protected IVMInstall addInstalledJre(IVMInstallType type, InstalledJre jreToAdd) throws Exception {
57+
IVMInstall retVal = findJre(jreToAdd.getVersion(), jreToAdd.getInstalledLocation());
58+
if (retVal == null) {
59+
IStatus validationStatus = type.validateInstallLocation(jreToAdd.getInstalledLocation());
60+
if (!validationStatus.isOK()) {
61+
throw new CoreException(validationStatus);
62+
}
63+
VMStandin vmStandin = new VMStandin(type, EcoreUtil.generateUUID());
64+
vmStandin.setInstallLocation(jreToAdd.getInstalledLocation());
65+
vmStandin.setName("JRE for " + jreToAdd.getVersion());
66+
IVMInstall realVM = vmStandin.convertToRealVM();
67+
retVal = realVM;
68+
}
69+
return retVal;
70+
}
71+
72+
protected void linkWithExecutionEnvironments(IVMInstall installedVm, InstalledJre jreToAdd) {
73+
if (jreToAdd.getExecutionEnvironments().isEmpty()) {
74+
return;
75+
} else {
76+
Set<String> execEnvsToAdd = new HashSet<>(jreToAdd.getExecutionEnvironments());
77+
IExecutionEnvironment[] executionEnvironments = JavaRuntime.getExecutionEnvironmentsManager()
78+
.getExecutionEnvironments();
79+
for (IExecutionEnvironment iExecutionEnvironment : executionEnvironments) {
80+
if (execEnvsToAdd.contains(iExecutionEnvironment.getId())) {
81+
iExecutionEnvironment.setDefaultVM(installedVm);
82+
}
83+
}
84+
}
85+
}
86+
87+
private IVMInstall findJre(String version, File location) throws Exception {
88+
for (IVMInstallType vmInstallType : JavaRuntime.getVMInstallTypes()) {
89+
for (IVMInstall vmInstall : vmInstallType.getVMInstalls()) {
90+
File installLocation = vmInstall.getInstallLocation();
91+
if (location.equals(installLocation)) {
92+
return vmInstall;
93+
}
94+
}
95+
}
96+
97+
return null;
98+
}
99+
}

src/main/java/com/diffplug/gradle/oomph/OomphConvention.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* Base class for implementing a DSL
2222
* around a specific part of the IDE.
2323
*/
24-
public class OomphConvention {
24+
public class OomphConvention implements AutoCloseable {
2525
protected final OomphIdeExtension extension;
2626

2727
OomphConvention(OomphIdeExtension extension) {
@@ -45,4 +45,14 @@ protected void setPerspectiveOver(String toSet, String... toTrump) {
4545
extension.perspective(toSet);
4646
}
4747
}
48+
49+
/**
50+
* This is called when the convention block ends.
51+
*
52+
* Usually it can just be empty, but if you've been accumulating
53+
* values, this is your chance to smush them down into
54+
* a setup action (see {@link ConventionJdt}.
55+
*/
56+
@Override
57+
public void close() {}
4858
}

src/main/java/com/diffplug/gradle/oomph/OomphIdeExtension.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -379,19 +379,22 @@ void ideClean() {
379379
/////////////////
380380
/** Convenience methods for setting the style. */
381381
public void style(Action<ConventionStyle> action) {
382-
ConventionStyle convention = new ConventionStyle(this);
383-
action.execute(convention);
382+
try (ConventionStyle convention = new ConventionStyle(this)) {
383+
action.execute(convention);
384+
}
384385
}
385386

386387
/** Adds the java development tools. */
387388
public void jdt(Action<ConventionJdt> action) {
388-
ConventionJdt convention = new ConventionJdt(this);
389-
action.execute(convention);
389+
try (ConventionJdt convention = new ConventionJdt(this)) {
390+
action.execute(convention);
391+
}
390392
}
391393

392394
/** Adds the plugin-development environment, @see ConventionPde. */
393395
public void pde(Action<ConventionPde> action) {
394-
ConventionPde convention = new ConventionPde(this);
395-
action.execute(convention);
396+
try (ConventionPde convention = new ConventionPde(this)) {
397+
action.execute(convention);
398+
}
396399
}
397400
}

0 commit comments

Comments
 (0)