繁体   English   中英

如何使用一个构建文件在Ant中构建多个项目?

[英]How can I build multiple projects in Ant with one build file?

我可以从一个构建文件构建多个项目。 例:

<project basedir="." default="all" name="app1">
    ...
</project>

<project basedir="." default="all" name="app2">
    ...
</project>

目前我输入ant -f build1.xml compile并构建我的应用程序,我必须使用两个单独的构建文件。 有没有办法让它以一种方式运行,我有两个项目定义了一个公共的构建文件,我可以键入类似ant app1 compileant app2 compile

这是我的构建文件的样子:

<?xml version="1.0" encoding="UTF-8"?>
<project name="azebooster" default="dist" basedir=".">

    <!-- Globals -->
    <property name="src" location="src/com/aelitis"/>
    <property name="build" location="build/azebooster"/>
    <property name="jar" location="jar/azebooster"/>
    <property name="resources" location="res/azebooster"/>

    <!-- Paths -->
    <path id="classpath">
        <fileset dir="." includes="**/*.jar"/>
    </path>

    <!-- Start it -->
    <target name="init">
      <tstamp/>
      <mkdir dir="${build}"/>
      <mkdir dir="${jar}"/>
    </target>

    <!-- Build it -->
    <target name="compile" depends="init" description="compile the source" >
        <javac srcdir="${src}" destdir="${build}">
            <classpath>
                <path refid="classpath"/>
            </classpath>
        </javac>
    </target>

    <!-- Jar it -->
    <target name="jar" depends="compile">
        <jar destfile="${jar}/${ant.project.name}.jar">
            <fileset dir="${build}"/>
            <fileset dir="${resources}" />
        </jar>
    </target>

    <!-- Clean it -->
    <target name="clean" description="clean up" >
        <tstamp/>
        <delete dir="${build}"/>
        <delete dir="${jar}"/>
    </target>

</project>

谢谢。

是的,您可以创建default.build文件(这样您就不需要指定文件,因为它默认使用)。 在它上面你可以创建以下目标:

<target name="all" depends="app1, app2" />

<target name="app1">
    <ant antfile=<app1file> target="compile" />        
</target>

<target name="app2">
    <ant antfile=<app2file> target="compile" />        
</target>

通过这种方式,您可以使用来自一个唯一文件的两个ant文件。

如果您将app1和app2目标替换为所需的目标,则可以在一个文件中执行所有操作,以便在单独的文件中编译它们。

编辑:

您可以使用不同的方法将它们都包含在一个文件中,也许最简单的方法是为每个目标上的每个项目包含一个后缀。 您可以调用特定的项目目标或两者的目标。

我给你一个编译目标的例子(对于init目标也是如此)。

您可以使用compile编译两个项目(我将其他项目称为其他项目),并使用compileazeboster编译azeboster项目。

您可以搜索常见的东西以避免不必要的重复代码(常见路径,目标等)

<property name="srcazebooster" location="src/com/aelitis"/>
<property name="buildazebooster" location="build/azebooster"/>
<property name="jarazebooster" location="jar/azebooster"/>
<property name="srcother" location="src/other"/>
<property name="buildother" location="build/other"/>
<property name="jarother" location="jar/other"/>


<!-- Start it -->
<target name="init" depends="initazebooster, initother"/>

<!-- Build it -->
<target name="compile" depends="compileazebooster, compileother" description="compile the source for all" />



<!-- Start azebooster-->
<target name="initazebooster">
    <tstamp/>
    <mkdir dir="${buildazebooster}"/>
    <mkdir dir="${jarazebooster}"/>
</target>

<!-- Build azeboster-->
<target name="compileazebooster" depends="initazebooster" description="compile the source for azebooster" >
    <javac srcdir="${srcazebooster}" destdir="${buildazebooster}">
        <classpath>
            <path refid="classpath"/>
        </classpath>
    </javac>
</target>

<!-- Start other-->
<target name="initother">
    <tstamp/>
    <mkdir dir="${buildotherr}"/>
    <mkdir dir="${jarother}"/>
</target>

<!-- Build other-->
<target name="compileother" depends="initother" description="compile the source for other" >
    <javac srcdir="${srcother}" destdir="${buildother}">
        <classpath>
            <path refid="classpath"/>
        </classpath>
    </javac>
</target>

我想补充Borja的一些重要步骤。

如何组织常见的ant文件?

假设我们有一些由许多Eclipse项目组成的大项目。 所有这些都在一个恶劣的空间文件夹中。 因此,工作空间本身就构成了全局项目。 (注意P / p差异)。 通常,您在工作区文件夹中已经有一个Ant构建文件。 或者您自己创建了常见的ant文件(构建和属性)。

稍后您要启动该全局构建文件。 怎么样? 你想进入工作区文件夹,更改为命令行或shell窗口并从那里运行ant,在外部控制台中输出并从Eclipse切换到该窗口并返回? 和编辑相同的问题? 有两个额外的窗户? 不,你肯定想在Eclipse中拥有所有窗口,编辑和输出。 但是我们如何从Eclipse内部引用这些全局构建文件?

  • 转到Package Explorer
  • 创建一个空项目(不是Java项目),让它命名为_Global(我们希望它始终位于顶部)。
  • 右键单击其名称。 选择Import - >常规 - >文件系统。
  • Advanced 选中Create links in WorkspaceCreate virtual folderscreate link locations 最后选择WORKSPACE_LOC
  • 在“ From Directory转到工作区或创建公共构建的位置。 毕竟,它甚至可能是几个工作空间的常见工具。
  • 您将在右侧看到带有复选字段的文件名对话框。 检查所需的文件。 (您需要导入之后创建的每个新公共文件)。
  • Finish

现在您看到您的项目引用了全局文件。 至于属性文件,您已经准备好了。 但是对于构建文件,您还需要更多步骤:

  • 右键单击全局构建文件, - > Run - > Run Configurations 您处于Ant Build组配置中。
  • 按下带有加号的“ New Launch Configuration按钮(在启动配置列表上方)。 现在您拥有该全局构建的运行配置。
  • 设置其参数并运行/调试它。

您可能能够使用宏执行您想要的操作(尽管它完全取决于您的两个项目之间的共同点),尽管您的命令更可能是

ant compile app1

要么

ant compile app2

其中compile是一个调用宏的目标,使用app1 / app2作为参数来决定调用哪个宏或传递给宏本身。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM