Skip to content

Commit

Permalink
Set Toolchan JDKs as JDT JVM installations
Browse files Browse the repository at this point in the history
Currently only the default java is used but this can cause issues if a
more specific jvm is required.

This now reads all jdk toolchains from the maven config and configure
those as JVMs in JDT.
  • Loading branch information
laeubi committed Jan 28, 2025
1 parent e6a9aa3 commit ad84679
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public record Bundles(Set<String> bundles) {
public static final String BUNDLE_ECLIPSE_HELP_BASE = "org.eclipse.help.base";
public static final String BUNDLE_PDE_CORE = "org.eclipse.pde.core";
public static final String BUNDLE_JDT_CORE = "org.eclipse.jdt.core";
public static final String BUNDLE_JDT_LAUNCHING = "org.eclipse.jdt.launching";

static final String BUNDLE_LAUNCHING_MACOS = "org.eclipse.jdt.launching.macosx";
static final String BUNDLE_APP = "org.eclipse.equinox.app";
Expand Down
11 changes: 11 additions & 0 deletions tycho-eclipse-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.eclipse.jdt</groupId>
<artifactId>org.eclipse.jdt.launching</artifactId>
<version>3.23.100</version>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.eclipse.tycho</groupId>
<artifactId>sisu-equinox-launching</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Repository;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
Expand All @@ -33,6 +36,8 @@
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.toolchain.Toolchain;
import org.apache.maven.toolchain.ToolchainManager;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.tycho.ArtifactKey;
Expand Down Expand Up @@ -102,6 +107,9 @@ public abstract class AbstractEclipseBuildMojo<Result extends EclipseBuildResult
@Parameter(property = "project", readonly = true)
protected MavenProject project;

@Component
protected MavenSession mavenSession;

@Component
private EclipseWorkspaceManager workspaceManager;

Expand All @@ -114,6 +122,11 @@ public abstract class AbstractEclipseBuildMojo<Result extends EclipseBuildResult
@Component
private TychoProjectManager projectManager;

@Component
ToolchainManager toolchainManager;



@Override
public final void execute() throws MojoExecutionException, MojoFailureException {
Optional<EclipseProject> eclipseProjectValue = projectManager.getEclipseProject(project);
Expand Down Expand Up @@ -169,6 +182,23 @@ public void run() {
thread.start();
framework.waitForApplicationStart(TimeUnit.SECONDS.toMillis(30));
}
if (hasJDTNature(eclipseProject)) {
if (framework.hasBundle(Bundles.BUNDLE_JDT_LAUNCHING)) {
List<Path> jvms = new ArrayList<>();
for (Toolchain toolchain : toolchainManager.getToolchains(mavenSession, "jdk", Map.of())) {
String tool = toolchain.findTool("java");
if (tool != null) {
jvms.add(Path.of(tool).getParent().getParent());
}
}
framework.execute(new SetJVMs(jvms, debug));
} else {
getLog().info(
"Skip set JVMs because " + Bundles.BUNDLE_JDT_LAUNCHING
+ " is not part of the framework...");
}
}

if (hasPDENature(eclipseProject)) {
if (framework.hasBundle(Bundles.BUNDLE_PDE_CORE)) {
framework.execute(new SetTargetPlatform(projectDependencies, debug));
Expand All @@ -182,7 +212,7 @@ public void run() {
if (hasBaselinesSet()) {
framework.execute(new SetApiBaseline(project.getId(), getBaselineBundles(), debug));
} else {
getLog().info("Skip set ApiBasline because no baselines set...");
getLog().info("Skip set ApiBaseline because no baselines set...");
}
} else {
getLog().info("Skip set ApiBasline because " + Bundles.BUNDLE_API_TOOLS
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*******************************************************************************
* Copyright (c) 2025 Christoph Läubrich and others.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Christoph Läubrich - initial API and implementation
*******************************************************************************/
package org.eclipse.tycho.eclipsebuild;

import java.io.File;
import java.io.Serializable;
import java.nio.file.Path;
import java.util.Collection;
import java.util.concurrent.Callable;

import org.eclipse.jdt.internal.launching.StandardVMType;
import org.eclipse.jdt.launching.IVMInstall;
import org.eclipse.jdt.launching.IVMInstall2;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.jdt.launching.VMStandin;

public class SetJVMs implements Callable<Serializable>, Serializable {

private static final long serialVersionUID = 1L;
private boolean debug;
private Collection<String> jvms;

public SetJVMs(Collection<Path> jvms, boolean debug) {
this.debug = debug;
this.jvms = jvms.stream().map(EclipseProjectBuild::pathAsString).toList();
}

@Override
public Serializable call() throws Exception {
StandardVMType standardType = (StandardVMType) JavaRuntime.getVMInstallType(StandardVMType.ID_STANDARD_VM_TYPE);
for (String entry : jvms) {
debug("Adding JVM " + entry + "...");
VMStandin workingCopy = new VMStandin(standardType, entry);
workingCopy.setInstallLocation(new File(entry));
workingCopy.setName(entry);
IVMInstall install = workingCopy.convertToRealVM();
if (!isValid(install)) {
standardType.disposeVMInstall(install.getId());
}
}
return null;
}

private static boolean isValid(IVMInstall install) {
return install instanceof IVMInstall2 vm && vm.getJavaVersion() != null;
}

private void debug(String string) {
if (debug) {
System.out.println(string);
}
}

}

0 comments on commit ad84679

Please sign in to comment.