You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by "gnodet (via GitHub)" <gi...@apache.org> on 2023/01/30 20:09:47 UTC

[GitHub] [maven-mvnd] gnodet commented on a diff in pull request #784: Make Classworld setup more alike to vanilla Maven

gnodet commented on code in PR #784:
URL: https://github.com/apache/maven-mvnd/pull/784#discussion_r1091091086


##########
common/src/main/java/org/mvndaemon/mvnd/common/MavenDaemon.java:
##########
@@ -18,67 +18,39 @@
  */
 package org.mvndaemon.mvnd.common;
 
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.net.URLClassLoader;
+import java.io.InputStream;
+import java.lang.reflect.Constructor;
 import java.nio.file.Files;
 import java.nio.file.Path;
-import java.util.stream.Stream;
+
+import org.codehaus.plexus.classworlds.ClassWorld;
+import org.codehaus.plexus.classworlds.launcher.Launcher;
+import org.codehaus.plexus.classworlds.realm.ClassRealm;
 
 public class MavenDaemon {
 
     public static void main(String[] args) throws Exception {
-        final Path mvndHome = Environment.MVND_HOME.asPath();
-        URL[] classpath = Stream.concat(
-                        /* jars */
-                        Stream.of("lib/ext", "lib", "boot")
-                                .map(mvndHome::resolve)
-                                .flatMap((Path p) -> {
-                                    try {
-                                        return Files.list(p);
-                                    } catch (java.io.IOException e) {
-                                        throw new RuntimeException("Could not list " + p, e);
-                                    }
-                                })
-                                .filter(p -> {
-                                    final String fileName = p.getFileName().toString();
-                                    return fileName.endsWith(".jar") && !fileName.startsWith("mvnd-client-");
-                                })
-                                .filter(Files::isRegularFile),
-                        /* resources */
-                        Stream.of(mvndHome.resolve("conf"), mvndHome.resolve("conf/logging")))
-                .map(Path::normalize)
-                .map(Path::toUri)
-                .map(uri -> {
-                    try {
-                        return uri.toURL();
-                    } catch (MalformedURLException e) {
-                        throw new RuntimeException(e);
-                    }
-                })
-                .toArray(URL[]::new);
-        ClassLoader loader = new URLClassLoader(classpath, null) {
+        Path mvndHome = Environment.MVND_HOME.asPath();
+        Launcher launcher = new Launcher();
+        launcher.setSystemClassLoader(new ClassLoader(ClassLoader.getPlatformClassLoader()) {
+
             @Override
             protected Class<?> findClass(String name) throws ClassNotFoundException {
-                try {
-                    return super.findClass(name);
-                } catch (ClassNotFoundException e) {
+                if (name.startsWith("org.codehaus.plexus.classworlds.")
+                        || name.startsWith("org.codehaus.classworlds.")) {
                     return MavenDaemon.class.getClassLoader().loadClass(name);
                 }
+                throw new ClassNotFoundException(name);
             }
-
-            @Override
-            public URL getResource(String name) {
-                URL url = super.getResource(name);
-                if (url == null) {
-                    url = MavenDaemon.class.getClassLoader().getResource(name);
-                }
-                return url;
-            }
-        };
-        Thread.currentThread().setContextClassLoader(loader);
-        Class<?> clazz = loader.loadClass("org.mvndaemon.mvnd.daemon.Server");
-        try (AutoCloseable server = (AutoCloseable) clazz.getConstructor().newInstance()) {
+        });
+        try (InputStream in = Files.newInputStream(mvndHome.resolve("bin/m2.conf"))) {

Review Comment:
   That looks wrong.  We should have a custom config that properly boots the `org.mvndaemon.mvnd.daemon.Server`.



##########
client/src/main/java/org/mvndaemon/mvnd/client/DaemonConnector.java:
##########
@@ -339,22 +339,34 @@ 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/ext"))) {

Review Comment:
   The implementation of `resolve` implies the usage of the file separator, which is OS specific.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org