in arthur-maven-plugin/src/main/java/org/apache/geronimo/arthur/maven/mojo/JibMojo.java [360:432]
private String addLddLibsAndFindLdLinux(final Map<String, String> env, final List<FileEntriesLayer> layers) throws IOException {
getLog().info("Running ldd on " + binarySource.getName());
final Process ldd = new ProcessBuilder("ldd", binarySource.getAbsolutePath()).start();
try {
final int status = ldd.waitFor();
if (status != 0) {
throw new IllegalArgumentException("LDD failed with status " + status + ": " + slurp(ldd.getErrorStream()));
}
} catch (final InterruptedException e) {
throw new IllegalStateException(e);
}
final Collection<Path> files;
try (final BufferedReader reader = new BufferedReader(new InputStreamReader(ldd.getInputStream(), StandardCharsets.UTF_8))) {
files = reader.lines()
.filter(it -> it.contains("/"))
.map(it -> {
final int start = it.indexOf('/');
int end = it.indexOf(' ', start);
if (end < 0) {
end = it.indexOf('(', start);
if (end < 0) {
end = it.length();
}
}
return it.substring(start, end);
})
.map(Paths::get)
.filter(Files::exists)
.collect(toList());
} catch (final IOException e) {
throw new IllegalStateException(e);
}
String ldLinux = null;
if (!files.isEmpty()) {
final FileEntriesLayer.Builder libraries = FileEntriesLayer.builder()
.setName("Libraries");
// copy libs + tries to extract ld-linux-x86-64.so.2 if present
ldLinux = files.stream()
.map(file -> {
final String fileName = file.getFileName().toString();
try {
libraries.addEntry(
file, AbsoluteUnixPath.get(nativeRootDir).resolve(fileName),
FilePermissions.fromPosixFilePermissions(Files.getPosixFilePermissions(file)), getTimestamp(file));
} catch (final IOException e) {
throw new IllegalStateException(e);
}
return fileName;
})
.filter(it -> it.startsWith("ld-linux"))
.min((a, b) -> { // best is "ld-linux-x86-64.so.2"
if ("ld-linux-x86-64.so.2".equals(a)) {
return -1;
}
if ("ld-linux-x86-64.so.2".equals(b)) {
return 1;
}
if (a.endsWith(".so.2")) {
return -1;
}
if (b.endsWith(".so.2")) {
return -1;
}
return a.compareTo(b);
})
.map(it -> AbsoluteUnixPath.get(nativeRootDir).resolve(it).toString()) // make it absolute since it will be added to the entrypoint
.orElse(null);
layers.add(libraries.build());
env.putIfAbsent("LD_LIBRARY_PATH", AbsoluteUnixPath.get(nativeRootDir).toString());
}
return ldLinux;
}