【问题标题】:Ant script to choose between multiple version of classpaths用于在多个版本的类路径之间进行选择的 Ant 脚本
【发布时间】:2012-06-03 10:05:54
【问题描述】:

我是 Ant 脚本的新手。

以下是需求说明

在我的工作区中,有各种项目,我必须让我的项目在 RAD 和 eclipse IDE 以及 Websphere、tomcat 和 jboss 环境中运行。我已经进行了项目特定设置以使项目在 RAD 和 websphere 上运行还有 eclipse 和 tomcat n jboss..

但是有几个文件有变化,比如类路径 n 几个配置文件。

这给我留下了三个版本的工作区。

但我的想法是让一个工作区具有多个版本的类路径,例如。 classpath_eclipse、classpath_rad 等.. 并有一个 ant 脚本,它将在构建期间根据哪个 ide 在正确的文件之间进行选择。

所以请大家提出一些方法,我该如何实施这种方法。对蚂蚁来说是全新的。 .:/

【问题讨论】:

  • 想要管理 IDE 环境或目标构建环境?详细信息:类路径在 Eclipse 和 ANT 中的管理方式不同......您希望 ANT 为您生成“.classpath”文件吗?或者是否需要管理与不同目标环境关联的不同类路径?例如Tomcat可能没有将所有的jar包都与websphere一起打包?
  • 我必须让我的工作区在两个 IDEs Eclipse as well as RAD. so I want ANT to generate the ".classpath" files. Yes the requirement for different classpaths is associated with different target environments as well as IDE. the dependency on environment is as u said. on IDE it occurs because i have some project related plugins that are RAD compatible but not eclipse. those plugin provide me with a set of library in RAD. whereas in eclipse i have to create a "User Library". so requirement to manage different classpaths is associated with different target environments as well as IDEs 上工作

标签: eclipse ant rad


【解决方案1】:

我建议使用Apache ivy 来管理复杂的类路径。它将您的构建依赖项外部化到一个单独的 ivy.xml 文件中。

其次,ivy 可以自动下载此类依赖项,从而在源代码控制下减少项目的大小。

最后,乍一看,这个解决方案可能看起来非常复杂。它的优点是它与其他构建技术兼容,例如Maven。

示例

ivy.xml

Ivy 使用“配置”来管理 jar 的逻辑分组。

在此示例中,代码针对 SLF4J api jar 进行编译,但在运行时使用不同的日志记录实现:

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

    <configurations>
        <conf name="compile" description="Required to compile application"/>
        <conf name="runtime.simple"  description="Runtime environment with minimal logging" extends="compile"/>
        <conf name="runtime.complex" description="Runtime environment with logback enabled" extends="compile"/>
        <conf name="test"    description="Required for test only" extends="runtime.simple"/>
        <conf name="build"   description="ANT tasks used by build"/>
    </configurations>

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

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

        <!-- complex runtime dependencies -->
        <dependency org="ch.qos.logback" name="logback-classic" rev="1.0.3" conf="runtime.complex->default"/>

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

        <!-- Build dependencies -->
        <dependency org="org.codehaus.groovy" name="groovy-all" rev="1.8.6" conf="build->default"/>
    </dependencies>

</ivy-module>

注意事项:

  • extends 属性允许创建 jar 联合集
  • 默认情况下,ivy 将从Maven Central(一个开放的存储库,现在托管大约 90% 的 Java 开源软件)下载。
  • 使用 conf 属性,您可以将依赖关系与一个或多个本地定义的配置相关联。
  • Ivy 还可用于管理 3rd 方 ANT 任务依赖项

build.xml

ivy ANT 任务被导入为antlib。 ivy cachepath 任务用于将 ivy 托管配置转换为正常的 ANT paths 并且 ivy report 任务生成依赖关系报告。

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

    <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.simple.path" conf="runtime.simple"/>
        <ivy:cachepath pathid="runtime.complex.path" conf="runtime.complex"/>
        <ivy:cachepath pathid="test.path"    conf="test"/>
        <ivy:cachepath pathid="build.path"   conf="build"/>
    </target>
    ..
    ..

ivy retrieve 任务用于在应用程序的打包阶段填充目录:

