Skip to content

Commit def5f24

Browse files
committed
Allow running inside Maven unit tests
When Maven's surefire plugin runs unit tests, the URLClassLoader does actually not list the URLs corresponding to the dependencies. Instead, surefire tries to be clever and hides those dependencies inside a single, empty .jar file's manifest's Class-Path: entry. We are not dumb, so surefire loses: we get the URLs anyway. Hah! Take this, surefire! *shakes-fist* Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent b40b8dc commit def5f24

File tree

1 file changed

+42
-4
lines changed

1 file changed

+42
-4
lines changed

src/main/java/org/scijava/plugins/scripting/java/JavaEngine.java

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,14 @@
4343
import java.io.Reader;
4444
import java.io.StringReader;
4545
import java.io.Writer;
46+
import java.net.MalformedURLException;
4647
import java.net.URL;
4748
import java.net.URLClassLoader;
4849
import java.util.ArrayList;
4950
import java.util.List;
51+
import java.util.jar.JarFile;
52+
import java.util.jar.Manifest;
53+
import java.util.jar.Attributes.Name;
5054
import java.util.regex.Matcher;
5155
import java.util.regex.Pattern;
5256

@@ -463,15 +467,49 @@ private static List<Coordinate> getAllDependencies(final BuildEnvironment env) {
463467
for (final URL url : ((URLClassLoader)loader).getURLs()) {
464468
if (url.getProtocol().equals("file")) {
465469
final File file = new File(url.getPath());
466-
final String artifactId = fakeArtifactId(env, file.getName());
467-
Coordinate dependency = new Coordinate(DEFAULT_GROUP_ID, artifactId, "1.0.0");
468-
env.fakePOM(file, dependency);
469-
result.add(dependency);
470+
if (url.toString().matches(".*/target/surefire/surefirebooter[0-9]*\\.jar")) {
471+
getSurefireBooterURLs(file, url, env, result);
472+
continue;
473+
}
474+
result.add(fakeDependency(env, file));
470475
}
471476
}
472477
}
473478
}
474479
return result;
475480
}
476481

482+
private static Coordinate fakeDependency(final BuildEnvironment env, final File file) {
483+
final String artifactId = fakeArtifactId(env, file.getName());
484+
Coordinate dependency = new Coordinate(DEFAULT_GROUP_ID, artifactId, "1.0.0");
485+
env.fakePOM(file, dependency);
486+
return dependency;
487+
}
488+
489+
private static void getSurefireBooterURLs(final File file, final URL baseURL,
490+
final BuildEnvironment env, final List<Coordinate> result)
491+
{
492+
try {
493+
final JarFile jar = new JarFile(file);
494+
Manifest manifest = jar.getManifest();
495+
if (manifest != null) {
496+
final String classPath =
497+
manifest.getMainAttributes().getValue(Name.CLASS_PATH);
498+
if (classPath != null) {
499+
for (final String element : classPath.split(" +"))
500+
try {
501+
final File dependency = new File(new URL(baseURL, element).getPath());
502+
result.add(fakeDependency(env, dependency));
503+
}
504+
catch (MalformedURLException e) {
505+
e.printStackTrace();
506+
}
507+
}
508+
}
509+
}
510+
catch (final IOException e) {
511+
e.printStackTrace();
512+
}
513+
}
514+
477515
}

0 commit comments

Comments
 (0)