我正在尝试使用 Ant 和 Eclipse 构建一个大型 Java 项目。因此,有许多引用的 jar 库,包括 Ant 使用的 asm.jar(我的 ANT_HOME/lib 中默认为 2.2.3 版)。现在碰巧我的项目需要不同版本的 asm.jar(特别是 3.0)。
但是,每当我尝试从命令行运行我的 ant 构建脚本时,它最终都会加载 asm-2.2.3.jar 而不是 asm-3.0.jar。我该如何解决这个冲突?
细节:
首先,我的 build.xml 文件:
<project name="MyProject" default="all" basedir=".">
<property name="runtime.jar" value="mp3.jar"/>
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="dist" location="dist"/>
<property name="test" location="test/unit/src" />
<property name="testclasses" location="test/unit/classes" />
<property name="project.lib" location="lib"/>
<property name="project.lib.jar" location="${project.lib}/jars"/>
<property name="project.lib.conf" location="${project.lib}/conf"/>
<property name="project.lib.gameconfigs" location="gameconfigs"/>
<property name="project.lib.images" location="${project.lib}/images"/>
<property name="project.classes" location="${build}"/>
<property name="project.doc" location="doc"/>
<property name="project.doc.java" location="${project.doc}/javadoc"/>
<property name="project.src" location="src"/>
<property name="experiments.src" location="experiments"/>
<property name="project.gui" location="gui"/>
<property name="project.agents" location="agents"/>
<property name="project.webctrl" location="webctrl"/>
<property name="project.web" location="webctrl/web"/>
<property name="project.webinf" location="webctrl/web/WEB-INF"/>
<path id="project.classpath">
<fileset dir=".">
<include name="lib/jars/*.jar"/>
</fileset>
</path>
<path id="test.classpath">
<pathelement location="lib/jars/junit-4.8.2.jar" />
<pathelement location="${test}" />
<pathelement location="${testclasses}" />
<pathelement location="${project.classes}"/>
<path refid="project.classpath" />
</path>
<!-- Import build specific targets for building MyProject -->
<import file="lib/conf/ant/ct-build.xml" as="ct-build"/>
</project>
现在,引用导入的相关部分:
<target name="init">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}"/>
</target>
<available property="unjar.done" file="${build}/org/apache/log4j/Appender.class"/>
<target name="unjar" depends="init" description="unjar the jars" unless="unjar.done">
<unjar src="${project.lib.jar}/mysql-connector-java-5.1.7-bin.jar" dest="${build}"/>
<unjar src="${project.lib.jar}/commons-codec-1.3.jar" dest="${build}"/>
<unjar src="${project.lib.jar}/forms_rt.jar" dest="${build}"/>
<unjar src="${project.lib.jar}/activemq-all-5.4.2.jar" dest="${build}"/>
<unjar src="${project.lib.jar}/log4j-1.2.14.jar" dest="${build}"/>
<unjar src="${project.lib.jar}/junit-4.8.2.jar" dest="${build}"/>
<unjar src="${project.lib.jar}/asm.jar" dest="${build}"/>
<unjar src="${project.lib.jar}/javac2.jar" dest="${build}"/>
<property name="unjar.done" value="true" />
</target>
<target name="compile" depends="unjar"
description="compile the source " >
<!-- Compile the java code from ${src} into ${build} -->
<mkdir dir="${build}/gnu/getopt"/>
<copy todir="${build}/gnu/getopt">
<fileset dir="${src}/gnu/getopt">
<include name="*.properties"/>
</fileset>
</copy>
<taskdef
name="uidesigner"
classname="com.intellij.uiDesigner.ant.Javac2"
classpathref="project.classpath"/>
<uidesigner
destdir="${project.classes}"
debug="on"
source="1.5"
includeantruntime="false">
<src path="${project.src}"/>
<src path="${project.gui}"/>
<src path="${project.agents}"/>
<src path="${experiments.src}"/>
<include name="**/*.java"/>
<include name="**/*.form"/>
<classpath refid="project.classpath"/>
<classpath path="${project.src}"/>
<classpath path="${project.classes}"/>
<exclude name="misc/**"/>
<exclude name="docs/**"/>
</uidesigner>
</target>
值得注意的是,在 Eclipse 中编写此代码会警告我无法找到 project.classpath(在导入的文件中)。但是,从命令行运行ant 时,定位Javac2.class 似乎没有问题。相反,我只是得到一个错误,说 asm.jar 中的一个方法丢失(我确定这是由于加载了错误的版本)。见下文:
compile:
[uidesigner] classpath=
E:\Repositories\MyProject\mp3\build;
E:\Repositories\MyProject\mp3\lib\jars\activemq-all-5.4.2.jar;
E:\Repositories\MyProject\mp3\lib\jars\asm-commons-3.0.jar;
E:\Repositories\MyProject\mp3\lib\jars\asm.jar;
E:\Repositories\MyProject\mp3\lib\jars\bcel.jar;
E:\Repositories\MyProject\mp3\lib\jars\commons-codec-1.3.jar;
E:\Repositories\MyProject\mp3\lib\jars\forms-1.1-preview.jar;
E:\Repositories\MyProject\mp3\lib\jars\forms_rt.jar;
E:\Repositories\MyProject\mp3\lib\jars\javac2.jar;
E:\Repositories\MyProject\mp3\lib\jars\jdom.jar;
E:\Repositories\MyProject\mp3\lib\jars\junit-4.8.2.jar;
E:\Repositories\MyProject\mp3\lib\jars\log4j-1.2.14.jar;
E:\Repositories\MyProject\mp3\lib\jars\mysql-connector-java-5.1.7-bin.jar;
E:\Repositories\MyProject\mp3\lib\jars\xerces.jar;
E:\Repositories\MyProject\mp3\src;E:\Repositories\MyProject\mp3\build;
E:\apache-ant-1.9.2\lib\ant-launcher.jar;
E:\Repositories\MyProject\mp3\.\;
C:\Program Files (x86)\Java\jre7\lib\ext\QTJava.zip;
E:\apache-ant-1.9.2\lib\activation-1.1.jar;
E:\apache-ant-1.9.2\lib\ant-antlr.jar;
E:\apache-ant-1.9.2\lib\ant-apache-bcel.jar;
E:\apache-ant-1.9.2\lib\ant-apache-bsf.jar;
E:\apache-ant-1.9.2\lib\ant-apache-log4j.jar;
E:\apache-ant-1.9.2\lib\ant-apache-oro.jar;
E:\apache-ant-1.9.2\lib\ant-apache-regexp.jar;
E:\apache-ant-1.9.2\lib\ant-apache-resolver.jar;
E:\apache-ant-1.9.2\lib\ant-apache-xalan2.jar;
E:\apache-ant-1.9.2\lib\ant-commons-logging.jar;
E:\apache-ant-1.9.2\lib\ant-commons-net.jar;
E:\apache-ant-1.9.2\lib\ant-jai.jar;
E:\apache-ant-1.9.2\lib\ant-javamail.jar;
E:\apache-ant-1.9.2\lib\ant-jdepend.jar;
E:\apache-ant-1.9.2\lib\ant-jmf.jar;
E:\apache-ant-1.9.2\lib\ant-jsch.jar;
E:\apache-ant-1.9.2\lib\ant-junit.jar;
E:\apache-ant-1.9.2\lib\ant-junit4.jar;
E:\apache-ant-1.9.2\lib\ant-launcher.jar;
E:\apache-ant-1.9.2\lib\ant-netrexx.jar;
E:\apache-ant-1.9.2\lib\ant-swing.jar;
E:\apache-ant-1.9.2\lib\ant-testutil.jar;
E:\apache-ant-1.9.2\lib\ant.jar;
E:\apache-ant-1.9.2\lib\antlr-2.7.7.jar;
E:\apache-ant-1.9.2\lib\asm-2.2.3.jar;
E:\apache-ant-1.9.2\lib\asm-commons-2.2.3.jar;
E:\apache-ant-1.9.2\lib\asm-tree-2.2.3.jar;
E:\apache-ant-1.9.2\lib\bcel-5.1.jar;
E:\apache-ant-1.9.2\lib\bsf-2.4.0.jar;
E:\apache-ant-1.9.2\lib\bsh-2.0b4.jar;
E:\apache-ant-1.9.2\lib\bsh-core-2.0b4.jar;
E:\apache-ant-1.9.2\lib\commons-logging-1.0.4.jar;
E:\apache-ant-1.9.2\lib\commons-logging-api-1.1.jar;
E:\apache-ant-1.9.2\lib\commons-net-1.4.1.jar;
E:\apache-ant-1.9.2\lib\hamcrest-core-1.3.jar;
E:\apache-ant-1.9.2\lib\jai-codec-1.1.3.jar;
E:\apache-ant-1.9.2\lib\jai-core-1.1.3.jar;
E:\apache-ant-1.9.2\lib\jasper-compiler-4.1.36.jar;
E:\apache-ant-1.9.2\lib\jasper-runtime-4.1.36.jar;
E:\apache-ant-1.9.2\lib\jdepend-2.9.1.jar;
E:\apache-ant-1.9.2\lib\jruby-0.9.8.jar;
E:\apache-ant-1.9.2\lib\js-1.6R7.jar;
E:\apache-ant-1.9.2\lib\jsch-0.1.50.jar;
E:\apache-ant-1.9.2\lib\junit-4.11.jar;
E:\apache-ant-1.9.2\lib\log4j-1.2.14.jar;
E:\apache-ant-1.9.2\lib\mail-1.4.jar;
E:\apache-ant-1.9.2\lib\maven-artifact-ant-2.0.4-dep.jar;
E:\apache-ant-1.9.2\lib\NetRexxC.jar;
E:\apache-ant-1.9.2\lib\oro-2.0.8.jar;
E:\apache-ant-1.9.2\lib\regexp-1.2.jar;
E:\apache-ant-1.9.2\lib\regexp-1.3.jar;
E:\apache-ant-1.9.2\lib\serializer-2.7.1.jar;
E:\apache-ant-1.9.2\lib\servlet-api-2.3.jar;
E:\apache-ant-1.9.2\lib\which-1.0.jar;
E:\apache-ant-1.9.2\lib\xalan-2.7.1.jar;
E:\apache-ant-1.9.2\lib\xml-apis-1.3.04.jar;
E:\apache-ant-1.9.2\lib\xml-resolver-1.2.jar;
C:\Program Files (x86)\Java\jdk1.6.0_26\lib\tools.jar;
C:\Program Files (x86)\Java\jdk1.6.0_26\jre\lib\resources.jar;
C:\Program Files (x86)\Java\jdk1.6.0_26\jre\lib\rt.jar;
C:\Program Files (x86)\Java\jdk1.6.0_26\jre\lib\jsse.jar;
C:\Program Files (x86)\Java\jdk1.6.0_26\jre\lib\jce.jar;
C:\Program Files (x86)\Java\jdk1.6.0_26\jre\lib\charsets.jar;
C:\Program Files (x86)\Java\jdk1.6.0_26\jre\lib\ext;
C:\Windows\Sun\Java\lib\ext\dnsns.jar;
C:\Program Files (x86)\Java\jdk1.6.0_26\jre\lib\ext;
C:\Windows\Sun\Java\lib\ext\localedata.jar;
C:\Program Files (x86)\Java\jdk1.6.0_26\jre\lib\ext;
C:\Windows\Sun\Java\lib\ext\sunjce_provider.jar;
C:\Program Files (x86)\Java\jdk1.6.0_26\jre\lib\ext;
C:\Windows\Sun\Java\lib\ext\sunmscapi.jar;
C:\Program Files (x86)\Java\jdk1.6.0_26\jre\lib\ext;C:\Windows\Sun\Java\lib\ext\sunpkcs11.jar
BUILD FAILED
E:\Repositories\MyProject\mp3\lib\conf\ant\ct-build.xml:49: java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.<init>(I)V
at com.intellij.ant.AntClassWriter.<init>(AntClassWriter.java:13)
at com.intellij.ant.Javac2.instrumentForms(Javac2.java:134)
at com.intellij.ant.Javac2.compile(Javac2.java:50)
at org.apache.tools.ant.taskdefs.Javac.execute(Javac.java:930)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
at sun.reflect.GeneratedMethodAccessor5.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:435)
at org.apache.tools.ant.Target.performTasks(Target.java:456)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1393)
at org.apache.tools.ant.Project.executeTarget(Project.java:1364)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1248)
at org.apache.tools.ant.Main.runBuild(Main.java:851)
at org.apache.tools.ant.Main.startAnt(Main.java:235)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)
最后一点:我不知道为什么 ant 加载 Java 6 库而不是 Java 7。