我正在编写一个简单的构建脚本,它应该从java类文件中获取一些常量,并将它们用作文件名中的版本号。我使用Eclipse和它自己的Ant,但是将bcel-5.2.jar放在我的libs文件夹中,并放入Ant调用的类路径中。
<target name="generate_version" depends="compile">
<loadproperties srcfile="${dir.dest}/MyVersion.class">
<classpath>
<fileset dir="${dir.libs}">
<include name="**/bcel*.jar"/>
</fileset>
</classpath>
<filterchain>
<classconstants/>
</filterchain>
</loadproperties>
</target>但不幸的是,ant任务加载属性失败了:
build.xml:46: expected a java resource as source之后,我尝试从Eclipse外部运行Ant,使用以下命令行:
set ANT_HOME=C:\Program Files\Java\ant\apache-ant-1.7.1
"%ANT_HOME%\bin\ant.bat"结果是
Buildfile: build.xml
init:
[echo] Building project.
[echo] ant.home: C:\Program Files\Java\ant\apache-ant-1.7.1
[echo] ant.java.version: 1.6
[echo] ant.version: Apache Ant version 1.7.1 compiled on June 27 2008
compile:
[javac] Compiling 262 source files to **********\build
[javac] Note: Some input files use or override a deprecated API.
[javac] Note: Recompile with -Xlint:deprecation for details.
[javac] Note: Some input files use unchecked or unsafe operations.
[javac] Note: Recompile with -Xlint:unchecked for details.
generate_version:
BUILD FAILED
********************\ant\build.xml:46: expected a java resource as source我现在真的迷路了。是bcel错误吗?这是Ant与我自己的bcel不兼容吗?
最后一个提示:从Ant目标中删除bcel类路径条目将导致以下结果:
Buildfile: build.xml
init:
[echo] Building project.
[echo] ant.home: C:\Program Files\Java\ant\apache-ant-1.7.1
[echo] ant.java.version: 1.6
[echo] ant.version: Apache Ant version 1.7.1 compiled on June 27 2008
compile:
[javac] Compiling 262 source files to ********************\build
[javac] Note: Some input files use or override a deprecated API.
[javac] Note: Recompile with -Xlint:deprecation for details.
[javac] Note: Some input files use unchecked or unsafe operations.
[javac] Note: Recompile with -Xlint:unchecked for details.
generate_version:
BUILD FAILED
java.lang.NoClassDefFoundError: org/apache/bcel/classfile/ClassParser
at org.apache.tools.ant.filters.util.JavaClassHelper.getConstants(JavaClassHelper.java:47)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)在中设置首选项之后,错误消息更改为:
BUILD FAILED
*********************\build.xml:46: org.apache.bcel.classfile.ClassFormatException: is not a Java .class file
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:115)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:357)
at org.apache.tools.ant.Target.performTasks(Target.java:385)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1329)
at org.apache.tools.ant.Project.executeTarget(Project.java:1298)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.eclipse.ant.internal.ui.antsupport.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32)
at org.apache.tools.ant.Project.executeTargets(Project.java:1181)
at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.run(InternalAntRunner.java:423)
at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.main(InternalAntRunner.java:137)现在,我认为这可能是Ant和BCEL之间的版本冲突。或BCEL和JDK1.6。或者Eclipse和BCEL还有Ant或者JDK..。我迷路了。
答案:
这是下面的评论
我应该提到这一点-你不需要改变任何东西。Doc:“自从Ant 1.7以来,编码ISO-8859-1的字符被用来从字符转换回字节,所以必须使用这种编码来读取java类文件。”这只是一种惯例,以绕过字符筛选器被用于原始字节这一事实。Ant.apache.org/手册/CoreTypes/…使用UTF-8是不好的!- McDowell
发布于 2009-05-18 12:13:07
该死,我就知道!这归结为文件编码问题。文件仍然在ISO-8819-1中,但我使用的是UTF-8。该项目相当陈旧,是在错误的编码条件下创建的。在javac和loadproperties任务中设置参数编码会修复它。
<target name="generate_version" depends="compile">
<loadproperties encoding="iso-8859-1" srcfile="${dir.dest}/MyVersion.class">
<filterchain>
<classconstants/>
</filterchain>
</loadproperties>
</target>我以为它被我们的Subversion服务器改变了,但我想我必须把每个文件都转换成UTF-8 .我认为这是另一个问题。
发布于 2009-05-18 09:28:01
看起来像是类路径错误。运行"ant -v“,向您提供有关错误的更多细节。
https://stackoverflow.com/questions/876840
复制相似问题