Maven and Ant Can't run Java - CreateProcess error=206, The filename or extension is too long

Peter Kahn picture Peter Kahn · Jul 19, 2013 · Viewed 16.3k times · Source

When maven via antrun executes this java code I get the dreaded error=206, The filename or extension is too long

<java classname="com.me.api" failonerror="true" fork="true" maxmemory="128m" output="${wsdlFile}.out">
  <arg value="${className}" />
  <arg value="${name}" />
  <arg value="${wsdlFile}" />
  <classpath>
    <path refid="maven.test.classpath" />
  </classpath>

Answer

Peter Kahn picture Peter Kahn · Jul 19, 2013

Maven creates lengthy classpaths due to the structure and location of the local maven repo. We need to use a pathing jar.

  • Convert Classpath into string
  • Escape windows drive letter (C: = bad \C: = good)
  • Create manifest only jar with class path attribute
  • Use the pathing jar instead of the maven compile classpath

<mkdir dir="${classpath-compile.dir}"/>

<!-- Convert into usable string .   -->
<pathconvert property="compile_classpath_raw" pathsep=" ">
    <path refid="maven.compile.classpath"/>                        
</pathconvert>

<!-- escape windows drive letters (remove C: from paths -- need to wrap with a condition os.family="windows")-->
<propertyregex property="compile_classpath_prep" 
  input="${compile_classpath_raw}"
  regexp="([A-Z]:)"
  replace="\\\\\1"
  casesensitive="false"
  global="true"/>

<!-- Create pathing Jars -->
<jar destfile="${classpath-compile.jar}">
  <manifest>
    <attribute name="Class-Path" value="${compile_classpath_prep}"/>
  </manifest>                      
</jar>

<java classname="com.me.api" failonerror="true" fork="true" maxmemory="128m" output="${wsdlFile}.out">
  <arg value="${className}" />
  <arg value="${name}" />
  <arg value="${wsdlFile}" />
  <classpath>
    <pathelement location="${classpath-compile.jar}" />
  </classpath>