|
| 1 | +/* |
| 2 | + * Copyright 2011 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.grails.exec.ant; |
| 17 | + |
| 18 | +import org.grails.exec.*; |
| 19 | + |
| 20 | +import java.io.File; |
| 21 | +import java.io.FilenameFilter; |
| 22 | +import java.net.MalformedURLException; |
| 23 | +import java.net.URL; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.Collections; |
| 26 | +import java.util.List; |
| 27 | + |
| 28 | +import org.apache.tools.ant.BuildException; |
| 29 | +import org.apache.tools.ant.Task; |
| 30 | +import org.apache.tools.ant.types.Path; |
| 31 | +import org.apache.tools.ant.types.Reference; |
| 32 | + |
| 33 | +/** |
| 34 | + * <p>Ant task for executing Grails scripts. To use it, first create a |
| 35 | + * task definition for it: |
| 36 | + * <pre> |
| 37 | + * <path id="grails.classpath"> |
| 38 | + * <fileset dir="${grails.home}/dist" includes="grails-bootstrap-*.jar"/> |
| 39 | + * <fileset dir="${grails.home}/lib"> |
| 40 | + * <include name="groovy-all*.jar"/> |
| 41 | + * <include name="ivy*.jar"/> |
| 42 | + * <include name="gpars*.jar"/> |
| 43 | + * <include name="gant_groovy*.jar"/> |
| 44 | + * </fileset> |
| 45 | + * </path> |
| 46 | + * |
| 47 | + * <taskdef name="grails" |
| 48 | + * classname="grails.ant.GrailsTask" |
| 49 | + * classpathref="grails.classpath"/> |
| 50 | + * </pre> |
| 51 | + * You must have the "grails-bootstrap", "groovy-all", Ivy, and GPars JARs on |
| 52 | + * the <code>taskdef<code>'s classpath, otherwise the task won't load. |
| 53 | + * </p> |
| 54 | + * <p>Once the task is defined, you can use it like this: |
| 55 | + * <pre> |
| 56 | + * <grails home="${grails.home}" script="Clean"/> |
| 57 | + * </pre> |
| 58 | + * The <code>home</code> attribute contains the location of a local |
| 59 | + * Grails installation, while <code>script</code> is the name of the |
| 60 | + * Grails script to run. Note that it's the <em>script</em> name not |
| 61 | + * the equivalent command name. |
| 62 | + * </p> |
| 63 | + * <p>If you want to use the Ant task without a Grails installation, |
| 64 | + * then you can use the <code>classpathref</code> attribute or |
| 65 | + * <code>classpath</code> nested element instead of <code>home</code>. |
| 66 | + * This allows you to control precisely which JARs and versions are |
| 67 | + * used to execute the Grails scripts. Typically you would use this |
| 68 | + * option in conjunction with something like Ivy. |
| 69 | + * </p> |
| 70 | + */ |
| 71 | +public class GrailsTask extends Task { |
| 72 | + |
| 73 | + private File home; |
| 74 | + private String script; |
| 75 | + private String args; |
| 76 | + private String environment; |
| 77 | + private boolean includeRuntimeClasspath = true; |
| 78 | + private Path classpath; |
| 79 | + |
| 80 | + private Path compileClasspath; |
| 81 | + private Path testClasspath; |
| 82 | + private Path runtimeClasspath; |
| 83 | + |
| 84 | + @Override |
| 85 | + public void execute() throws BuildException { |
| 86 | + // The "script" must be specified. |
| 87 | + if (script == null) throw new BuildException("'script' must be provided."); |
| 88 | + |
| 89 | + // Check that one, and only one, of Grails home and classpath are set. |
| 90 | + if (home == null && classpath == null) { |
| 91 | + throw new BuildException("One of 'home' or 'classpath' must be provided."); |
| 92 | + } |
| 93 | + |
| 94 | + if (home != null && classpath != null) { |
| 95 | + throw new BuildException("You cannot use both 'home' and 'classpath' with the Grails task."); |
| 96 | + } |
| 97 | + |
| 98 | + runGrails(script, args); |
| 99 | + } |
| 100 | + |
| 101 | + protected void runGrails(String targetName, @SuppressWarnings("hiding") String args) { |
| 102 | + // First get the dependencies from the classpath. |
| 103 | + List<URL> urls = new ArrayList<URL>(); |
| 104 | + if (classpath != null) { |
| 105 | + urls.addAll(pathsToUrls(classpath)); |
| 106 | + } |
| 107 | + else { |
| 108 | + urls.addAll(getRequiredLibsFromHome()); |
| 109 | + } |
| 110 | + |
| 111 | + try { |
| 112 | + URL[] loaderUrls = urls.toArray(new URL[urls.size()]); |
| 113 | + RootLoader rootLoader = new RootLoader(loaderUrls, getClass().getClassLoader()); |
| 114 | + |
| 115 | + GrailsExecutor executor; |
| 116 | + if (getProject().getBaseDir() != null) { |
| 117 | + executor = new GrailsExecutor(rootLoader, home == null ? null : |
| 118 | + home.getCanonicalPath(), getProject().getBaseDir().getCanonicalPath()); |
| 119 | + } |
| 120 | + else { |
| 121 | + executor = new GrailsExecutor(rootLoader, home == null ? null : home.getCanonicalPath()); |
| 122 | + } |
| 123 | + |
| 124 | + int retval; |
| 125 | + if (environment == null) { |
| 126 | + retval = executor.execute(targetName, args); |
| 127 | + } |
| 128 | + else { |
| 129 | + retval = executor.execute(targetName, args, environment); |
| 130 | + } |
| 131 | + |
| 132 | + if (retval != 0) { |
| 133 | + throw new BuildException("Grails returned non-zero value: " + retval); |
| 134 | + } |
| 135 | + } |
| 136 | + catch (Exception ex) { |
| 137 | + throw new BuildException("Unable to start Grails: " + ex.getMessage(), ex); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private List<URL> getRequiredLibsFromHome() { |
| 142 | + List<URL> urls = new ArrayList<URL>(); |
| 143 | + |
| 144 | + try { |
| 145 | + // Make sure Groovy, Gant, Ivy and GPars are on the classpath if we are using "grailsHome". |
| 146 | + File[] files = new File(home, "lib").listFiles(new FilenameFilter() { |
| 147 | + public boolean accept(File dir, String name) { |
| 148 | + return name.startsWith("gant_") || name.startsWith("groovy-all") || |
| 149 | + name.startsWith("ivy") || name.startsWith("gpars"); |
| 150 | + } |
| 151 | + }); |
| 152 | + |
| 153 | + for (File file : files) { |
| 154 | + urls.add(file.toURI().toURL()); |
| 155 | + } |
| 156 | + |
| 157 | + // Also make sure the bootstrap JAR is on the classpath. |
| 158 | + files = new File(home, "dist").listFiles(new FilenameFilter() { |
| 159 | + public boolean accept(File dir, String name) { |
| 160 | + return name.startsWith("grails-bootstrap"); |
| 161 | + } |
| 162 | + }); |
| 163 | + |
| 164 | + for (File file : files) { |
| 165 | + urls.add(file.toURI().toURL()); |
| 166 | + } |
| 167 | + |
| 168 | + return urls; |
| 169 | + } |
| 170 | + catch (MalformedURLException e) { |
| 171 | + throw new RuntimeException(e); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + private List<URL> pathsToUrls(Path path) { |
| 176 | + if (path == null) return Collections.emptyList(); |
| 177 | + |
| 178 | + List<URL> urls = new ArrayList<URL>(path.size()); |
| 179 | + for (String filePath : path.list()) { |
| 180 | + try { urls.add(new File(filePath).toURI().toURL()); } |
| 181 | + catch (MalformedURLException ex) { throw new RuntimeException(ex); } |
| 182 | + } |
| 183 | + |
| 184 | + return urls; |
| 185 | + } |
| 186 | + |
| 187 | + public String getCommand() { |
| 188 | + return script == null ? null : NameUtils.toCommandName(script); |
| 189 | + } |
| 190 | + |
| 191 | + public void setCommand(String command) { |
| 192 | + if (command == null) { |
| 193 | + throw new BuildException("'command' cannot be null"); |
| 194 | + } else if (command.trim().length() == 0) { |
| 195 | + throw new BuildException("'command' cannot be a blank string"); |
| 196 | + } |
| 197 | + |
| 198 | + script = NameUtils.toScriptName(command); |
| 199 | + } |
| 200 | + |
| 201 | + public File getHome() { |
| 202 | + return home; |
| 203 | + } |
| 204 | + |
| 205 | + public void setHome(File home) { |
| 206 | + this.home = home; |
| 207 | + } |
| 208 | + |
| 209 | + public String getScript() { |
| 210 | + return script; |
| 211 | + } |
| 212 | + |
| 213 | + public void setScript(String script) { |
| 214 | + this.script = script; |
| 215 | + } |
| 216 | + |
| 217 | + public String getArgs() { |
| 218 | + return args; |
| 219 | + } |
| 220 | + |
| 221 | + public void setArgs(String args) { |
| 222 | + this.args = args; |
| 223 | + } |
| 224 | + |
| 225 | + public String getEnvironment() { |
| 226 | + return environment; |
| 227 | + } |
| 228 | + |
| 229 | + public void setEnvironment(String environment) { |
| 230 | + this.environment = environment; |
| 231 | + } |
| 232 | + |
| 233 | + public boolean isIncludeRuntimeClasspath() { |
| 234 | + return includeRuntimeClasspath; |
| 235 | + } |
| 236 | + |
| 237 | + public void setIncludeRuntimeClasspath(boolean includeRuntimeClasspath) { |
| 238 | + this.includeRuntimeClasspath = includeRuntimeClasspath; |
| 239 | + } |
| 240 | + |
| 241 | + public Path getClasspath() { |
| 242 | + return classpath; |
| 243 | + } |
| 244 | + |
| 245 | + public void addClasspath(@SuppressWarnings("hiding") Path classpath) { |
| 246 | + this.classpath = classpath; |
| 247 | + } |
| 248 | + |
| 249 | + public void setClasspathRef(Reference ref) { |
| 250 | + addClasspath((Path) ref.getReferencedObject()); |
| 251 | + } |
| 252 | + |
| 253 | + @Deprecated |
| 254 | + public Path getCompileClasspath() { |
| 255 | + return compileClasspath; |
| 256 | + } |
| 257 | + |
| 258 | + @Deprecated |
| 259 | + public void addCompileClasspath(@SuppressWarnings("hiding") Path compileClasspath) { |
| 260 | + this.compileClasspath = compileClasspath; |
| 261 | + } |
| 262 | + |
| 263 | + @Deprecated |
| 264 | + public Path getTestClasspath() { |
| 265 | + return testClasspath; |
| 266 | + } |
| 267 | + |
| 268 | + @Deprecated |
| 269 | + public void addTestClasspath(@SuppressWarnings("hiding") Path testClasspath) { |
| 270 | + this.testClasspath = testClasspath; |
| 271 | + } |
| 272 | + |
| 273 | + @Deprecated |
| 274 | + public Path getRuntimeClasspath() { |
| 275 | + return runtimeClasspath; |
| 276 | + } |
| 277 | + |
| 278 | + @Deprecated |
| 279 | + public void addRuntimeClasspath(@SuppressWarnings("hiding") Path runtimeClasspath) { |
| 280 | + this.runtimeClasspath = runtimeClasspath; |
| 281 | + } |
| 282 | +} |
0 commit comments