Java – How to resolve Maven exec plugin: classpath too long error

classpathjavamaven-2maven-plugin

I have a large Java project with a large number of jar file dependencies. When I try to run the project (using exec) from Eclipse or Netbeans, Maven throws an exception which turns out to be a too large number of entries on the classpath (only 2/3 of the needed entries are included). Does anyone know a workaround for this? (Except from building an executable jar and running it from terminal.) Is it possbile to "extend" the "classpath-buffer"-size?

Best Answer

This is a Maven exec plugin bug, it is documented in MEXEC-68, the reporter created a patch so I hope it will be resolved soon.

One workaround would be to add the classpath to the manifest file using this config for the maven-jar-plugin, add the dependencies to a folder and add just that folder to the CLASSPATH envvar.

For example:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        ...
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
            </manifest>
          </archive>
        </configuration>
        ...
      </plugin>
    </plugins>
  </build>
  ...
</project>

This will add to the manifest something like:

Class-Path: plexus-utils-1.1.jar commons-lang-2.1.jar

If that JARs are in a CLASSPATH folder, you can run your JAR using maven exec plugin hidding the classpath with something like:

mvn exec:exec [...] -Dexec.classpathScope="test"

I used -Dexec.classpathScope="test" to make the plugin ignore the dependencies and add just the ones in scope test.