如何在build.xml中设置不同的路径

发布于 2025-01-04 19:12:31 字数 4395 浏览 2 评论 0原文

我想放置不同目录的路径(其中包含 jar,目前位于 d:/ext_jars 上)。 运行build.xml后。我希望该路径应该采用可运行的 jar。

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

请帮助我,如何在 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

SO 创建的 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.RELEASE 主类:myProject.MainApp

I want to put path of different directory (which contains jars, presently on d:/ext_jars).
After running the build.xml. I want that path should take that runnable jar.

Currently that jar is giving me errors because of classpath is not present (which was previously under c:/project/lib/*.jars, converted into D:/ext_jars).

Please help me, that how can I set that external directory in classpath of build.xml?


My code in : 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>

Using this code After runing project it is running well, But after running jar created by this code it is not running it is throughing error as follows:

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

SO created Jar is not running../ I want it to be run, can any one please help me

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

manifest.mf

Manifest-Version: 1.0
Class-Path: 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.RELEASE
Main-Class: myProject.MainApp

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

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

发布评论

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

评论(2

你是暖光i 2025-01-11 19:12:31

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

<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

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

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

Based on some hints from the links I have provided in my comments, you can try this (which I haven't tried):

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

Example MANIFEST.MF generated by Ant task:

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

As you can see all external jar libraries listed in the Class-Path follow the exact name of the jars that the main application will use. All of them must be at the same file path as the main application's jar file path. The main application's class (usually with the main() method) will be called from the Main-Class of the MANIFEST.MF and its jar must not be included in the Class-Path. If everything is correct, the jar will be runnable.

Don't worry about the weird formatting of the MANIFEST.MF; it is generated in 80-column text style by the Ant task.

飘过的浮云 2025-01-11 19:12:31

有多种方法可以修改 java 类路径,例如使用类路径命令行选项或编辑 CLASSPATH 变量,但从根本上讲,您稍后为自己创建的问题是您的构建变得依赖于特定的目录结构。

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

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

杰夫

There are several ways to fiddle with the java classpath like using classpath command line options or editing the CLASSPATH variable but fundamentally the issue you create for yourself later is that your build becomes dependent upon a certain directory structure.

This makes it hard to share your code and definitely annoys other developers that may work with you. Sometimes its better to go the extra mile and make your dependencies available on a website (or find a website they are already available on) and get ant to download them, unzip them and then add them to the classpath from the same location every time (eg build/lib directory you create).

Of course dependency management is what Maven (http://maven.apache.org) is all about so maybe its also worth a look for you.

Jeff

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