【问题标题】:How to set different path in build.xml如何在 build.xml 中设置不同的路径
【发布时间】:2012-02-10 06:41:35
【问题描述】:

我想放置不同目录的路径(包含罐子,目前在d:/ext_jars)。 运行 build.xml 后。我希望该路径应该采用那个可运行的 jar。

由于类路径不存在(之前在c:/project/lib/*.jars 下,转换为D:/ext_jars),目前该jar 给我错误。

请帮助我,如何在 build.xml 的类路径中设置该外部目录?


我的代码在:build.xml

<?xml version="1.0"?>
<project name="MyProject" default="deploy" basedir="." >
    <property file="manifest.mf" />
    <property name="dir.src" value="src" />
    <property name="dir.build" value="bin" />
    <property name="dir.dist" value="dist/MyProject" />
    <property name="dir.lib" value="lib" />
<!-- Creates the output directories -->
    <target name="prepare">
        <mkdir dir="${dir.build}" />
        <mkdir dir="${dir.dist}" />

        <mkdir dir="${dir.dist}/${dir.csvdata}" />
        <mkdir dir="${dir.dist}/${dir.MH}" />
    </target>

    <target name="clean" description="Remove all generated files.">
        <delete dir="${dir.build}" />
        <delete dir="${dir.dist}" />
    </target>

    <path id="build.classpath">
        <fileset dir="${dir.lib}">
            <include name="**/*.jar" />
            <include name="**/*.zip" />
        </fileset>
    </path>
    <path id="build.classpath.ref">
        <fileset dir="D:/ext_jars">
            <include name="**/*.jar" />
            <include name="**/*.zip" />
        </fileset>
    </path>

    <target name="compile" depends="prepare" description="Compile all source code.">
        <!--<javac srcdir="${dir.src}" destdir="${dir.build}" debug="true">-->
        <javac  destdir="${dir.build}" debug="true">
            <classpath refid="build.classpath" />
            <classpath refid="build.classpath.ref" />
            <src path="src"/>           
        </javac>        
        <copy todir="${dir.build}">
            <fileset dir="${dir.src}">
                <exclude name="**/*.java" />
            </fileset>
        </copy>     
    </target>

    <pathconvert property="classpath" refid="build.classpath">
    </pathconvert>
    <pathconvert property="classpath" refid="build.classpath.ref">
    </pathconvert>

    <target name="jar" depends="compile" description="Generates ${project.name}.Jar in the 'dist' directory.">
        <jar jarfile="${dir.dist}/${ant.project.name}.jar">
            <fileset dir="${dir.build}" includes="**/*.*" />
            <manifest>
                <attribute name="Class-Path" value="${Class-Path}" />
                <attribute name="Main-Class" value="${Main-Class}" />
            </manifest>
        </jar>
    </target>
    <target name="deploy" depends="clean,jar">
        <copy todir="${dir.dist}/${dir.lib}">
            <fileset dir="${dir.lib}">
                <include name="**/*.jar" />
                <include name="**/*.zip" />
            </fileset>
        </copy>
    </target>

使用此代码运行项目后运行良好,但运行此代码创建的jar后却没有运行它通过错误如下:

Exception in thread "main" java.lang.NoClassDefFoundError: dowlibpkg/DowLib
        at mypkg.MainApp.main(MainApp.java:109)
Caused by: java.lang.ClassNotFoundException: dowlibpkg.DowLib
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 1 more

所以创建的 Jar 没有运行../ 我希望它运行,任何人都可以帮助我

================================================ ===============

manifest.mf

清单版本:1.0 类路径:lib/org.springframework.asm-3.0.1.RELEASE\lib/org.springframework.beans-3.0.1.RELEASE\lib/org.springframework.context-3.0.1.RELEASE\lib/org。 springframework.core-3.0.1.RELEASE\lib/org.springframework.expression-3.0.1.RELEASE\lib/org.springframework.oxm-3.0.1.RELEASE\lib/org.springframework.web-3.0.1。释放 主类:myProject.MainApp

【问题讨论】:

  • 你指的是Ant任务的build.xml吗?您是指类路径中的路径吗?
  • @ee 是的。我正在使用 Ant 构建运行。
  • 您的意思是要在一组外部 jar 中添加一些 ant 任务?
  • 问题是Ant项目基础是由它的basedir属性定义的,如下:the base directory from which all path calculations are done. This attribute might be overridden by setting the "basedir" property beforehand. When this is done, it must be omitted in the project tag. If neither the attribute nor the property have been set, the parent directory of the buildfile will be used.ant.apache.org/manual/using.html基本上,这意味着Ant项目总是相对于build.xml位于或相对于它的 basedir 属性...
  • 也许,通过在 Ant 元素中设置 relative=false 属性可以覆盖此相对路径行为以成为绝对路径...但是,我不确定...您可能需要检查关于 relative 属性可以在哪个 Ant 元素中找到的 Ant 文档...

标签: java ant classpath relative-path build.xml


【解决方案1】:

根据我在我的 cmets 中提供的链接的一些提示,您可以试试这个(我没有尝试过):

<target depends="init" name="build-projects">
    <property name="myproject.root.path" location="d:/ext_jars"/>

    <path id="class.path">
        <fileset dir="${myproject.root.path}">
            <include name="*.jar"/>
            <exclude name="${ant.project.name}.jar"/>
        </fileset>
    </path>

    <manifestclasspath property="jar.classpath" jarfile="${myproject.root.path}/${ant.project.name}.jar">
        <classpath refid="class.path"/>
    </manifestclasspath>

    <jar destfile="${myproject.root.path}/${ant.project.name}.jar" basedir="classes/app" excludes="*.properties">
        <manifest> 
            <attribute name="Main-Class" value="path.to.my.MainClass" />
            <attribute name="Class-Path" value="${jar.classpath}"/>
        </manifest>
    </jar>
</target>

Ant 任务生成的示例 MANIFEST.MF:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.2
Created-By: 1.6.0_29-b11 (Sun Microsystems Inc.)
Main-Class: path.to.my.MainClass
Class-Path: Filters.jar RXTXcomm.jar collections-generic-4.01.jar colt
 -1.2.0.jar commons-codec-1.5.jar commons-io-2.0.1.jar commons-validat
 or-1.3.1.jar concurrent-1.3.4.jar forms-1.3.0.jar gdal.jar geotiff-ja
 i.jar jai_imageio-1.1.jar jakarta-oro-2.0.1.jar java-image-scaling-0.
 8.5.jar jcommander-1.7.jar jgroups-2.12.1.Final.jar jna.jar jung-algo
 rithms-2.0.1.jar jung-api-2.0.1.jar jung-graph-impl-2.0.1.jar jung-vi
 sualization-2.0.1.jar nimrodlf-1.2.jar platform.jar stax-api-1.0.1.ja
 r swingx-beaninfo-1.6.2.jar swingx-core-1.6.2.jar vecmath-1.3.1.jar v
 lcj-1.2.0.jar wstx-asl-3.2.6.jar

您可以看到 Class-Path 中列出的所有外部 jar 库都遵循主应用程序将使用的 jar 的确切名称。它们都必须与主应用程序的 jar 文件路径位于同一文件路径。主应用程序的类(通常使用 main() 方法)将从 MANIFEST.MF 的 Main-Class 调用,并且其 jar 不得包含在 Class-Path 中。如果一切正确,jar 就可以运行了。

不用担心 MANIFEST.MF 的奇怪格式;由 Ant 任务以 80 列文本样式生成。

【讨论】:

  • 谢谢@ee 这是很好的建议,但仍然发生错误我的当前目录是 C:/projects/ 我想为 jars 提供 D:/ext_jars 的路径,我使用了你的代码sn-ps 但仍然会出现此错误
  • 我提供的上述 Ant 目标 sn-p 要求您修改一些内容,例如提供要为清单文件中的 Main-Class 属性标记的主类路径的实际值(而不是 path.to.my.MainClass!)。如果您不修改它,它将无法正常工作。我已经检查了您最近包含的 build.xml,但您为什么将 元素的值设置为 ${Main-Class}${Class-Path}?你如何定义它们?
  • @eee :我在 manifest.mf 中提到了 ${Main-Class} 和 ${Class-Path},所以我使用了它。
  • @Amit 嗯,我通常让 Ant 任务根据 Ant 任务本身中创建的一些属性变量生成 MANIFEST.MF 文件,以便在 期间包含到 jar 中,如您在我的帖子,而不是相反。
  • @Amit 你能发布你用过的 MANIFEST.MF 吗?由于该文件中的任何错误都不允许 jar 可运行。
【解决方案2】:

有几种方法可以处理 java 类路径,例如使用类路径命令行选项或编辑 CLASSPATH 变量,但基本上你以后为自己创建的问题是你的构建依赖于特定的目录结构。

这使得共享您的代码变得困难,并且肯定会惹恼可能与您合作的其他开发人员。有时最好加倍努力,使您的依赖项在网站上可用(或找到它们已经可用的网站)并让 ant 下载它们,解压缩它们,然后每次都从同一位置将它们添加到类路径中(例如你创建的 build/lib 目录)。

当然,依赖管理是 Maven (http://maven.apache.org) 的全部意义所在,所以也许它也值得你看看。

杰夫

【讨论】:

    猜你喜欢
    • 2012-01-31
    • 2014-10-30
    • 1970-01-01
    • 1970-01-01
    • 2010-11-23
    • 1970-01-01
    • 2020-08-06
    • 1970-01-01
    • 2017-04-22
    相关资源
    最近更新 更多