Don't jarhell check system jars
This commit is contained in:
parent
b55a3f32b8
commit
30f267f758
|
@ -46,6 +46,11 @@ import java.util.jar.Manifest;
|
||||||
/** Simple check for duplicate class files across the classpath */
|
/** Simple check for duplicate class files across the classpath */
|
||||||
public class JarHell {
|
public class JarHell {
|
||||||
|
|
||||||
|
/** Simple driver class, can be used eg. from builds. Returns non-zero on jar-hell */
|
||||||
|
public static void main(String args[]) throws Exception {
|
||||||
|
checkJarHell();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks the current classloader for duplicate classes
|
* Checks the current classloader for duplicate classes
|
||||||
* @throws IllegalStateException if jar hell was found
|
* @throws IllegalStateException if jar hell was found
|
||||||
|
@ -57,9 +62,9 @@ public class JarHell {
|
||||||
}
|
}
|
||||||
ESLogger logger = Loggers.getLogger(JarHell.class);
|
ESLogger logger = Loggers.getLogger(JarHell.class);
|
||||||
if (logger.isDebugEnabled()) {
|
if (logger.isDebugEnabled()) {
|
||||||
logger.debug("java.class.path= {}" + System.getProperty("java.class.path"));
|
logger.debug("java.class.path: {}", System.getProperty("java.class.path"));
|
||||||
logger.debug("sun.boot.class.path= {}" + System.getProperty("sun.boot.class.path"));
|
logger.debug("sun.boot.class.path: {}", System.getProperty("sun.boot.class.path"));
|
||||||
logger.debug("classloader urls= {}" + Arrays.toString(((URLClassLoader)loader).getURLs()));
|
logger.debug("classloader urls: {}", Arrays.toString(((URLClassLoader)loader).getURLs()));
|
||||||
}
|
}
|
||||||
checkJarHell(((URLClassLoader)loader).getURLs());
|
checkJarHell(((URLClassLoader)loader).getURLs());
|
||||||
}
|
}
|
||||||
|
@ -70,14 +75,26 @@ public class JarHell {
|
||||||
*/
|
*/
|
||||||
@SuppressForbidden(reason = "needs JarFile for speed, just reading entries")
|
@SuppressForbidden(reason = "needs JarFile for speed, just reading entries")
|
||||||
public static void checkJarHell(URL urls[]) throws Exception {
|
public static void checkJarHell(URL urls[]) throws Exception {
|
||||||
|
ESLogger logger = Loggers.getLogger(JarHell.class);
|
||||||
|
// we don't try to be sneaky and use deprecated/internal/not portable stuff
|
||||||
|
// like sun.boot.class.path, and with jigsaw we don't yet have a way to get
|
||||||
|
// a "list" at all. So just exclude any elements underneath the java home
|
||||||
|
String javaHome = System.getProperty("java.home");
|
||||||
final Map<String,URL> clazzes = new HashMap<>(32768);
|
final Map<String,URL> clazzes = new HashMap<>(32768);
|
||||||
Set<String> seenJars = new HashSet<>();
|
Set<String> seenJars = new HashSet<>();
|
||||||
for (final URL url : urls) {
|
for (final URL url : urls) {
|
||||||
String path = URLDecoder.decode(url.getPath(), "UTF-8");
|
String path = URLDecoder.decode(url.getPath(), "UTF-8");
|
||||||
|
// exclude system resources
|
||||||
|
if (path.startsWith(javaHome)) {
|
||||||
|
logger.debug("excluding system resource: {}", path);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if (path.endsWith(".jar")) {
|
if (path.endsWith(".jar")) {
|
||||||
if (!seenJars.add(path)) {
|
if (!seenJars.add(path)) {
|
||||||
|
logger.debug("excluding duplicate classpath element: {}", path);
|
||||||
continue; // we can't fail because of sheistiness with joda-time
|
continue; // we can't fail because of sheistiness with joda-time
|
||||||
}
|
}
|
||||||
|
logger.debug("examining jar: {}", path);
|
||||||
try (JarFile file = new JarFile(path)) {
|
try (JarFile file = new JarFile(path)) {
|
||||||
Manifest manifest = file.getManifest();
|
Manifest manifest = file.getManifest();
|
||||||
if (manifest != null) {
|
if (manifest != null) {
|
||||||
|
@ -111,6 +128,7 @@ public class JarHell {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
logger.debug("examining directory: {}", path);
|
||||||
// case for tests: where we have class files in the classpath
|
// case for tests: where we have class files in the classpath
|
||||||
final Path root = PathUtils.get(url.toURI());
|
final Path root = PathUtils.get(url.toURI());
|
||||||
final String sep = root.getFileSystem().getSeparator();
|
final String sep = root.getFileSystem().getSeparator();
|
||||||
|
|
Loading…
Reference in New Issue