3131
3232package org .scijava .plugins .scripting .java ;
3333
34+ import io .github .classgraph .ClassGraph ;
35+ import org .scijava .command .CommandService ;
36+ import org .scijava .minimaven .BuildEnvironment ;
37+ import org .scijava .minimaven .Coordinate ;
38+ import org .scijava .minimaven .MavenProject ;
39+ import org .scijava .plugin .Parameter ;
40+ import org .scijava .plugin .PluginService ;
41+ import org .scijava .run .RunService ;
42+ import org .scijava .script .AbstractScriptEngine ;
43+ import org .scijava .util .FileUtils ;
44+ import org .scijava .util .LineOutputStream ;
45+ import org .w3c .dom .Document ;
46+ import org .w3c .dom .Element ;
47+ import org .xml .sax .SAXException ;
48+
49+ import javax .script .ScriptEngine ;
50+ import javax .script .ScriptException ;
51+ import javax .xml .parsers .DocumentBuilderFactory ;
52+ import javax .xml .parsers .ParserConfigurationException ;
53+ import javax .xml .transform .OutputKeys ;
54+ import javax .xml .transform .Transformer ;
55+ import javax .xml .transform .TransformerConfigurationException ;
56+ import javax .xml .transform .TransformerException ;
57+ import javax .xml .transform .TransformerFactory ;
58+ import javax .xml .transform .TransformerFactoryConfigurationError ;
59+ import javax .xml .transform .dom .DOMSource ;
60+ import javax .xml .transform .stream .StreamResult ;
3461import java .io .BufferedReader ;
3562import java .io .ByteArrayInputStream ;
3663import java .io .ByteArrayOutputStream ;
4370import java .io .Reader ;
4471import java .io .StringReader ;
4572import java .io .Writer ;
46- import java .net .MalformedURLException ;
4773import java .net .URL ;
4874import java .net .URLClassLoader ;
4975import java .util .ArrayList ;
5076import java .util .List ;
51- import java .util .jar .Attributes .Name ;
52- import java .util .jar .JarFile ;
53- import java .util .jar .Manifest ;
5477import java .util .regex .Matcher ;
5578import java .util .regex .Pattern ;
5679
57- import javax .script .ScriptEngine ;
58- import javax .script .ScriptException ;
59- import javax .xml .parsers .DocumentBuilderFactory ;
60- import javax .xml .parsers .ParserConfigurationException ;
61- import javax .xml .transform .OutputKeys ;
62- import javax .xml .transform .Transformer ;
63- import javax .xml .transform .TransformerConfigurationException ;
64- import javax .xml .transform .TransformerException ;
65- import javax .xml .transform .TransformerFactory ;
66- import javax .xml .transform .TransformerFactoryConfigurationError ;
67- import javax .xml .transform .dom .DOMSource ;
68- import javax .xml .transform .stream .StreamResult ;
69-
70- import org .scijava .command .CommandService ;
71- import org .scijava .minimaven .BuildEnvironment ;
72- import org .scijava .minimaven .Coordinate ;
73- import org .scijava .minimaven .MavenProject ;
74- import org .scijava .plugin .Parameter ;
75- import org .scijava .plugin .PluginService ;
76- import org .scijava .run .RunService ;
77- import org .scijava .script .AbstractScriptEngine ;
78- import org .scijava .util .FileUtils ;
79- import org .scijava .util .LineOutputStream ;
80- import org .w3c .dom .Document ;
81- import org .w3c .dom .Element ;
82- import org .xml .sax .SAXException ;
83-
8480/**
8581 * A pseudo-{@link ScriptEngine} compiling and executing Java classes.
8682 * <p>
@@ -787,23 +783,14 @@ private static Element append(final Document document, final Element parent,
787783 getAllDependencies (final BuildEnvironment env )
788784 {
789785 final List <Coordinate > result = new ArrayList <Coordinate >();
790- for (ClassLoader loader = Thread .currentThread ().getContextClassLoader (); loader != null ; loader =
791- loader .getParent ())
792- {
793- if (loader instanceof URLClassLoader ) {
794- for (final URL url : ((URLClassLoader ) loader ).getURLs ()) {
795- if (url .getProtocol ().equals ("file" )) {
796- final File file = new File (url .getPath ());
797- if (url .toString ().matches (
798- ".*/target/surefire/surefirebooter[0-9]*\\ .jar" ))
799- {
800- getSurefireBooterURLs (file , url , env , result );
801- continue ;
802- }
803- result .add (fakeDependency (env , file ));
804- }
805- }
806- }
786+ ClassGraph cg = new ClassGraph ();
787+ String cp = cg .getClasspath ();
788+ String [] candidates = cp .split (File .pathSeparator );
789+
790+ for ( String candidate : candidates ){
791+ File file = new File (candidate );
792+ Coordinate c = fakeDependency (env , file );
793+ result .add (c );
807794 }
808795 return result ;
809796 }
@@ -830,56 +817,6 @@ private static Coordinate fakeDependency(final BuildEnvironment env,
830817 return dependency ;
831818 }
832819
833- /**
834- * Figures out the class path given a {@code .jar} file generated by the
835- * {@code maven-surefire-plugin}.
836- * <p>
837- * A little-known feature of JAR files is that their manifest can specify
838- * additional class path elements in a {@code Class-Path} entry. The
839- * {@code maven-surefire-plugin} makes extensive use of that: the URLs of the
840- * of the active {@link URLClassLoader} will consist of only a single
841- * {@code .jar} file that is empty except for a manifest whose sole purpose is
842- * to specify the dependencies.
843- * </p>
844- * <p>
845- * This method can be used to discover those additional class path elements.
846- * </p>
847- *
848- * @param file the {@code .jar} file generated by the
849- * {@code maven-surefire-plugin}
850- * @param baseURL the {@link URL} of the {@code .jar} file, needed for class
851- * path elements specified as relative paths
852- * @param env the {@link BuildEnvironment}, to store the Maven POMs faked for
853- * the class path elements
854- * @param result the list of dependencies to which the discovered dependencies
855- * are added
856- */
857- private static void getSurefireBooterURLs (final File file , final URL baseURL ,
858- final BuildEnvironment env , final List <Coordinate > result )
859- {
860- try {
861- final JarFile jar = new JarFile (file );
862- Manifest manifest = jar .getManifest ();
863- if (manifest != null ) {
864- final String classPath =
865- manifest .getMainAttributes ().getValue (Name .CLASS_PATH );
866- if (classPath != null ) {
867- for (final String element : classPath .split (" +" ))
868- try {
869- final File dependency =
870- new File (new URL (baseURL , element ).getPath ());
871- result .add (fakeDependency (env , dependency ));
872- }
873- catch (MalformedURLException e ) {
874- e .printStackTrace ();
875- }
876- }
877- }
878- }
879- catch (final IOException e ) {
880- e .printStackTrace ();
881- }
882- }
883820
884821 /**
885822 * Read complete contents of a Reader and return as String.
0 commit comments