Ant loadproperties 失败(bcel 错误?)

发布于 2024-07-19 08:49:10 字数 4268 浏览 8 评论 0 原文

我正在编写一个简单的构建脚本,该脚本应该从 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 任务 loadproperties 失败:

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)

更新 在 Eclipse 中设置 Ant 首选项后,错误消息发生了变化:

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...我迷路了。


答案:

这是下面的评论

我应该提到这一点 - 你不需要转换任何东西。 医生:“自从蚂蚁金服 1.7、字符编码ISO-8859-1用于从字符转换回 字节,因此必须使用此编码来读取 java 类文件。”这只是一个 约定以避免对原始字节使用字符过滤器的事实。 ant.apache.org/manual/CoreTypes/... 使用 UTF-8 会很糟糕! ——麦克道尔

I'm working on a simple build script that should get some constants from a java class file and use them as the version numbers in my file names. I use Eclipse and its own Ant, but put bcel-5.2.jar in my libs folder and into the classpath for the Ant call.

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

But unfortunatly the ant task loadproperties fails:

build.xml:46: expected a java resource as source

After that I tried to run Ant from outside Eclipse, using this command line:

set ANT_HOME=C:\Program Files\Java\ant\apache-ant-1.7.1
"%ANT_HOME%\bin\ant.bat"

The result is

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

I'm really lost now. Is it a bcel error? Is it an Ant incompatibility with my own bcel?

One last hint: Removing the bcel classpath entry from the Ant target results in this:

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)

UPDATE After setting the Ant preferences in Eclipse, the error message changed:

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)

Now I think it's maybe a version conflict between Ant and BCEL. Or BCEL and JDK1.6. Or Eclipse and BCEL and Ant or JDK... I'm lost.


ANSWER:

This is the comment found below

I should have mentioned this - you do not need to convert anything. Doc: "since Ant
1.7, the character encoding ISO-8859-1 is used to convert from characters back to
bytes, so ONE HAS TO USE THIS ENCODING for reading the java class file." This is just a
convention to get round the fact that a character filter is being used on raw bytes.
ant.apache.org/manual/CoreTypes/… Using UTF-8 would be bad! – McDowell

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷了相思 2024-07-26 08:49:10

糟糕,我就知道! 归根结底是文件编码问题。 这些文件仍然采用 ISO-8819-1 格式,但我使用的是 UTF-8。 该项目已经相当陈旧,并且是使用错误的编码创建的。 在 javacloadproperties 任务中设置参数 encoding 可以修复此问题。

<target name="generate_version" depends="compile">
    <loadproperties encoding="iso-8859-1" srcfile="${dir.dest}/MyVersion.class">
        <filterchain>
            <classconstants/>
        </filterchain>
    </loadproperties>
</target>

我认为它被我们的 Subversion 服务器更改了,但我想我现在必须自己将每个文件转换为 UTF-8...认为这是 SO 的另一个问题。

Crap, I knew it! It comes down to file encoding issues. The files are still in ISO-8819-1, but I'm using UTF-8. The project is pretty aged and was created with the wrong encoding in place. Setting the parameter encoding in the javac and loadproperties Task fixes it.

<target name="generate_version" depends="compile">
    <loadproperties encoding="iso-8859-1" srcfile="${dir.dest}/MyVersion.class">
        <filterchain>
            <classconstants/>
        </filterchain>
    </loadproperties>
</target>

I thought it got changed by our Subversion server, but I think I have to convert every single file to UTF-8 myself now... think that is another question for SO.

め七分饶幸 2024-07-26 08:49:10

loadproperties 的文档 指出嵌套的 < em>classpath 元素与 resource 属性一起使用 - 是使用 srcfile 的替代方法。

将 BCEL jar 添加到您的全局类路径。 在 Eclipse 中,将其作为全局条目添加到 运行时首选项。 在命令行上,使用 -lib 开关。

替代文本
(来源:eclipse.组织)

The documentation for loadproperties says that the nested classpath element is for use with the resource attribute - an alternative to using srcfile.

Add the BCEL jar to your global classpath. In Eclipse, add it as a global entry to your classpath in the runtime preferences. On the command line, use the -lib switch.

alt text
(source: eclipse.org)

乱世争霸 2024-07-26 08:49:10

看起来像类路径错误。 运行“ant -v”以提供有关错误的更多详细信息。

Looks like a classpath error. Run "ant -v" to give you some more detail on errors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文