Maven和Ant无法运行Java -错误处理错误=206,文件名或扩展名太长

cunj1qz1  于 5个月前  发布在  Maven
关注(0)|答案(5)|浏览(86)

当maven通过antrun执行此java代码时,我得到可怕的错误=206,文件名或扩展名太长

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

字符串

s1ag04yj

s1ag04yj1#

由于本地maven存储库的结构和位置,Maven会创建很长的类路径。我们需要使用一个pathing jar。

  • 将类路径转换为字符串
  • 转义Windows驱动器号(C:=坏\C:=好)
  • 只创建带有类路径属性的清单jar
  • 使用pathing jar而不是maven compile类路径
<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>

字符串

uidvcgyl

uidvcgyl2#

扩展@user4386022提供的答案:您可以定义(从Ant 1.8开始)这个宏,如果您在构建过程中的不同地方遇到相同的问题,它可以帮助您(并且您不能只是复制粘贴相同的片段,因为Ant不允许重新定义属性,所以您将得到一个错误,说“manifest.classpath”已经定义。

<macrodef name="create-classpath-jar" description="Create classpath Jar, to avoid getting the error about CreateProcess error=206, The filename or extension is too long">
    <attribute name="classpathjar"/>
    <attribute name="classpathref"/>
    <sequential>
        <!-- Turn the classpath into a property formatted for inclusion in a MANIFEST.MF file -->
        <local name="manifest.classpath.property"/>
        <manifestclasspath property="manifest.classpath.property" jarfile="@{classpathjar}">
            <classpath refid="@{classpathref}" />
        </manifestclasspath>
        <!-- Create the Jar -->
        <jar destfile="@{classpathjar}">
            <manifest>
                <attribute name="Class-Path" value="${manifest.classpath.property}"/>
            </manifest>                      
        </jar>
    </sequential>
</macrodef>

字符串
要在目标或任务中使用宏,只需像这样使用它:

<path id="myclasspath">
   .........
</path>
<create-classpath-jar classpathjar="classpath-compile.jar" classpathref="myclasspath" />

ego6inou

ego6inou3#

如果使用Ant1.7或更高版本,则可以利用manifestclasspath任务生成清单文件,然后将其包含在jar中,以便在javac类路径上使用

<!-- turn the classpath into a property formatted for inclusion in a MANIFEST.MF file -->
<manifestclasspath property="manifest.classpath"
                   jarfile="${classpath-compile.jar}">
  <classpath refid="maven.compile.classpath" />
</manifestclasspath>

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

字符串

jc3wubiy

jc3wubiy4#

通过从build.xml文件中的javac目标中删除fork="true"修复了问题。如果构建过程中必须使用forking,请参考上面的解决方案。

8nuwlpux

8nuwlpux5#

Java提供了一个简单的机制来永久解决这个问题,它被称为@argfile。这个想法很简单:
1.与将所有参数作为命令行参数附加到java命令不同,您可以将它们写入一个简单的文本文件,其中每个参数位于单独的行中,例如使用echo任务。
1.然后指定该文本文件的位置,如下所示:java @myArguments.txt
然后Java读取文本文件,并将在其中找到的任何内容作为参数展开。由于您使用Ant,因此您的配置可能如下所示:

<property name="test_classpath" refid="maven.test.classpath"/>
<echo message="-classpath&#xD;&#xA;${test_classpath}&#xD;&#xA;${className}&#xD;&#xA;${name}&#xD;&#xA;${wsdlFile}"
      file="${project.build.directory}/antJvmArgs.txt"/>
<java classname="com.me.api" failonerror="true" fork="true" maxmemory="128m" output="${wsdlFile}.out">
  <arg value="@${project.build.directory}/antJvmArgs.txt" />
</java>

字符串
一些补充说明:

  • &#xD;&#xA;CRLF的XML编码版本,即Windows换行符。
  • 作为参考,生成的文本文件看起来像这样:
-classpath
C:\Users\username\git\myProject\target\test-classes;C:\Users\username\git\myProject\target\classes
what.ever.the.value.of.className.Is
ValueOfTheNameVariable
C:\Users\username\git\myProject\target\wsdlFile.wsdl

相关问题