简体   繁体   English

缩短命令行的类路径 (-cp)

[英]Shorten classpath (-cp) for command line

My maven build in failing on jdeps plugin (we need it to upgrade to jdk11).我的 maven 构建在 jdeps 插件上失败(我们需要它升级到 jdk11)。

The command line is too long for windows .命令行对于 windows 来说太长了 Here is the error I get:这是我得到的错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-jdeps-plugin:3.1.1:jdkinternals (default) on project myproject:
[ERROR] Exit code: 1 - La ligne de commande est trop longue.
[ERROR]
[ERROR] Command line was: cmd.exe /X /C 
"
    "C:\Program Files\Java\jdk-11.0.2\bin\jdeps.exe"
    -cp "
        C:\Users\Me\.m2\repository\com\something\firstJar.jar;
        C:\Users\Me\.m2\repository\com\somethingElse\secondJar.jar;
        C:\Users\Me\.m2\repository\com\somethingDifferent\someOtherJar.jar;
        ... and one more
        ... and another one
        ... I think you get the idea......."
    --multi-release 9 D:\git\myworkspace\myproject\target\classes
"

For the example I only let 3 jars but I have so many dependencies...例如,我只放了 3 个 jar,但我有很多依赖项...

How to shorten this command-line?如何缩短此命令行? (and make sure it is not user dependant) (并确保它不依赖于用户)

Restriction : It's a shared project, changing anything only on my computer is not a solution.限制:这是一个共享项目,仅在我的计算机上更改任何内容都不是解决方案。

The maven-jdeps-plugin is using plexus-utils to fork out a child process to run the jdeps executable. maven-jdeps-plugin使用plexus-utils派生出一个子进程来运行 jdeps 可执行文件。 plexus-utils implements this by building up a command-line and passing it to cmd.exe. plexus-utils 通过构建命令行并将其传递给 cmd.exe 来实现这一点。 This is the wrong approach as it will be subject to the 8192 char limit imposed by cmd.exe.这是错误的方法,因为它将受到 cmd.exe 施加的 8192 个字符限制。 The correct approach would be to use the Java ProcessBuilder API.正确的方法是使用 Java ProcessBuilder API。 This itself uses ProcessImpl.create API method, which, on Windows, is implemented by a Win32 API call to CreateProcess.这本身使用 ProcessImpl.create API 方法,该方法在 Windows 上由对 CreateProcess 的 Win32 API 调用实现。 The latter API has a 32k char limit, which should be enough for most use cases.后一个 API 有 32k 个字符的限制,对于大多数用例来说应该足够了。

There is a plexus-utils bug report for this.有一个 plexus-utils错误报告 You may want to raise one with maven-jdeps-plugin as well - the Java ProcessBuilder API is quite usable, so there's no need to use plexus-utils just to run jdeps.您可能还想使用 maven-jdeps-plugin 来提高一个 - Java ProcessBuilder API 非常有用,所以没有必要使用 plexus-utils 来运行 jdeps。

If you are using Windows 10 Anniversary Update or Windows Server 2016, or later, you can increase the maximum path length beyond the 260 character default.如果您使用的是 Windows 10 周年更新或 Windows Server 2016 或更高版本,则可以将最大路径长度增加到超过 260 个字符的默认值。

You can either copy the following two lines into a file with a .reg extension and open it,您可以将以下两行复制到扩展名为.reg的文件中并打开它,

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem]
"LongPathsEnabled"=dword:00000001

Or, open the Registry Editor and browse to the location, and change the value from 0 to 1.或者,打开注册表编辑器并浏览到该位置,然后将值从 0 更改为 1。

The best is to create empty jar file with classpath configured in manifest.最好的方法是创建jar 文件,并在清单中配置类路径。

Official oracle document is at Adding Classes to the JAR File's Classpath官方 oracle 文档位于将类添加到 JAR 文件的类路径

maven-jar plugin does support updating manifest classpath attribute: How to add Class-Path to the manifest file with maven maven-jar插件确实支持更新清单类路径属性: How to add Class-Path to the manifest file with maven

maybe a bit of a cheeky solution but...也许有点厚颜无耻的解决方案但是......

what about using an env variable?使用环境变量怎么样?

set MR=C:\Users\Me\.m2\repository\

"C:\Program Files\Java\jdk-11.0.2\bin\jdeps.exe"
    -cp "
        %MR%\com\something\firstJar.jar;
        %MR%\com\somethingElse\secondJar.jar;

did not test, hope it works...没有测试,希望它有效......

This is the reason Maven is meant to mange a large amount of dependency as you can simply place all of them in the .pom file.这就是 Maven 旨在管理大量依赖项的原因,因为您可以简单地将所有依赖项放在 .pom 文件中。 The use of the a centralized dependency list allows Maven to be able to see nearly everything you need to run your program.使用集中的依赖项列表使 Maven 能够查看运行程序所需的几乎所有内容。 Take a look at this post that does what you're trying though add each of your jar's for Maven to see them.看看这篇文章,尽管添加了每个 jar 以便 Maven 看到它们,但它可以完成您正在尝试的操作。 How do I package and run a simple command-line application with dependencies using maven? 如何使用 maven 打包和运行具有依赖项的简单命令行应用程序?

Also, a good guide to the .pom basics Read the pom guide on www.maven.apache.org.另外,一个很好的 .pom 基础指南 阅读www.maven.apache.org 上的 pom 指南。

Maven will not use cmdline arguments the way you are trying because of the Manifest specifications.由于 Manifest 规范,Maven 不会像您尝试的那样使用 cmdline 参数。 This is the fundamental reason programmers, including myself, love Maven so it will really make life much simpler as it's built to do exactly what you need.这是程序员(包括我自己)喜欢 Maven 的根本原因,因此它确实会让生活变得更加简单,因为它可以完全满足您的需求。 As the files change you have one file to make your updated versions.随着文件的变化,您有一个文件来制作更新版本。

So I did struggle with this problem for a long time and finally found solution to this issue with too long classpath while doing maven build.因此,我确实与这个问题斗争了很长时间,最终在进行 maven 构建时找到了类路径过长的解决方案。 This is a workaround but it works perfectly.这是一种解决方法,但效果很好。

Run build from linux - that't not a joke (sic!)从 linux 运行构建 - 这不是一个笑话(原文如此!)

  • Turn on WSL(Windows Subsystem for Linux) on Windows by following https://docs.microsoft.com/en-us/windows/wsl/install-win10按照https://docs.microsoft.com/en-us/windows/wsl/install-win10在 Windows 上打开 WSL(适用于 Linux 的 Windows 子系统)
  • After all done just Run your Linux subsystem on windows毕竟完成只是在 Windows 上运行你的 Linux 子系统
  • edit linux maven settings.xml /usr/share/maven/conf/settings.xml编辑 linux maven settings.xml /usr/share/maven/conf/settings.xml
  • Add or override <localRepository>/mnt/c/.m2</localRepository> (/mnt/c/.m2) - is my windows maven repo path seen from WSL添加或覆盖<localRepository>/mnt/c/.m2</localRepository> (/mnt/c/.m2) - 是从 WSL 看到的我的 windows maven repo 路径
  • cd /path/to/your/project cd /path/to/你的/项目
  • mvn build mvn 构建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM