31
31
32
32
package org .scijava .plugins .scripting .java ;
33
33
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 ;
34
61
import java .io .BufferedReader ;
35
62
import java .io .ByteArrayInputStream ;
36
63
import java .io .ByteArrayOutputStream ;
43
70
import java .io .Reader ;
44
71
import java .io .StringReader ;
45
72
import java .io .Writer ;
46
- import java .net .MalformedURLException ;
47
73
import java .net .URL ;
48
74
import java .net .URLClassLoader ;
49
75
import java .util .ArrayList ;
50
76
import java .util .List ;
51
- import java .util .jar .Attributes .Name ;
52
- import java .util .jar .JarFile ;
53
- import java .util .jar .Manifest ;
54
77
import java .util .regex .Matcher ;
55
78
import java .util .regex .Pattern ;
56
79
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
-
84
80
/**
85
81
* A pseudo-{@link ScriptEngine} compiling and executing Java classes.
86
82
* <p>
@@ -787,23 +783,14 @@ private static Element append(final Document document, final Element parent,
787
783
getAllDependencies (final BuildEnvironment env )
788
784
{
789
785
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 );
807
794
}
808
795
return result ;
809
796
}
@@ -830,56 +817,6 @@ private static Coordinate fakeDependency(final BuildEnvironment env,
830
817
return dependency ;
831
818
}
832
819
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
- }
883
820
884
821
/**
885
822
* Read complete contents of a Reader and return as String.
0 commit comments