帮助处理 ant 文件 - Java 任务的类路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1988566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 18:50:35  来源:igfitidea点击:

help with ant file - classpath for Java task

javaantclasspath

提问by Ravishankar

I have the below build file for Ant and trying to use the target 'run' for executing the program.

我有以下用于 Ant 的构建文件,并尝试使用目标“运行”来执行程序。

<property name="springjar"  location="E:/Tools/spring-30/dist/" />
<property name="logjar"     location="E:/Tools/commons-logging-1.1.1/" />

<patternset id="jar.files"><include name="**/*.jar"/></patternset>

<path id="springlearn.classpath">
    <fileset dir="${springjar}"><patternset refid="jar.files"/></fileset>
    <fileset dir="${logjar}"><patternset refid="jar.files"/></fileset>
</path>


<target name="run" depends="dist" description="Execute the Java Program">
    <java dir ="." fork="true" jar="dist\app.jar" classpathref ="springlearn.classpath">
    </java>
</target>

Using the same classpathref, I am able to successfully compile & create the jar, but when use the target run, I get the below error

使用相同的类路径引用,我能够成功编译和创建 jar,但是当使用目标运行时,我收到以下错误

java.lang.NoClassDefFoundError: org/springframework/core/io/Resource
Caused by: java.lang.ClassNotFoundException: org.springframework.core.io.Resource
    at java.net.URLClassLoader.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:289)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)

Exception in thread "main"

线程“main”中的异常

Any help is greatly appreciated. Thanks

任何帮助是极大的赞赏。谢谢

回答by matt

From the javatask docs:

java任务文档

When using the jar attribute, all classpath settings are ignored according to Sun's specification.

使用 jar 属性时,根据Sun 的规范,所有类路径设置都将被忽略。

So the classpath you set up isn't being looked at at all.

所以你设置的类路径根本没有被查看。

You could add dist/app.jarto your classpath and call the main class direct:

您可以添加dist/app.jar到您的类路径并直接调用主类:

<java dir ="." fork="true" classname="com.yourdomain.YourMainClass">
  <classpath>
    <path refid="springlearn.classpath" />
    <pathelement location="dist\app.jar" />
  </classpath>
</java>

If you want to call the jar, you'll need to set up a classpath entry in the manifest when you build it. Have a look at the pathconverttask, it could be useful.

如果要调用 jar,则需要在构建它时在清单中设置类路径条目。看看pathconvert任务,它可能很有用。

回答by duffymo

Here's a generic Ant build.xml that works well for me. See if it can help you as well:

这是一个对我很有效的通用 Ant build.xml。看看它是否也能帮助你:

    <?xml version="1.0" encoding="UTF-8"?>
    <project name="ui" basedir="." default="package">

    <property name="version" value="1.6"/>
    <property name="haltonfailure" value="no"/>

    <property name="out" value="out"/>

    <property name="production.src" value="src"/>
    <property name="production.lib" value="lib"/>
    <property name="production.resources" value="config"/>
    <property name="production.classes" value="${out}/production/${ant.project.name}"/>

    <property name="test.src" value="test"/>
    <property name="test.lib" value="lib"/>
    <property name="test.resources" value="config"/>
    <property name="test.classes" value="${out}/test/${ant.project.name}"/>

    <property name="exploded" value="out/exploded/${ant.project.name}"/>
    <property name="exploded.classes" value="${exploded}/WEB-INF/classes"/>
    <property name="exploded.lib" value="${exploded}/WEB-INF/lib"/>

    <property name="reports.out" value="${out}/reports"/>
    <property name="junit.out" value="${reports.out}/junit"/>
    <property name="testng.out" value="${reports.out}/testng"/>

    <path id="production.class.path">
        <pathelement location="${production.classes}"/>
        <pathelement location="${production.resources}"/>
        <fileset dir="${production.lib}">
            <include name="**/*.jar"/>
            <exclude name="**/junit*.jar"/>
            <exclude name="**/*test*.jar"/>
        </fileset>
    </path>

    <path id="test.class.path">
        <path refid="production.class.path"/>
        <pathelement location="${test.classes}"/>
        <pathelement location="${test.resources}"/>
        <fileset dir="${test.lib}">
            <include name="**/junit*.jar"/>
            <include name="**/*test*.jar"/>
        </fileset>
    </path>

    <path id="testng.class.path">
        <fileset dir="${test.lib}">
            <include name="**/testng*.jar"/>
        </fileset>
    </path>

    <available file="${out}" property="outputExists"/>

    <target name="clean" description="remove all generated artifacts" if="outputExists">
        <delete dir="${out}" includeEmptyDirs="true"/>
        <delete dir="${reports.out}" includeEmptyDirs="true"/>
    </target>

    <target name="create" description="create the output directories" unless="outputExists">
        <mkdir dir="${production.classes}"/>
        <mkdir dir="${test.classes}"/>
        <mkdir dir="${junit.out}"/>
        <mkdir dir="${testng.out}"/>
        <mkdir dir="${exploded.classes}"/>
        <mkdir dir="${exploded.lib}"/>
        <mkdir dir="${reports.out}"/>
    </target>

    <target name="compile" description="compile all .java source files" depends="create">
        <!-- Debug output
                <property name="production.class.path" refid="production.class.path"/>
                <echo message="${production.class.path}"/>
        -->
        <javac srcdir="src" destdir="${out}/production/${ant.project.name}" debug="on" source="${version}">
            <classpath refid="production.class.path"/>
            <include name="**/*.java"/>
            <exclude name="**/*Test.java"/>
        </javac>
        <javac srcdir="${test.src}" destdir="${out}/test/${ant.project.name}" debug="on" source="${version}">
            <classpath refid="test.class.path"/>
            <include name="**/*Test.java"/>
        </javac>
    </target>

    <target name="junit-test" description="run all junit tests" depends="compile">
        <!-- Debug output
                <property name="test.class.path" refid="test.class.path"/>
                <echo message="${test.class.path}"/>
        -->
        <junit printsummary="yes" haltonfailure="${haltonfailure}">
            <classpath refid="test.class.path"/>
            <formatter type="xml"/>
            <batchtest fork="yes" todir="${junit.out}">
                <fileset dir="${test.src}">
                    <include name="**/*Test.java"/>
                </fileset>
            </batchtest>
        </junit>
        <junitreport todir="${junit.out}">
            <fileset dir="${junit.out}">
                <include name="TEST-*.xml"/>
            </fileset>
            <report todir="${junit.out}" format="frames"/>
        </junitreport>
    </target>

    <taskdef resource="testngtasks" classpathref="testng.class.path"/>
    <target name="testng-test" description="run all testng tests" depends="compile">
        <!-- Debug output
                <property name="test.class.path" refid="test.class.path"/>
                <echo message="${test.class.path}"/>
        -->
        <testng classpathref="test.class.path" outputDir="${testng.out}" haltOnFailure="${haltonfailure}" verbose="2">
            <classfileset dir="${out}/test/${ant.project.name}" includes="**/*.class"/>
        </testng>
    </target>

    <target name="exploded" description="create exploded deployment" depends="testng-test">
        <copy todir="${exploded.classes}">
            <fileset dir="${production.classes}"/>
        </copy>
        <copy todir="${exploded.lib}">
            <fileset dir="${production.lib}"/>
        </copy>
    </target>

    <target name="package" description="create package file" depends="exploded">
        <jar destfile="${out}/${ant.project.name}.jar" basedir="${production.classes}" includes="**/*.class"/>
    </target>

</project>