[英]How can I build multiple projects in Ant with one build file?
Can I build multiple projects from one build-file. 我可以从一个构建文件构建多个项目。 Example:
例:
<project basedir="." default="all" name="app1">
...
</project>
<project basedir="." default="all" name="app2">
...
</project>
Currently I type ant -f build1.xml compile
and it builds my application and I have to use two separate build files. 目前我输入
ant -f build1.xml compile
并构建我的应用程序,我必须使用两个单独的构建文件。 Is there some way to get it running in a way that i have both the projects defined a common build-file and I can type something like ant app1 compile
or ant app2 compile
? 有没有办法让它以一种方式运行,我有两个项目定义了一个公共的构建文件,我可以键入类似
ant app1 compile
或ant app2 compile
?
Here's what my build-file looks like: 这是我的构建文件的样子:
<?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>
Thank you. 谢谢。
Yes you can create a default.build file (in this way you don't need to specify the file, because it's used by default). 是的,您可以创建default.build文件(这样您就不需要指定文件,因为它默认使用)。 On it you can create the following targets:
在它上面你可以创建以下目标:
<target name="all" depends="app1, app2" />
<target name="app1">
<ant antfile=<app1file> target="compile" />
</target>
<target name="app2">
<ant antfile=<app2file> target="compile" />
</target>
On this way you can use both ant file from one unique file. 通过这种方式,您可以使用来自一个唯一文件的两个ant文件。
And you can do all in only one file, if you replace the app1 and app2 targets, with the needed targets to compile them that you have in the separate files. 如果您将app1和app2目标替换为所需的目标,则可以在一个文件中执行所有操作,以便在单独的文件中编译它们。
EDITED: 编辑:
You have different ways to include both of them in only one file, maybe the easiest one is include a suffix for each project on each target. 您可以使用不同的方法将它们都包含在一个文件中,也许最简单的方法是为每个目标上的每个项目包含一个后缀。 And you can call the specific project target or the target for both.
您可以调用特定的项目目标或两者的目标。
I put you an example with the compile target (and for init target too). 我给你一个编译目标的例子(对于init目标也是如此)。
You can use compile for compile both projects (I call the other project other), and compileazeboster to compile the azeboster project. 您可以使用compile编译两个项目(我将其他项目称为其他项目),并使用compileazeboster编译azeboster项目。
You can search the common things to avoid the innecesary duplicated code (common paths, targets and so on) 您可以搜索常见的东西以避免不必要的重复代码(常见路径,目标等)
<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>
I would like to add to the excellent answer of Borja some important steps. 我想补充Borja的一些重要步骤。
Let's say we have some big Project consisting of many Eclipse projects. 假设我们有一些由许多Eclipse项目组成的大项目。 All of them are in a worspace folder.
所有这些都在一个恶劣的空间文件夹中。 Thus, the workspace itself makes the global Project.
因此,工作空间本身就构成了全局项目。 (Notice P/p difference).
(注意P / p差异)。 Very often you already have an Ant build file in the workspace folder.
通常,您在工作区文件夹中已经有一个Ant构建文件。 Or you have created the common ant files (build and properties) in it by yourself.
或者您自己创建了常见的ant文件(构建和属性)。
Later you want to launch that global build file. 稍后您要启动该全局构建文件。 How?
怎么样? Do you want to go into the workspace folder, change to the command line or shell window and run the ant from there, having the output in the external console and switching from Eclipse to that window and back?
你想进入工作区文件夹,更改为命令行或shell窗口并从那里运行ant,在外部控制台中输出并从Eclipse切换到该窗口并返回? And the same problem for editing?
和编辑相同的问题? Having two additional windows?
有两个额外的窗户? No, surely you want to have all windows, editing and output, in Eclipse.
不,你肯定想在Eclipse中拥有所有窗口,编辑和输出。 But how can we reference those global build files from inside the Eclipse?
但是我们如何从Eclipse内部引用这些全局构建文件?
Package Explorer
. Package Explorer
。 Import
-> General -> File System. Import
- >常规 - >文件系统。 Advanced
. Advanced
。 Check Create links in Workspace
, Create virtual folders
, create link locations
. Create links in Workspace
, Create virtual folders
, create link locations
。 The last choose for WORKSPACE_LOC
. WORKSPACE_LOC
。 From Directory
go to the workspace or where you have created the common build. From Directory
转到工作区或创建公共构建的位置。 After all, it could be even the common one for several workspaces. Finish
. Finish
。 Now you see your project with references to the global files. 现在您看到您的项目引用了全局文件。 As for properties files, you are ready.
至于属性文件,您已经准备好了。 But for build files you need some more steps:
但是对于构建文件,您还需要更多步骤:
Run
-> Run Configurations
. Run
- > Run Configurations
。 You are in the Ant Build group of configurations. New Launch Configuration
button with plus on it (above the launch configurations list). New Launch Configuration
按钮(在启动配置列表上方)。 Now you have the run configuration for that global build. You will probably be able to do what you want using macros (though it depends precisely on what is common between your two projects), though your command is more likely to be 您可能能够使用宏执行您想要的操作(尽管它完全取决于您的两个项目之间的共同点),尽管您的命令更可能是
ant compile app1
or 要么
ant compile app2
where compile is a target which calls a macro, using app1/app2 as a parameter to either decide which macro to call or to pass into the macro itself. 其中compile是一个调用宏的目标,使用app1 / app2作为参数来决定调用哪个宏或传递给宏本身。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.