1 package org.quartz.jobs; 2 3 import java.io.File ; 4 import java.util.Iterator ; 5 import java.util.Map ; 6 import java.util.Properties ; 7 8 import org.apache.tools.ant.BuildException; 9 import org.apache.tools.ant.Project; 10 import org.apache.tools.ant.ProjectHelper; 11 import org.quartz.Job; 12 import org.quartz.JobDataMap; 13 import org.quartz.JobExecutionContext; 14 import org.quartz.JobExecutionException; 15 16 45 public class AntJob implements Job { 46 47 50 public AntJob() { 51 super(); 52 } 53 54 57 public void execute(JobExecutionContext context) 58 throws JobExecutionException { 59 60 JobDataMap dataMap = context.getMergedJobDataMap(); 61 String execDir = dataMap.getString("execdir"); 62 String buildFileName = dataMap.getString("buildfile"); 63 String target = dataMap.getString("target"); 64 65 Project project = new Project(); 67 try { 68 project.init(); 69 } catch (BuildException e) { 70 throw new JobExecutionException(e.getMessage()); 71 } 72 73 if (buildFileName == null) { 75 buildFileName = "build.xml"; 76 } 77 File buildFile = new File (execDir + File.separator + buildFileName); 78 79 Properties sysProps = System.getProperties(); 81 Iterator it = sysProps.entrySet().iterator(); 82 while (it.hasNext()) { 83 Map.Entry entry = (Map.Entry ) it.next(); 84 project.setProperty(entry.getKey().toString(), entry.getValue().toString()); 85 } 86 87 it = dataMap.keySet().iterator(); 89 while (it.hasNext()) { 90 String propName = (String ) it.next(); 92 String propValue = dataMap.getString(propName); 93 if (propName.startsWith("$P_")) { 94 project.setProperty(propName.substring(3), propValue); 95 } else if (propName.startsWith("$U_")) { 96 project.setUserProperty(propName.substring(3), propValue); 97 } 98 } 99 100 try { 101 ProjectHelper.getProjectHelper().parse(project, buildFile); 102 103 project.setProperty("basedir", project.getBaseDir().getPath()); 105 project.setProperty("ant.project.name", project.getName()); 106 project.setProperty("ant.file", buildFile.getPath()); 107 108 if (target == null) { 110 target = project.getDefaultTarget(); 111 } 112 113 project.executeTarget(target); 115 116 } catch (BuildException e) { 117 throw new JobExecutionException(e.getMessage()); 118 } 119 } 120 } | Popular Tags |