Use relative paths in classpath to shorten execution line on large apps

This commit is contained in:
Benjamin König
2022-02-18 14:28:29 +01:00
parent bb4b9e3390
commit 9a99f4e27d
4 changed files with 13 additions and 20 deletions
@@ -1009,7 +1009,7 @@ public class Application
ClassPath classPath = PathBuilder.buildClassPath(this); ClassPath classPath = PathBuilder.buildClassPath(this);
if (!dashJarMode) { if (!dashJarMode) {
args.add("-classpath"); args.add("-classpath");
args.add(classPath.asArgumentString()); args.add(classPath.asArgumentString(getAppDir()));
} }
// we love our Mac users, so we do nice things to preserve our application identity // we love our Mac users, so we do nice things to preserve our application identity
@@ -1028,7 +1028,7 @@ public class Application
// @TODO optional getdown.txt parameter to set addCurrentLibraryPath to true or false? // @TODO optional getdown.txt parameter to set addCurrentLibraryPath to true or false?
ClassPath javaLibPath = PathBuilder.buildLibsPath(this, true); ClassPath javaLibPath = PathBuilder.buildLibsPath(this, true);
if (javaLibPath != null) { if (javaLibPath != null) {
args.add("-Djava.library.path=" + javaLibPath.asArgumentString()); args.add("-Djava.library.path=" + javaLibPath.asArgumentString(getAppDir()));
} }
// pass along any pass-through arguments // pass along any pass-through arguments
@@ -1060,7 +1060,7 @@ public class Application
// if we're in -jar mode add those arguments, otherwise add the app class name // if we're in -jar mode add those arguments, otherwise add the app class name
if (dashJarMode) { if (dashJarMode) {
args.add("-jar"); args.add("-jar");
args.add(classPath.asArgumentString()); args.add(classPath.asArgumentString(getAppDir()));
} else { } else {
args.add(_class); args.add(_class);
} }
@@ -31,13 +31,17 @@ public class ClassPath
* <pre> * <pre>
* /path/to/a.jar:/path/to/b.jar * /path/to/a.jar:/path/to/b.jar
* </pre> * </pre>
*
* The paths are relativized to dir.
*
* @param dir the working directory of the process
*/ */
public String asArgumentString () public String asArgumentString (File dir)
{ {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
String delimiter = ""; String delimiter = "";
for (File entry: _classPathEntries) { for (File entry: _classPathEntries) {
builder.append(delimiter).append(entry.getAbsolutePath()); builder.append(delimiter).append(dir.toPath().relativize(entry.toPath()));
delimiter = File.pathSeparator; delimiter = File.pathSeparator;
} }
return builder.toString(); return builder.toString();
@@ -36,8 +36,8 @@ public class ClassPathTest
@Test public void shouldCreateValidArgumentString () @Test public void shouldCreateValidArgumentString ()
{ {
assertEquals( assertEquals(
_firstJar.getAbsolutePath() + File.pathSeparator + _secondJar.getAbsolutePath(), "a.jar:b.jar",
_classPath.asArgumentString()); _classPath.asArgumentString(_folder.getRoot()));
} }
@Test public void shouldProvideJarUrls () throws MalformedURLException, URISyntaxException @Test public void shouldProvideJarUrls () throws MalformedURLException, URISyntaxException
@@ -36,9 +36,7 @@ public class PathBuilderTest
@Test public void shouldBuildDefaultClassPath () throws IOException @Test public void shouldBuildDefaultClassPath () throws IOException
{ {
ClassPath classPath = PathBuilder.buildDefaultClassPath(_application); ClassPath classPath = PathBuilder.buildDefaultClassPath(_application);
String expectedClassPath = _firstJarFile.getAbsolutePath() + File.pathSeparator + assertEquals("a.jar:b.jar", classPath.asArgumentString(_appdir.getRoot()));
_secondJarFile.getAbsolutePath();
assertEquals(expectedClassPath, classPath.asArgumentString());
} }
@Test public void shouldBuildCachedClassPath () throws IOException @Test public void shouldBuildCachedClassPath () throws IOException
@@ -47,17 +45,8 @@ public class PathBuilderTest
when(_application.getDigest(_secondJar)).thenReturn("second"); when(_application.getDigest(_secondJar)).thenReturn("second");
when(_application.getCodeCacheRetentionDays()).thenReturn(1); when(_application.getCodeCacheRetentionDays()).thenReturn(1);
Path firstCachedJarFile = _appdir.getRoot().toPath().
resolve(PathBuilder.CODE_CACHE_DIR).resolve("fi").resolve("first.jar");
Path secondCachedJarFile = _appdir.getRoot().toPath().
resolve(PathBuilder.CODE_CACHE_DIR).resolve("se").resolve("second.jar");
String expectedClassPath = firstCachedJarFile.toAbsolutePath() + File.pathSeparator +
secondCachedJarFile.toAbsolutePath();
ClassPath classPath = PathBuilder.buildCachedClassPath(_application); ClassPath classPath = PathBuilder.buildCachedClassPath(_application);
assertEquals(expectedClassPath, classPath.asArgumentString()); assertEquals(".cache/fi/first.jar:.cache/se/second.jar", classPath.asArgumentString(_appdir.getRoot()));
} }
@Mock protected Application _application; @Mock protected Application _application;