简体   繁体   中英

Shorten classpath (-cp) for command line

My maven build in failing on jdeps plugin (we need it to upgrade to jdk11).

The command line is too long for 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...

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. plexus-utils implements this by building up a command-line and passing it to cmd.exe. This is the wrong approach as it will be subject to the 8192 char limit imposed by cmd.exe. The correct approach would be to use the Java ProcessBuilder API. This itself uses ProcessImpl.create API method, which, on Windows, is implemented by a Win32 API call to CreateProcess. The latter API has a 32k char limit, which should be enough for most use cases.

There is a plexus-utils bug report for this. 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.

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.

You can either copy the following two lines into a file with a .reg extension and open it,

[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.

The best is to create empty jar file with classpath configured in manifest.

Official oracle document is at Adding Classes to the JAR File's Classpath

maven-jar plugin does support updating manifest classpath attribute: 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. The use of the a centralized dependency list allows Maven to be able to see nearly everything you need to run your program. Take a look at this post that does what you're trying though add each of your jar's for Maven to see them. How do I package and run a simple command-line application with dependencies using maven?

Also, a good guide to the .pom basics Read the pom guide on www.maven.apache.org.

Maven will not use cmdline arguments the way you are trying because of the Manifest specifications. 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. 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. This is a workaround but it works perfectly.

Run build from linux - that't not a joke (sic!)

  • Turn on WSL(Windows Subsystem for Linux) on Windows by following https://docs.microsoft.com/en-us/windows/wsl/install-win10
  • After all done just Run your Linux subsystem on windows
  • edit 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
  • cd /path/to/your/project
  • mvn build

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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