<target name="war">
    <ivy:retrieve pattern="${build.dir}/libs/[artifact].[ext]" conf="runtime.complex"/>

    <war destfile="myapp.war" webxml="src/metadata/myapp.xml">
        <fileset dir="${src.dir}/html/myapp"/>
        <fileset dir="${src.dir}/jsp/myapp"/>
        <lib dir="${build.dir}/libs"/>
        <classes dir="${build.dir}/classes"/>
    </war>
</target>

IDE 配置文件

常春藤的Eclipse plugin 可用。

还可以使用嵌入式groovy 任务生成 IDE 配置文件。以下是一个 Eclipse 示例:

<target name="eclipse">
    <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>

    <ivy:cachefileset setid="libfiles" conf="compile"/>

    <groovy>
    <arg value="${src.dir}"/>
    <arg value="${build.dir}/classes"/>

    import groovy.xml.MarkupBuilder

    //
    // Generate the project file
    //
    project.log("Creating .project")

    new File(".project").withWriter { writer ->
        def xml = new MarkupBuilder(writer)

        xml.projectDescription() {
            name(project.name)
            comment()
            projects()
            buildSpec() {
                buildCommand() {
                    name("org.eclipse.jdt.core.javabuilder")
                    arguments()
                }
            }
            natures() {
                nature("org.eclipse.jdt.core.javanature")
            }
        }
    }

    //
    // Generate the classpath file
    //
    // The "lib" classpathentry fields are populated using the ivy artifact report
    //
    project.log("Creating .classpath")

    new File(".classpath").withWriter { writer ->
        def xml = new MarkupBuilder(writer)

        xml.classpath() {
            classpathentry(kind:"src",    path:args[0])
            classpathentry(kind:"output", path:args[1])
            classpathentry(kind:"con",    path:"org.eclipse.jdt.launching.JRE_CONTAINER")

            project.references.libfiles.each {
                classpathentry(kind:"lib", path:it)
            }
        }
    }
    </groovy>        
</target>

【讨论】:

  • 嘿,马克,我知道要再次问同样的问题,这个论坛的新手.. appologies.
  • 使用 ivy 和 groovy 看起来相当复杂,ill have to redo all current implementation.. could u suggest some approach to implement what i asked in that second question ? because thats how the architect has suggested and the approach has been approved.. ill 必须走很长的路才能提出一种新方法.. :(
  • 您要解决的问题并非微不足道。首先,您尝试支持两种不同构建工具的多个实例。每个都有不同的文件格式。其次,实际上你是在创建自己的依赖管理系统......我见过非常大的 ANT 项目使用属性文件来管理模块类路径......它最终成为一个或两个构建工程师会理解的定制解决方案......再次考虑使用常春藤。最后还有一个用于与 eclipse 交互的 ivy 插件。这可能会避免对时髦的东西的需要。
  • 嘿,马克..我明白你在说什么。准备了一份草稿,并向我的团队和架构师提出了更改。我被建议(和告诉:/)我们不打算修改构建过程。我们只需要用户第一次运行的一些脚本来设置类路径和运行时以及其他设置等。之后我们可以使用我们通常的构建过程。我对 ivy 进行了挖掘,它看起来很酷。但不能对使用它做任何事情。感谢您向我介绍这项技术。 -问候穆库尔
  • 嗨,马克,正如我所暗示的,我必须使用以前的方法。这些是我需要处理的事情:类路径和运行时配置。您能否提供一些指导方针。谢谢-Mukul
【解决方案2】:

我想分享一下我最终实施的方法。

有classpath、settings 和一些project config xmls 依赖于运行时。

在每个项目中,我们为每个文件创建了 runtime_classpah & runtime_settings 和 configxml_runtime 版本。

在ant 中创建了一个target,将runtime 作为参数,遍历每个项目并将classpath_runtime 的内容复制到classpath,setting_runtime to settings。

还有一个目标用configxml_runtime 的内容覆盖configxml

【讨论】:

  • 这是依赖管理框架旨在为您解决的问题。
  • @MarkO'Connor 你说得对,先生,正如我提到的,我确实读过ivy,我喜欢它。但选择的决定不在我手中,以上是我建议使用的方法,考虑到常春藤所需的努力和时间交付限制。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-19
  • 2012-10-11
相关资源
最近更新 更多