我正在开发一个遗留应用程序,它使用包 org.apache.tools.ant.taskdefs
使用 ant 任务生成 Javadoc。在那里我需要传递 maven 给出的类路径。这个类路径在 maven 本地存储库中主要有 jar 路径。
Javadoc javadoc = new Javadoc();
javadoc.setUseExternalFile(true);
javadoc.setProject(proj);
javadoc.setClasspath(new Path(proj, classpath));
问题是这个类路径足够长,因此不允许在 Windows 上执行,这会导致以下错误。
enterJavadoc failed: java.io.IOException: Cannot run program "C:\jdk1.8.0_144\bin\javadoc.exe": CreateProcess error=206, The filename or extension is too long code here
对于这个问题,我所做的是创建一个路径 jar,在其 list 文件中包含长类路径。所以我将这个 jar 文件添加为 Javadoc 任务的类路径。我在 ant 中使用 ManifestClassPath 任务为我的长类路径中的 jars 生成相对路径。
ManifestClassPath mc = new ManifestClassPath();
mc.setProject(proj);
mc.addClassPath(new Path(proj, classPath));
mc.setJarFile(new File(pathingJar));
mc.setProperty("classpath");
mc.setMaxParentLevels(20);
mc.execute();
Jar jar = new Jar();
Manifest manifest = new Manifest();
Manifest.Attribute attribute = new Manifest.Attribute();
attribute.setName("Class-Path");
attribute.setValue(proj.getProperty("classpath"));
manifest.addConfiguredAttribute(attribute);
jar.addConfiguredManifest(manifest);
jar.setDestFile(new File(pathingJar));
jar.setProject(proj);
jar.execute();
javadoc.setClasspath(new Path(proj, pathingJar));
此解决方案一直有效,直到我在 Maven 本地存储库所在的同一驱动器中开发它。但是 Maven 本地存储库位于不同的驱动器中,它失败了,因为它无法为不同的驱动器生成相对路径。然后我尝试使用如下文件协议(protocol)处理类路径
Class-Path: file:///C:/mvn_repo/... file:///C:/mvn_repo/... ...
由于 Javadoc 任务失败,此解决方案似乎不起作用。有没有其他方法可以尝试使用路径 jar 来解决这个长类路径问题?
最佳答案
正如@MarquisofLorne 所提到的,你不能。
Class-path entries in the Manifest are relative URLs
无论如何,您可以对文件夹链接使用解决方法。在您的pathing jar 驱动器上,您可以创建一个指向 maven 目录的文件夹链接。文件夹链接可以指向另一个驱动器。
mklink /J "mvn_repo_link" "C:\mvn_repo".
然后你可以通过mvn_repo_link
子目录使用相对路径
关于java - 如何将不同驱动器中的 jar 包含到 jar 文件中的 list 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62573605/