在类路径中错误的产生,从默认的模板蚂蚁 [英] Error in classpath generation from default-template ant

查看:229
本文介绍了在类路径中错误的产生,从默认的模板蚂蚁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用默认的模板在我的Java项目生成启动脚本的基础上,在我的项目中指定的常春藤依赖关系。我现在面临的问题是常春藤的依赖越来越捞起,放在classpath中,但没有得到包括主体工程的jar在classpath中。(可提供试图运行脚本类未发现异常),在进一步的调查与其他脚本比较,我发现有一个行。

I am trying to use the default-template in my Java project to generate the launch script, based on the ivy dependencies specified in my project. The issue I am facing is that the ivy dependencies are getting picked up and put in the classpath but the main project jar is not getting included in the classpath.(Which gives a class not found exception while trying to run the script) On further investigation and comparison with other scripts I found that there is a line as.

export RUNTIME_CLASSPATH=""

在我生成的脚本。这应该具备的路径,我的项目罐子,但没有得到填充(例如:出口RUNTIME_CLASSPATH =$(目录名称$ 0)/../的lib / myproject.jar:

in my generated script. This should have the path to my projects jar but is not getting populated (ex: export RUNTIME_CLASSPATH="$(dirname $0)/../lib/myproject.jar:")

到底在哪我们指定项目在脚本本身指(主叫项目)?
我曾看着其他所有文件,包括生成属性,爬山虎,常春藤设置无济于事。
这将是巨大的,如果有人可以帮助我找到我丢失的东西。

Where exactly do we specify the project referring itself(calling project) in the script?? I have looked into all other files including the build properties, ivy, ivy settings to no avail. It would be great If someone could help me find what I am missing.

推荐答案

ANT没有产生具体的操作系统启动脚本的标准功能。你可以做的是产生一个可执行的JAR 可以通过运行java启动如下:

ANT doesn't have any standard functionality for generating OS specific launch scripts. What you can do is generate an executable jar which can be launched by running java as follows:

java -jar demo.jar

一个可执行的JAR的主要特点是它的明显的同时指定主类和类路径:

The key feature of an executable jar is it's manifest that specifies both the main class and classpath:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.3
Created-By: 1.6.0_20-b20 (Sun Microsystems Inc.)
Main-Class: org.demo.App
Class-Path: lib/slf4j-api-1.6.4.jar lib/slf4j-simple-1.6.4.jar

下面的ANT项目演示了如何常春藤能在创建可执行的jar文件

The following ANT project demonstrates how ivy can assist in the creation of executable jars

|-- build.xml
|-- ivy.xml
`-- src
    |-- main
    |   `-- java
    |       `-- org
    |           `-- demo
    |               `-- App.java
    `-- test
        `-- java
            `-- org
                `-- demo
                    `-- AppTest.java

运行构建

ant clean build

执行程序

java -jar build/dist/demo.jar

罐包装使用位于一个相对的LIB子目录依赖

jar is packaged to use dependencies located in a relative "lib" subdirectory

build/dist/demo.jar
build/dist/lib/slf4j-api-1.6.4.jar
build/dist/lib/slf4j-simple-1.6.4.jar

的ivy.xml

<ivy-module version="2.0">
    <info organisation="com.myspotontheweb" module="demo"/>

    <configurations>
        <conf name="compile" description="Required to compile application"/>
        <conf name="runtime" description="Additional run-time dependencies" extends="compile"/>
        <conf name="test"    description="Required for test only" extends="runtime"/>
    </configurations>

    <dependencies>
        <!-- compile dependencies -->
        <dependency org="org.slf4j" name="slf4j-api" rev="1.6.4" conf="compile->default"/>

        <!-- runtime dependencies -->
        <dependency org="org.slf4j" name="slf4j-simple" rev="1.6.4" conf="runtime->default"/>

        <!-- test dependencies -->
        <dependency org="junit" name="junit" rev="4.10" conf="test->default"/>
    </dependencies>

</ivy-module>

的build.xml

<project name="demo" default="build" xmlns:ivy="antlib:org.apache.ivy.ant">

    <!--
    ================
    Build properties
    ================
    -->
    <property name="src.dir"          location="src/main/java"/>
    <property name="test.src.dir"     location="src/test/java"/>
    <property name="build.dir"        location="build"/>
    <property name="classes.dir"      location="${build.dir}/classes"/>
    <property name="test.classes.dir" location="${build.dir}/test-classes"/>
    <property name="ivy.reports.dir"  location="${build.dir}/ivy-reports"/>
    <property name="test.reports.dir" location="${build.dir}/test-reports"/>
    <property name="dist.dir"         location="${build.dir}/dist"/>

    <property name="jar.main.class" value="org.demo.App"/>
    <property name="jar.file"       value="${dist.dir}/${ant.project.name}.jar"/>

    <!--
    ===========
    Build setup
    ===========
    -->
    <target name="init">
        <ivy:resolve/>

        <ivy:report todir='${ivy.reports.dir}' graph='false' xml='false'/>

        <ivy:cachepath pathid="compile.path" conf="compile"/>
        <ivy:cachepath pathid="runtime.path" conf="runtime"/>
        <ivy:cachepath pathid="test.path"    conf="test"/>
    </target>

    <!--
    ===============
    Compile targets
    ===============
    -->
    <target name="compile" depends="init">
        <mkdir dir="${classes.dir}"/>
        <javac srcdir="${src.dir}" destdir="${classes.dir}" includeantruntime="false" debug="true" classpathref="compile.path"/>
    </target>

    <target name="compile-tests" depends="compile">
        <mkdir dir="${test.classes.dir}"/>
        <javac srcdir="${test.src.dir}" destdir="${test.classes.dir}" includeantruntime="false" debug="true">
            <classpath>
                <path refid="test.path"/>
                <pathelement path="${classes.dir}"/>
            </classpath>
        </javac>
    </target>

    <!--
    ============
    Test targets
    ============
    -->
    <target name="test" depends="compile-tests">
        <mkdir dir="${test.reports.dir}"/>
        <junit printsummary="yes" haltonfailure="yes">
            <classpath>
                <path refid="test.path"/>
                <pathelement path="${classes.dir}"/>
                <pathelement path="${test.classes.dir}"/>
            </classpath>
            <formatter type="xml"/>
            <batchtest fork="yes" todir="${test.reports.dir}">
                <fileset dir="${test.src.dir}">
                    <include name="**/*Test*.java"/>
                    <exclude name="**/AllTests.java"/>
                </fileset>
            </batchtest>
        </junit>
    </target>

    <!--
    =====================
    Build and run targets
    =====================
    -->
    <target name="build" depends="test">
        <ivy:retrieve pattern="${dist.dir}/lib/[artifact]-[revision](-[classifier]).[ext]" conf="runtime"/>

        <manifestclasspath property="jar.classpath" jarfile="${jar.file}">
            <classpath>
                <fileset dir="${dist.dir}/lib" includes="*.jar"/>
            </classpath>
        </manifestclasspath>

        <jar destfile="${jar.file}" basedir="${classes.dir}">
            <manifest>
                <attribute name="Main-Class" value="${jar.main.class}" />
                <attribute name="Class-Path" value="${jar.classpath}" />
            </manifest>
        </jar>
    </target>

    <target name="run" depends="build">
        <java jar="${jar.file}" fork="true"/>
    </target>

    <!--
    =============
    Clean targets
    =============
    -->
    <target name="clean">
        <delete dir="${build.dir}"/>
    </target>

    <target name="clean-all" depends="clean">
        <ivy:cleancache/>
    </target>

</project>

这篇关于在类路径中错误的产生,从默认的模板蚂蚁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