环境:
1.jdk:jdk1.5.0_22
2.ant:apache-ant-1.7.0
3.svnclient:TortoiseSVN 1.6.11
4.svnant:从http://subclipse.tigris.org获取ant使用svn的jar【ganymed.jar、svnant.jar、svnClientAdapter.jar、svnjavahl.jar、svnkit.jar】
5.Eclipse的编译包jdtCompilerAdapter.jar 从【Eclipse的plugin/org.eclipse.jdt.core_XXXx.jar】里面获取
6.示例代码
6.1tomcat的web工程 在tomcat下的{tomcathome}/conf/Catalina/localhost/下有工程引用的配置
6.2或者是在jboss中的{jbosshome/server/default/deploy/XXX.war}
7.环境变量设置ANT_HOME
步骤:
1.编写build.properties
svn.url=projectsvn的地址
local.dir=源码的位置
svn.username=svn的用户名
svn.password=svn的密码
jdk.home=jdk位置 【ex:C:/java】
3.如果编译时eclipse的adapeter也报【UTF-8的格式的文件时会出现“非法字符:/65279”的错误】 的话则需要过滤java文件中的特殊字符【UTF8Parser.java】
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.FileChannel;
public class UTF8Parser {
public static String SRCS="/src/";
public static String WEB_INF_CLASSES="/WEB-INF/classes/src/";
public static void main(String[] args) {
if (args.length < 2) {
return;
}
if (args[1].startsWith(".")) {
args[1] = args[1].substring(1);
}
File file = new File(args[0] + args[1]);
System.out.println("java代码位置:" + file.getAbsolutePath());
UTF8Parser.clearUTF8Mark(file);
}
private static void clearUTF8Mark(File file) {
if (file.isDirectory()) {
for (File f : file.listFiles()) {
clearUTF8Mark(f);
}
} else if(file.getName().endsWith(".java")) {
FileInputStream fis = null;
FileInputStream fis1 = null;
FileOutputStream fos = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
OutputStream out = null;
try {
fis = new FileInputStream(file);
is = new BufferedInputStream(fis);
baos = new ByteArrayOutputStream();
byte b[] = new byte[3];
is.read(b);
// System.out.println(b[0] + ":" + b[1] + ":" + b[2]);
//System.out.println("src:"+file.getAbsolutePath().replace("//", "/"));
//System.out.println("dest"+file.getAbsolutePath().replace("//", "/").replace(SRCS, WEB_INF_CLASSES));
if (-17 == b[0] && -69 == b[1] && -65 == b[2]) {
b = new byte[1024];
while (true) {
int bytes = 0;
try {
bytes = is.read(b);
} catch (IOException e) {
}
if (bytes == -1) {
break;
}
baos.write(b, 0, bytes);
b = baos.toByteArray();
}
// System.out.println(new String(b, "utf-8"));
//file.delete();
if (!mkDirs(getDir(file.getAbsolutePath().replace("//", "/").replace(SRCS, WEB_INF_CLASSES)))) {
System.err.println("Dest Dir:" + getDir(file.getAbsolutePath().replace("//", "/").replace(SRCS, WEB_INF_CLASSES)) + " can't make!!!");
return;
}
out = new FileOutputStream(
new File(file.getAbsolutePath().replace("//", "/").replace(SRCS, WEB_INF_CLASSES))
);
baos.writeTo(out);
}else{
fis1 = new FileInputStream(file);
FileChannel srcChannel = fis1.getChannel();
if (!mkDirs(getDir(file.getAbsolutePath().replace("//", "/").replace(SRCS, WEB_INF_CLASSES)))) {
System.err.println("Dest Dir:" + getDir(file.getAbsolutePath().replace("//", "/").replace(SRCS, WEB_INF_CLASSES)) + " can't make!!!");
return;
}
fos=new FileOutputStream(new File(file.getAbsolutePath().replace("//", "/").replace(SRCS, WEB_INF_CLASSES)));
FileChannel dstChannel = fos.getChannel();
dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
}
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
} finally {
try {
if (fis != null) {
fis.close();
}
if (fis1 != null) {
fis1.close();
}
if (fos != null) {
fos.close();
}
if (out != null) {
out.close();
}
if (is != null) {
is.close();
}
if (baos != null) {
baos.close();
}
} catch (Exception e) {
System.exit(0);
}
}
}
}
/**
* 获取一个完整路径文件名的dir
*
* @param path
* @return
*/
private static String getDir(String path) {
File file = new File(path);
return file.getParent();
}
/**
* 创建dir ex:c:/lee/src ...
*
* @param dirPath
* @return
*/
private static boolean mkDirs(String dirPath) {
File dir = new File(dirPath);
if (dir.exists()) {
return true;
}
return dir.mkdirs();
}
}
4.编写build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="test" default="all">
<property file="build.properties" />
<property name="project.name" value="projectName " />
<property name="module.jdk.bin.test" value="${jdk.home}/bin"/>
<property name="project.path" value="${local.dir}" />
<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter" />
<property name="jboss.home" value="jboss的位置 " /> <!-- ex:D:/jboss-4.2.2.GA -->
<property name="tomcat.home" value="tomcat的位置 " />
<property name="bind.address" value="localhost " />
<!--property name="dest.dir" value="${project.path}/WEB-INF/classes" /-->
<property name="dest.dir" value="工程src编译后存放的位置 " /><!--ex: D:/MySys/projectName/WEB-INF/classes -->
<path id="ant.classpath">
<pathelement location="${ant_home}/lib/svnant.jar"/>
<pathelement location="${ant_home}/lib/svnClientAdapter.jar"/>
<pathelement location="${ant_home}/lib/svnjavahl.jar"/>
<pathelement location="${ant_home}/lib/svnkit.jar"/>
<pathelement location="${ant_home}/lib/ganymed.jar"/>
</path>
<taskdef name="svn" classname="org.tigris.subversion.svnant.SvnTask" >
<classpath refid="ant.classpath"/>
</taskdef>
<target name="checkout" >
<echo message="download ${project.name} source"/>
<svn javahl="true" username="${svn.username}" password="${svn.password}" >
<!-- <update url="${svn.url}" destPath="${project.path}" /> -->
<update dir="${project.path}" />
</svn>
</target>
<target name="clean.dir" description="Delete the generated configuration">
<delete dir="${dest.dir}" />
<mkdir dir="${dest.dir}" />
<mkdir dir="${dest.dir}/src" />
</target>
<target name="utf8">
<echo>clear utf-8 tag</echo>
<java dir="./" classname="UTF8Parser" fork="true" failοnerrοr="true" maxmemory="128m">
<arg line="${project.path}/ src" />
</java>
</target>
<target name="compile-src" depends="utf8" description="compile ${project.name} source" >
<echo message="compile ${project.name} source"/>
<javac srcdir="${dest.dir}/src" source="1.5" target="1.5" destdir="${dest.dir}"
debug="true" compiler= "modern" encoding="UTF-8" fork="yes" includes="**" includeAntRuntime="no"
memoryInitialSize="256m" memoryMaximumSize="1024m">
<compilerarg value="-Xlint:unchecked"/>
<compilerarg value="-Xlint:deprecation"/>
<classpath>
<fileset dir="${project.path}/WEB-INF/lib">
<include name="*.jar"/>
</fileset>
</classpath>
</javac>
<copy todir="${dest.dir}" overwrite="yes">
<fileset dir="${project.path}/src">
<include name="**/**" />
<exclude name="**/*.svn"/>
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target name="start.tomcat" description="Start Tomcat and wait for it to become available">
<exec executable="${tomcat.home}/bin/startup.bat" osfamily="windows" />
<exec executable="${tomcat.home}/bin/startup.sh" osfamily="unix" />
<waitfor maxwait="2"
maxwaitunit="minute"
checkevery="15"
checkeveryunit="second"
timeoutproperty="tomcat.timeout">
<socket server="${bind.address}" port="8080" />
</waitfor>
<fail if="tomcat.timeout" message="tomcat did not start within 2 minutes" />
</target>
<target name="start.jboss" description="Start JBoss AS and wait for it to become available">
<exec executable="${jboss.home}/bin/run.bat" spawn="yes" osfamily="windows">
<arg value="-b" />
<arg value="${bind.address}" />
</exec>
<exec executable="${jboss.home}/bin/run.sh" spawn="yes" osfamily="unix">
<arg value="-b" />
<arg value="${bind.address}" />
</exec>
<waitfor maxwait="5"
maxwaitunit="minute"
checkevery="30"
checkeveryunit="second"
timeoutproperty="jboss.timeout">
<socket server="${bind.address}" port="8080" />
</waitfor>
<fail if="jboss.timeout" message="jboss did not start within 5 minutes" />
</target>
<target name="all" depends="clean.dir,checkout,utf8,compile-src"/>
<!-- <target name="all" depends="clean.dir,checkout,utf8,compile-src,start.jboss"/> -->
</project>