【问题标题】:Dynamically generate JAR files based on package name with ANT使用 ANT 根据包名动态生成 JAR 文件
【发布时间】:2009-06-05 12:27:15
【问题描述】:

我当前的构建文件有以下重复性任务:

<jar jarfile="${build.lib}/${prefix}-foo.jar">
    <fileset dir="${build.classes}">
        <include name="com/a/c/foo/**"/>
    </fileset>  
</jar>
<jar jarfile="${build.lib}/${prefix}-bar.jar">
    <fileset dir="${build.classes}">
        <include name="com/a/c/bar/**"/>
    </fileset>
</jar>

...等问题是必须为每个新包或每个新子项目修改 build.xml。这在我工作的地方经常发生。

我想将其替换为基于“根”包动态生成 JAR 及其文件名的逻辑。因此,例如,我可以将根包设置为 com/a/c,并且该包下的所有包都将获得自己的 JAR。请注意,“foo”或“bar”下的所有包都只是“foo.jar”或“bar.jar”的一部分。

我查找了 ANT 的循环逻辑任务。我在每个 ant-contrib 和 JWare/AntXtras 中都找到了一个,但我都无法按预期工作。

【问题讨论】:

  • 这似乎是一种奇怪的要求。您能否进一步详细说明为什么单个罐子不适合您的目的?
  • 我同意,这绝对是超出了左侧字段。我们为所有常见的 api 提供了一个“基础 jar”。从那里,每个 JAR 都可以与基本 jar 一起放置在不同的 linux 环境中。此外,我们的大多数客户只想替换一两个包含一小部分应用程序的 jar,而不是替换所有内容。

标签: java ant


【解决方案1】:

我不知道循环和查找所有包名称,但您可以使用宏来避免代码重复。

我没试过,但它可以工作

<macrodef name="build_jar">
    <attribute name="name"/>
    <sequential>
        <jar jarfile="${build.lib}/${prefix}-@{name}.jar">
            <fileset dir="${build.classes}">
                <include name="com/a/c/@{name}/**"/>
            </fileset>
        </jar>
    </sequential
</macrodef>

<target name="build_foo">
    <build_jar name="foo"/>
</target>

<target name="build_bar">
    <build_jar name="bar"/>
</target>

【讨论】:

    【解决方案2】:

    这个怎么样:

    <project name="dynjar" default="jar" basedir=".">
        <property name="build.classes" value="${basedir}/classes"/>
        <property name="build.lib" value="${basedir}/lib"/>
        <property name="prefix" value="prefix"/>
        <property name="root" value="com/a/c"/>
    
        <target name="jar">
            <!-- ${ant.file} is the name of the current build file -->
            <subant genericantfile="${ant.file}" target="do-jar">
    
                <!-- Pass the needed properties to the subant call. You could also use
                     the inheritall attribute on the subant element above to pass all
                     properties. -->
                <propertyset>
                    <propertyref name="build.classes"/>
                    <propertyref name="build.lib"/>
                    <propertyref name="prefix"/>
                    <propertyref name="root"/>
                </propertyset>
    
                <!-- subant will call the "do-jar" target for every directory in the
                     ${build.classes}/${root} directory, making the subdirectory the
                     basedir. -->
                <dirset dir="${build.classes}/${root}" includes="*"/>
            </subant>
        </target>
    
        <target name="do-jar">
            <!-- Get the basename of the basedir (foo, bar, etc.) -->
            <basename file="${basedir}" property="suffix"/>
    
            <jar jarfile="${build.lib}/${prefix}-${suffix}.jar">
                <fileset dir="${build.classes}">
                    <include name="${root}/${suffix}/**"/>
                </fileset>
            </jar>
        </target>
    </project>
    

    【讨论】:

    • 我用它来制作我的罐子,但我有一个问题。你能帮我here吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-15
    • 1970-01-01
    • 2012-11-10
    • 1970-01-01
    • 2012-04-16
    • 2013-07-15
    • 2017-07-20
    相关资源
    最近更新 更多