是否有任何 Ant 功能可以将类路径依赖项复制到 WEB-INF/lib? [英] Is there any Ant feature, that copies classpath dependencies to WEB-INF/lib?

查看:17
本文介绍了是否有任何 Ant 功能可以将类路径依赖项复制到 WEB-INF/lib?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法影响将类路径依赖项复制到 WEB-INF/lib 类别的过程:没有复制这些 jar 的特殊 ANT 任务(至少,我找不到任何与WEB-INF"相关的复制"任务/lib" 字符串作为 PATH 参数),但它们在项目构建后出现.如何影响这个程序?基本上,我需要排除 JAXB jar 以避免依赖冲突.同时我在编译时需要这个罐子,所以我不能删除它们.也许,使用删除"任务手动擦除这些罐子更容易?

I can't affect the procedure of copying classpath dependencies to WEB-INF/lib category: there is no special ANT task that copies those jars (at least, i cant find any 'copy' task with related "WEB-INF/lib" string as PATH argument), but they appeared after project building. How to affect this procedure? Basically, i need to exclude JAXB jars to avoid dependency conflict. At the same time i need this jars at compile-time, so i can't remove them. Maybe, it is easier to erase those jars manually, using 'delete' task?

推荐答案

您遇到的难题是多类路径管理.在典型的构建中,至少有 4 种类路径:

What your struggling with is multiple classpath management. In a typical build there are at least 4 types of classpath:

  • compile:您的代码直接调用的类
  • 运行时:您的代码通过其他类间接调用的类
  • provided:您需要编译的类,但其实现将由目标平台提供
  • test:测试代码时需要但未随最终应用程序一起提供的其他类(如 junit)
  • compile: Classes that your code directly invokes
  • runtime: Classes that your code indirectly invokes via other classes
  • provided: Classes that you need to compile against, but whose implementation will be provided by the target platform
  • test: Additional classes (like junit) that are needed when you're testing code but which are not shipped with your final application

正是 Maven 构建工具正式识别了这些常见的类路径,并提供了一个依赖管理系统来解决和在构建过程中填充类路径.

It was the Maven build tool which formally identified these common classpaths and provided a dependency management system for resolving and populating classpaths during the build process.

坏消息是 ANT 早于 Maven,因此将类路径管理完全留给程序员.... 通常这是通过将 jars 放入不同的目录或在构建逻辑中使用复杂的文件集来完成的.

The bad news is that ANT pre-dates Maven and therefore leaves classpath management completely up to the programmer.... Typically this is done by putting jars into different directories or using complicated filesets within your build logic.

好消息是有一个名为 ivy 的 ANT 插件,它执行类似 Maven 的依赖管理.值得学习,特别是如果您使用开源库(现在越来越多地使用 Maven)进行大量编程.

The good news is that there is an ANT plugin called ivy which performs Maven-like dependency management. It's worth learning, especially, if you program a lot with open source libraries (which increasingly use Maven now).

组成各个类路径的文件必须在构建的顶部进行管理.显然,这些文件必须单独下载到lib"目录中.随着文件数量的增加,这种方法变得笨拙.

The files which make up the individual classpaths must be managed at the top of the build. Obviously the files must be separately downloaded into the "lib" directory. As the number of files increases this approach becomes unwieldy.

<project name="demo" default="build">

    <!-- 
    ================
    File collections 
    ================
    -->
    <fileset dir="lib" id="compile.files">
        <include name="*.jar"/>
        <exclude name="slf4j-log4j12.jar"/>
        <exclude name="log4j.jar"/>
        <exclude name="junit.jar"/>
        <exclude name="hamcrest-core.jar"/>
    </fileset>

    <fileset dir="lib" id="runtime.files">
        <include name="*.jar"/>
        <exclude name="junit.jar"/>
        <exclude name="hamcrest-core.jar"/>
    </fileset>

    <fileset dir="lib" id="test.files">
        <include name="*.jar"/>
    </fileset>

    <!--
    ===============
    Compile targets
    ===============
    -->
    ..
    ..

    <target name="compile" depends="init,resolve, resources" description="Compile code">
        <mkdir dir="${classes.dir}"/>
        <javac srcdir="${src.dir}" destdir="${classes.dir}" includeantruntime="false" debug="true">
            <classpath>
                <fileset refid="compile.files"/>
            </classpath>
        </javac>
    </target>

    <!--
    ===============
    Distribution targets
    ===============
    -->
    ..
    ..

    <target name="package" depends="test" description="Create the WAR file">
        <copy todir="build/lib">
            <fileset refid="runtime.files"/>
        </copy>

        <war destfile="${war.file}" webxml="${resources.dir}/web.xml">
            <fileset dir="${resources.dir}" excludes="web.xml"/>
            <lib dir="${build.dir}/lib"/>
        </war>
    </target>

示例(使用常春藤)

对 ivy 及其任务的高层次介绍.请参阅下面的检索"常春藤任务,该任务提供了您正在寻找的功能.

Example (Using ivy)

Very high level introduction to ivy and it's tasks. See the "retrieve" ivy task below which delivers the functionality you're looking for.

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

    <!--
    ===========
    Build setup
    ===========
    -->
    <target name="bootstrap" description="Install ivy">
        <mkdir dir="${user.home}/.ant/lib"/>
        <get src="http://search.maven.org/remotecontent?filepath=org/apache/ivy/ivy/2.2.0/ivy-2.2.0.jar"
             dest="${user.home}/.ant/lib/ivy.jar"/>
    </target>

    <!--
    ============================
    Resolve project dependencies
    ============================
    -->
    <target name="resolve" description="Use ivy to resolve classpaths">
        <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,resolve, resources" description="Compile code">
        <mkdir dir="${classes.dir}"/>
        <javac srcdir="${src.dir}" destdir="${classes.dir}" includeantruntime="false" debug="true" classpathref="compile.path"/>
    </target>

    <!--
    ===============
    Distribution targets
    ===============
    -->
    ..
    ..

    <target name="package" depends="test" description="Create the WAR file">
        <ivy:retrieve pattern="${build.dir}/lib/[artifact].[ext]" conf="runtime"/>

        <war destfile="${war.file}" webxml="${resources.dir}/web.xml">
            <fileset dir="${resources.dir}" excludes="web.xml"/>
            <lib dir="${build.dir}/lib"/>
        </war>
    </target>

注意事项

  • bootstrap"目标旨在安装 ivy(它没有与 ANT 核心一起打包)
  • cachepath"任务用于创建自定义 ANT 路径
  • 检索"任务使用运行时所需的 jar 填充 WAR 文件的 WEB-INF/lib 目录(由 ivy 配置管理)

此文件列出了项目的依赖项.它使用配置将 jar 逻辑组合在一起,并使 ivycachpath"任务能够在您的构建中创建匹配的类路径.最后,在构建过程中下载并缓存第 3 方 jar.这非常方便,这意味着您可以减少项目的大小.

This file lists your project's dependencies. It uses configurations to logically group jars together and enables the ivy "cachpath" task to create matching classpaths within your build. Finally the 3rd party jars are downloaded and cached during the build process. This is very convenient and it means you can reduce the size of your project.

<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.7.2" conf="compile->default"/>

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

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

</ivy-module>

这篇关于是否有任何 Ant 功能可以将类路径依赖项复制到 WEB-INF/lib?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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