Skip to content

Commit

Permalink
Make Classworld setup more alike to vanilla Maven
Browse files Browse the repository at this point in the history
Use the plexus Launcher to start the daemon server, just like we would
launch a normal Maven process.

This improves compatbility with any extensions or plugins that assume that
their ClassLoader is a ClassRealm.
  • Loading branch information
oehme committed Feb 6, 2023
1 parent 3508f0e commit 1678fcc
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 493 deletions.
45 changes: 26 additions & 19 deletions client/src/main/java/org/mvndaemon/mvnd/client/DaemonConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package org.mvndaemon.mvnd.client;

import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
Expand Down Expand Up @@ -52,7 +51,6 @@
import org.mvndaemon.mvnd.common.DaemonState;
import org.mvndaemon.mvnd.common.DaemonStopEvent;
import org.mvndaemon.mvnd.common.Environment;
import org.mvndaemon.mvnd.common.MavenDaemon;
import org.mvndaemon.mvnd.common.Message;
import org.mvndaemon.mvnd.common.Os;
import org.mvndaemon.mvnd.common.SocketFamily;
Expand Down Expand Up @@ -339,33 +337,43 @@ private Process startDaemonProcess(String daemonId, ClientOutput output) {
final Path mvndHome = parameters.mvndHome();
final Path workingDir = parameters.userDir();
String command = "";
try (DirectoryStream<Path> jarPaths =
Files.newDirectoryStream(mvndHome.resolve("lib").resolve("ext"))) {
try {
List<String> args = new ArrayList<>();
// executable
final String java = Os.current().isUnixLike() ? "bin/java" : "bin\\java.exe";
args.add(parameters.javaHome().resolve(java).toString());
// classpath
String mvndCommonPath = null;
String mvndAgentPath = null;
for (Path jar : jarPaths) {
String s = jar.getFileName().toString();
if (s.endsWith(".jar")) {
if (s.startsWith("mvnd-common-")) {
mvndCommonPath = jar.toString();
} else if (s.startsWith("mvnd-agent-")) {
mvndAgentPath = jar.toString();
String plexusClassworldsPath = null;
try (DirectoryStream<Path> jarPaths =
Files.newDirectoryStream(mvndHome.resolve("lib").resolve("ext"))) {
for (Path jar : jarPaths) {
String s = jar.getFileName().toString();
if (s.endsWith(".jar")) {
if (s.startsWith("mvnd-agent-")) {
mvndAgentPath = jar.toString();
}
}
}
}
if (mvndCommonPath == null) {
throw new IllegalStateException("Could not find mvnd-common jar in lib/");
try (DirectoryStream<Path> jarPaths = Files.newDirectoryStream(mvndHome.resolve("boot"))) {
for (Path jar : jarPaths) {
String s = jar.getFileName().toString();
if (s.endsWith(".jar")) {
if (s.startsWith("plexus-classworlds-")) {
plexusClassworldsPath = jar.toString();
}
}
}
}
if (mvndAgentPath == null) {
throw new IllegalStateException("Could not find mvnd-agent jar in lib/");
throw new IllegalStateException("Could not find mvnd-agent jar in lib/ext/");
}
if (plexusClassworldsPath == null) {
throw new IllegalStateException("Could not find plexus-classworlds jar in boot/");
}
args.add("-classpath");
args.add(mvndCommonPath + File.pathSeparator + mvndAgentPath);
args.add(plexusClassworldsPath);
args.add("-javaagent:" + mvndAgentPath);
// debug options
if (parameters.property(Environment.MVND_DEBUG).asBoolean()) {
Expand Down Expand Up @@ -420,8 +428,7 @@ private Process startDaemonProcess(String daemonId, ClientOutput output) {
}

Environment.MVND_HOME.addSystemProperty(args, mvndHome.toString());
args.add("-Dmaven.home=" + mvndHome);
args.add("-Dmaven.conf=" + mvndHome.resolve("conf"));
args.add("-Dclassworlds.conf=" + mvndHome.resolve("bin").resolve("mvnd-server.conf"));

Environment.MVND_JAVA_HOME.addSystemProperty(
args, parameters.javaHome().toString());
Expand All @@ -439,7 +446,7 @@ private Process startDaemonProcess(String daemonId, ClientOutput output) {
.orElseGet(() -> getJavaVersion() >= 16.0f ? SocketFamily.unix : SocketFamily.inet)
.toString());
parameters.discriminatingSystemProperties(args);
args.add(MavenDaemon.class.getName());
args.add("org.codehaus.plexus.classworlds.launcher.Launcher");
command = String.join(" ", args);

LOGGER.debug(
Expand Down
85 changes: 0 additions & 85 deletions common/src/main/java/org/mvndaemon/mvnd/common/MavenDaemon.java

This file was deleted.

Loading

0 comments on commit 1678fcc

Please sign in to comment.