2

我尝试使用 Ant 编译 jsoup 源。我可以编译成功,但无法通过测试。这是过程:

jsoup 版本:1.6.3;蚂蚁版本:1.8.2

jsoup的源码在目录中

src/

我做了一个构建文件

src/build.xml

该文件包含

<project name="jsoup">
<target name="compile">
    <mkdir dir="build/classes"/>
    <javac srcdir="src" destdir="build/classes" includeantruntime="false"/>
</target>

<target name="jar">
    <mkdir dir="build/jar"/>
    <jar destfile="build/jar/jsoup.jar" basedir="build/classes">
        <manifest>
            <attribute name="Main-Class" value="StateTrace"/>
        </manifest>
    </jar>
</target>

<target name="run">
    <!--<java jar="build/jar/jsoup.jar" input="htmls/index.html" fork="true"/>-->
    <exec executable="java">
        <arg value="-jar"/>
        <arg value="build/jar/jsoup.jar"/>
        <arg value="htmls/index.html"/>
    </exec>
</target>
</project>

注:1.StateTrace.java是我自己的测试程序(如下);2. htmls/index.html 是StateTrace.java 的输入。

String html;   // contains the content in htmls/index.html
...
try{
    Document doc = Jsoup.parse(html);
}catch(IOException e){e.printStackTrace();}

然后我用 Ant 编译并运行它:

> ant compile
> ant jar
> ant run

在此之后,我得到了这样的错误:

run:
 [exec] Exception in thread "main" java.lang.ExceptionInInitializerError
 [exec]     at org.jsoup.nodes.Entities$EscapeMode.<clinit>(Unknown Source)
 [exec]     at org.jsoup.nodes.Document$OutputSettings.<init>(Unknown Source)
 [exec]     at org.jsoup.nodes.Document.<init>(Unknown Source)
 [exec]     at org.jsoup.parser.TreeBuilder.initialiseParse(Unknown Source)
 [exec]     at org.jsoup.parser.TreeBuilder.parse(Unknown Source)
 [exec]     at org.jsoup.parser.HtmlTreeBuilder.parse(Unknown Source)
 [exec]     at org.jsoup.parser.Parser.parse(Unknown Source)
 [exec]     at org.jsoup.Jsoup.parse(Unknown Source)
 [exec]     at StateTrace.main(Unknown Source)
 [exec] Caused by: java.lang.NullPointerException
 [exec]     at java.util.Properties$LineReader.readLine(Properties.java:418)
 [exec]     at java.util.Properties.load0(Properties.java:337)
 [exec]     at java.util.Properties.load(Properties.java:325)
 [exec]     at org.jsoup.nodes.Entities.loadEntities(Unknown Source)
 [exec]     at org.jsoup.nodes.Entities.<clinit>(Unknown Source)
 [exec]     ... 9 more
 [exec] Result: 1

BUILD SUCCESSFUL
Total time: 0 seconds

但是,如果我手动编译了所有的 java 源代码,比如

javac src/org/jsoup/*.java src/org/jsoup/parser/*.java src/org/jsoup/examples/*.java src/org/jsoup/nodes/*.java src/org/jsoup/safety/*.java src/org/jsoup/select/*.java src/org/jsoup/helper/*.java

我可以成功编译并通过我的测试。

有什么线索吗?谢谢!

4

3 回答 3

0

我没有找到这个错误的根本原因。但我确实让它最终工作。

似乎类文件需要保存在源文件的相同目录中。所以 build.xml 是这样的:

<project>
<target name="compile">
    <mkdir dir="build/classes"/>
    <javac srcdir="src" destdir="src" includeantruntime="false"/>
</target>

<target name="clean">
    <delete dir="build"/>
</target>

<target name="jar">
    <mkdir dir="build/jar"/>
    <jar destfile="build/jar/jsoupIns.jar" basedir="src">
        <manifest>
            <attribute name="Main-Class" value="org.jsoup.examples.HtmlToPlainText"/>
        </manifest>
    </jar>
</target>

<target name="run">
    <!--<java jar="build/jar/jsoup.jar"  destdir="build/classes" input="htmls/index.html" fork="true"/>-->
    <exec executable="java">
        <arg value="-jar"/>
        <arg value="build/jar/jsoupIns.jar"/>
        <arg value="http://www.google.com"/>
    </exec>
</target>
</project>

如果您知道此问题的更深层原因的任何线索,请告诉我。谢谢!

于 2012-07-07T15:10:04.793 回答
0

jar 文件需要在jar 目录org/jsoup/nodes 中包含文件entities-base.properties 和entities-full.properties。将文件集添加到 ant jar 任务以包含这些文件就足够了。

<target name="local.all.build" >

   <echo message="Building ${local.app.jar.name}" />
   <mkdir dir="${top.dirs.class}"/>
   <mkdir dir="${top.dirs.jar}"/>

   <javac srcdir            = "${local.dirs.source}"
          destdir           = "${top.dirs.class}"
          debug             = "true"
          includeantruntime = "false" >
      <classpath refid="top.app.classpath"/>
      <compilerarg value="-Xlint:unchecked" />
   </javac>

   <jar jarfile   = "${top.dirs.jar}/${local.app.jar.name}"
        basedir   = "${top.dirs.class}" >
      <fileset dir      = "${top.dirs.source}"
               includes = "**/*.properties" />
   </jar>

</target>
于 2015-02-05T21:10:59.453 回答
0

此 build.xml 适用于 ant 1.8.2:

<?xml version="1.0"?>
<project name="jsoup">
    <target name="compile">
        <mkdir dir="build/classes"/>
        <javac srcdir="src" destdir="build/classes" includeantruntime="false"/>
    </target>

    <target name="jar">
        <mkdir dir="build/jar"/>
        <jar destfile="build/jar/jsoup.jar" basedir="build/classes">
            <manifest>
                <attribute name="Main-Class" value="org.jsoup.examples.HtmlToPlainText"/>
            </manifest>
        </jar>
    </target>

    <target name="run">
        <!--<java jar="build/jar/jsoup.jar" input="htmls/index.html" fork="true"/>-->
        <exec executable="java">
            <arg value="-jar"/>
            <arg value="build/jar/jsoup.jar"/>
            <arg value="htmls/index.html"/>
        </exec>
    </target>
</project>

... 等待更多信息 ... ... 直到出现,我的猜测是 htmls/index.html 中没有文件

于 2012-07-07T00:28:33.040 回答