http://bobcat.webappcabaret.net/javachina/faq/ant_01.htm#ant_mid_Q100
官网
http://ant.apache.org/faq.html#precompile-jsps
FAQ on ANT Building Process by Roseanne Zhang
The questions and answers here are my selective postings on jGuru , JavaRanch , ChinaJavaWorld ProgramFan in order to answer others' questions. After a while of doing this, I realized that a lot of similar questions are being asked again and again.
The purpose of creating this page is to let people in the future have a place to look for answers. People don't need to answer the same question again and again.
Software reusability is extremely important. OO analysis, design, and programming are invented for this purposes. So does the component-based software. Design patterns are discovered for reusing other's design ideas. This page is created for reusing my own answers. Hopefully, this page will make your learning process a little easier.
We all work together to make a difference!
Ant ABC
Just kidding! ANT is a Java based building tool, which is similar to make , and so much better than make .
ANT, what a smart name for a building tool, even the original author of ANT, James Duncan Davidson, meant "Another Neat Tool".
Ant is easy!
The hard part is how to make a very complicated diversified system work very simple and elegant. Knowledge about ant is not enough, you need an elegant and simple design, you need great naming convention, you need to optimize the code reusability and flexibility, you need a least maintenance system...
Then it is not easy now ...
- Download the most recent version of ant from Apache ; unzip it some where on your machine.
- Install j2sdk 1.4 or above.
- Set JAVA_HOME and ANT_HOME to the directory your installed them respectively.
- Put %JAVA_HOME%/bin;%ANT_HOME%/bin on your Path. Use ${JAVA_HOME}/bin:${ANT_HOME}/bin on UNIX. Yes, you can use forward slash on windows.
- Write a "Hello world" build.xml
<project name="hello" default="say.hello" basedir="." > <property name="hello.msg" value="Hello, World!" /> <target name="say.hello" > <echo>${hello.msg}</echo> </target> </project>
- Type ant in the directory your build.xml located.
- You are ready to go!!!!
<delete quiet="true" dir="${classes.dir}" includes="*.class"/>
- You should not use implicit fileset, which is deprecated. You should use nested fileset.
- If dir does not exist, the build will fail, period!
- If you are not sure, use a upper level dir, which exists for sure. See the following fileset.
<delete> <fileset dir="${upperdir.which.exists}"> <include name="${classes.dir}/*.class" /> </fileset> </delete>
<path id="build.classpath"> <fileset dir="${build.lib}" includes="**/*.jar"/> <fileset dir="${build.classes}" /> </path> <target....> <javac ....> <classpath refid="build.classpath" /> </java> </target> <target....> <java ....> <classpath refid="build.classpath" /> </java> </target>
Store your password in your ${user.home}/prj.properties
pswd=yourrealpassword
In your include directory master prj.properties
pswd=password
In your build-common.xml read properties files in this order
- The commandline will prevail, if you use it: ant -Dpswd=newpassword
- ${user.home}/prj.properties (personal)
- yourprojectdir/prj.properties (project team wise)
- your_master_include_directory/prj.properties (universal)
Problem solved!
<taskdef resource="net/sf/antcontrib/antcontrib.properties" />
Believe me or not? Forward slash works on windows in all ant or java code. It also works in windows environment variables. It does not work in cmd (dos) window before XP. It also works in XP dos window now!
<fileset id="fs1" dir="t1" includes="**/*.java"/> <property name="f1.contents" refid="fs1"/> <echo>f1.contents=${f1.contents}</echo>
Ant Intermediate
<target name="run" depends="some.target,some.other.target"> <java classname="${run.class}" fork="yes"> <classpath> <path refid="classpath" /> </classpath> <jvmarg line="${debug.jvmargs}" /> <jvmarg line="${my.jvmargs}" /> <jvmarg value="-Dname=${name}" /> <jvmarg line="${run.jvmargs}" /> <arg line="${run.args}" /> </java> </target>
- Do an echo on where you have doubt. You will find out what is the problem easily. Just like the old c printf() or Java System.println()
- Use project.log("msg") in your javascript or custom ant task
- Run Ant with -verbose, or even -debug, to get more information on what it is doing, and where. However, you might be tired with that pretty soon, since it give you too much information.
Ant Intermediate
<copy todir="${to.dir}" > <fileset dir="${from.dir}" > <exclude name="dirname1" /> <exclude name="dirname2" /> <exclude name="abc/whatever/dirname3" /> <exclude name="**/dirname4" /> </fileset> </copy>
ExecTask compile = (ExecTask)project.createTask("exec");
- You don't need to unzip the files from archive to put into your destination jar/ear/war files.
- You can use zipfileset in your jar/war/ear task to extract files from old archive to different directory in your new archive.
- You also can use zipfileset in your jar/war/ear task to send files from local directory to different directory in your new archive.
<jar destfile="${dest}/my.jar"> <zipfileset src="old_archive.zip" includes="**/*.properties" prefix="dir_in_new_archive/prop"/> <zipfileset dir="curr_dir/abc" prefix="new_dir_in_archive/xyz"/> </jar>
compile: [javac] Warning: commons-logging.properties modified in the future. [javac] Warning: dao\DAO.java modified in the future. [javac] Warning: dao\DBDao2.java modified in the future. [javac] Warning: dao\HibernateBase.java modified in the future.
- You changed the system time
- I had the same problem before, I checked out files from cvs to windows, and transfer them to a unix machine, somehow, I got huge amount of such warnings because the system timing issue.
- If you transfer files from Australia/China/India to the United States, you will get the problem too. True enough, I did and met the problem once.
In your own $ANT_HOME/docs/manual directory, there also is tutorial-writing-tasks-src.zip
Use them! Use taskdef to define it in your script, define it before using it.
<include name="a,b,c"/>If files are in the directory or subdirectories:
<include name="**/a,**/b,**/c"/>If you want all files without extension are in the directory or subdirectories:
<exclude name="**/*.*"/>
- Don't define java.home by yourself. Ant uses an internal one derived from your environment var JAVA_HOME. It is immutable.
- I do the followings:
- In my build.properties (read first)
jdk13.bin=${tools.home}/jdk1.3.1_13/bin
jdk14.bin=${tools.home}/j2sdk1.4.2_08/bin/ - In my master properties file (read last), set default
javac.location=${jdk13.bin} - In my prj.properties, if I need to use 1.4
javac.location=${jdk14.bin} - in my javac task
executable="${javac.location}/javac.exe"
- In my build.properties (read first)
<compilerarg value="-Xlint"/> <!-- or --> <compilerarg value="-Xlint:unchecked"/>
<xslt style="${xslfile}" in="${infile}" out="${outfile}" > <classpath> <fileset dir="${xml.home}/bin" includes="*.jar" /> </classpath> </xslt>
- Since target if/unless all depend on some property is defined or not, you can use condition to define different NEW properties, which in turn depends on your ant property values. This makes your ant script very flexible, but a little hard to read.
- Ant-contrib has <if> <switch> tasks for you to use.
- Ant-contrib also has <propertyregex> which can make very complicate decisions.
Ant advanced
<property file="${antutil.includes}/${os.name}-${os.arch}.properties" />This will auto-detect your platform, and you write one file for each environment specific variables. For example: HP-UX-PA_RISC2.0.properties SunOS-sparc.properties Windows XP-x86.properties ... They work great!!! :)
TimedBufferedReader.java
package setup; import java.io.Reader; import java.io.BufferedReader; import java.io.IOException; /** * Provides a BufferedReader with a readLine method that * blocks for only a specified number of seconds. If no * input is read in that time, a specified default * string is returned. Otherwise, the input read is returned. * Thanks to Stefan Reich * for suggesting this implementation. * @author: Anthony J. Young-Garner * @author: Roseanne Zhang made improvement. */ public class TimedBufferedReader extends BufferedReader { private int timeout = 60; // 1 minute private String defaultStr = ""; /** * TimedBufferedReader constructor. * @param in Reader */ TimedBufferedReader(Reader in) { super(in); } /** * TimedBufferedReader constructor. * @param in Reader * @param sz int Size of the input buffer. */ TimedBufferedReader(Reader in, int sz) { super(in, sz); } /** * Sets number of seconds to block for input. * @param seconds int */ public void setTimeout(int timeout) { this.timeout=timeout; } /** * Sets defaultStr to use if no input is read. * @param str String */ public void setDefaultStr(String str) { defaultStr = str; } /** * We use ms internally * @return String */ public String readLine() throws IOException { int waitms = timeout*1000; int ms = 0; while (!this.ready()) { try { Thread.currentThread().sleep(10); ms += 10; } catch (InterruptedException e) { break; } if (ms >= waitms) { return defaultStr; } } return super.readLine(); } }
Windose makes a lot of decisions for you, even it is not always correct.
I was struggling with similar problems these two days. When ant cannot do it, then try use your hand to delete it, if you cannot, it is clear it is "Windose".
How to fight with your "Windose"? Try the followings in order:
- Check/disable your anti-virus software.
- Close all possible apps use that file/dir.
- Close cmd or cygwin windows
- Close Windows Explorer, then reopen.
- If still not work, shut-down your PC, count 1 to 10, then start again. It worked.
Haha!
Dev |_src | |_com | |_mycom | |_mypkg | |_A.java |_test |_src |_com |_mycom |_mypkg |_ATest.java
- Quilt here
- Copied from Quilt site: Quilt is a Java software development tool that measures coverage , the extent to which unit testing exercises the software under test. It is optimized for use with the JUnit unit test package, the Ant Java build facility, and the Maven project management toolkit.
- Interesting!!
We wrote something similar to the functionality of quilt long time ago, when I was working in a Microsoft Solution Provider shop. We actually could tell how QA did their job. However, it was considered spying on QA, and for keeping the reasonable relationship between development and QA, we were forced to take them out.
It would be better if it were a third party product, in my case.
- Took a look, we 'd be better off not to use it. The reasons are:
- It is stale, it stopped developing in 2003.
- It is dangerous, since it alters the bitecode.
lang=en lang=cn lang=any other supported languages such as Spanish, etc.
/* @cn.comment.start@ String abc="??"; @cn.comment.end@ */ /* @en.comment.start@ String abc="Source code"; @en.comment.end@ */ // More languages here.When lang=en, or English "@en.comment.start@" is replaced by "*/". "@en.comment.end@" is replaced by "/*" The cn, Chinese code and all other language code is untouched or still commented out. Advantages
- When we copy the code to the place for build (compile), the replacement is done automatically by ant copy task. Build games in different languages became as easy as switching one word in the build.properties.
- One more thing is wonderful here. It is way better than use "if else or switch case statements". since the byte code is much smaller. It is especially important when you are building games on mobile devices.
- We only need one code base instead of many, which made code maintenance a lot simpler, less headache!!!
By the way, I did all my native build by myself on 4 different operating systems. It is quite hack of work. You basically need to know how to do it without ant, then put them in ant to automate the process. Even I've not used ant-contrib cc task yet, but from rough look at the doc, I guess what I said would be still true.
I had a good reason to do the similar like you said here when I was working in another company. We had developers work in a sandbox, no other people knew the new code yet, but the working developer. The sanbox would have the same name source code, which might be different from the mainstream same name code. In my case, when the sandbox built, the mainstream would be your src2.
How did I handle it?
I had a build/tmp/src directory, before compiling, I copied the source code need to compile to that directory. To mainstream developer, copying mainstream src was enough, then compile. To sandbox developer, copying mainstream src first, then copying the sandbox/src second, and set overwrite="true", very important, otherwise, copy task copies according to timestamps.
You javac from build/tmp/src to build/classes. In this way, your problem would be solved.
Actually, my case was way more complicated then, I even wrote my own copy task which called CopyWithDependencies. Of course, this is out of the range of our discussion here... Writing it down might help you think further...
- ant source code
- ant javadoc, when you download the source code, the javadoc is included.
- default.properties under ant source code
相关推荐
3. **docs目录**:包含了Ant的文档,包括用户指南、API参考和FAQ等,对于理解和使用Ant非常有帮助。 4. **LICENSE和NOTICE文件**:这些文件提供了关于Apache Ant的许可信息和版权声明,确保用户可以合法地使用和...
总结来说,Apache Ant 1.7.0是一个强大的Java项目构建工具,它的XML配置方式、任务驱动模型、依赖管理等功能为开发者提供了高效、灵活的项目构建解决方案。无论是在小型项目还是大型企业级应用中,Ant都能发挥其重要...
1. **Ant**:Ant是一个由Apache基金会开发的Java构建工具,它使用XML来定义构建过程。Ant通过任务(task)执行编译、打包、测试等操作,是Java项目自动化构建的基石。它可以与多种IDE和构建系统集成,确保跨平台的兼容...
13.Beans to XML 14.Replacing JSP with XSLT 15.Introducing AspectJ 16.Ant for Compilation and Distribution 16.1.Installation of ANT 16.2.What is ANT 16.3.Configuration of ANT 16.4.Usage ...
13.Beans to XML 14.Replacing JSP with XSLT 15.介绍AspectJ 16.用Ant Compilation和Distribution 16.01.安装Ant 16.02.什么是Ant 16.03.配置Ant 16.04.Ant的使用 17.结论 18.关于这个文档的技术背景 ...
`build.bat`可能是Windows下的批处理文件,而`build.xml`则是基于Ant的构建配置文件,用于执行构建过程,如编译源代码、创建JAR文件等。 `zsql_proxy(java).xsl`和`zsql_language.xsl`是XSLT(可扩展样式表语言转换...
FreeMarker手册同样会讨论一些高级特性,例如XML处理指南,这会涉及如何在FreeMarker模板中使用XML数据,包括XML节点树的处理、XML声明的处理等。在数据模型章节,手册会介绍如何构建数据模型,包括标量、容器、方法...
6. **编译和构建脚本**:可能包含Ant或Maven的build.xml或pom.xml文件,用于构建和打包项目。 开发者可以使用这个压缩包来学习如何在Java环境下结合Struts和Axis构建Web服务,包括创建服务端点、编写服务处理逻辑、...
教程的其他部分包括使用logic标签、模板(Templates)、FAQ、Beans到XML的转换、使用XSLT替换JSP、以及AspectJ的介绍,这些都是Struts框架中的重要概念和技术。此外,还介绍了Ant的安装和使用,Ant是Java项目常用的...
Jetty 欢迎访问Jetty文档 Wiki.... 这个wiki提供jetty的入门教程、基础配置、功能特性、优化、安全、JavaEE、监控、常见问题、故障排除帮助等等。它包含教程、使用手册、视频、...FAQ Contributing Contributing to Jetty
- **FAQ**:针对初学者常见的疑问提供了解答,帮助解决实际开发中遇到的问题。 #### 十五、附录 - **技术背景**:介绍了编写本教程时使用的工具和技术。 - **作者介绍**:简述作者的背景信息。 #### 十六、用户...
"build.xml"是Ant构建工具的配置文件,Ant是一个Java平台的构建工具,用于自动化软件构建任务,如编译、打包和测试。在这个StrutsTest项目中,"build.xml"会定义如何编译源代码、运行测试和生成最终的部署包。 最后...
16. **用Ant Compilation和Distribution**:Ant是Java项目构建的工具,这部分涵盖了Ant的安装、配置和使用,以及如何用Ant完成项目的编译和部署。 17. **结论**:总结整个教程的主要内容和学习成果。 18. **关于这...
16. **用Ant Compilation和Distribution**:介绍构建工具Ant的使用,包括安装、配置和基本任务的执行,用于自动化构建和部署流程。 17. **结论**:对整个教程的总结,鼓励读者进一步实践和深入学习。 18. **关于...
Erik Hatcher是Ant、Lucene和Tapestry项目的主要成员之一,同时也是《Java Development with Ant》一书的作者之一,该书获得了JDJ读者选择奖的FirstRunnerUp大奖。Otis Gospodnetic则是Lucene核心开发组的成员,并在...
5. **构建脚本**(如:build.xml):如果是使用Ant或Maven进行构建,那么会有一个构建脚本,用于自动化编译、测试和打包过程。 6. **版本控制文件**(如:.gitignore, .svn):如果项目使用了Git或SVN等版本控制...
- **与 Ant 的集成:** 在构建脚本中使用 FreeMarker。 - **Jython 包装器:** 支持 Jython 脚本语言。 #### 三、XML 处理指南 ##### 1. 前言 介绍如何利用 FreeMarker 处理 XML 数据。 ##### 2. 暴露 XML 文档 ...
- 在 `pom.xml` 文件中使用 `<dependencyManagement>` 来统一管理依赖版本。 - 使用 `<exclusions>` 排除不必要的依赖。 #### 4. 社区与贡献指南 - **如何为 Maven 贡献代码或文档?** - 加入邮件列表或者 IRC ...
#### 三、FAQ 1. **如何安装 Maven?** - 下载 Maven 的最新版本,解压到指定目录,设置 `MAVEN_HOME` 环境变量,并将 `bin` 目录添加到系统的 `PATH` 环境变量中。 2. **如何使用 Maven 创建新项目?** - 使用...