PageRenderTime 30ms CodeModel.GetById 16ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 1ms

/maven-scalate-plugin/src/main/java/org/fusesource/scalate/maven/PrecompileMojo.scala

http://github.com/scalate/scalate
Scala | 130 lines | 81 code | 22 blank | 27 comment | 0 complexity | 20645b7d5c10fc25212a99029a65f2eb MD5 | raw file
  1/**
  2 * Copyright (C) 2009-2011 the original author or authors.
  3 * See the notice.md file distributed with this work for additional
  4 * information regarding copyright ownership.
  5 *
  6 * Licensed under the Apache License, Version 2.0 (the "License");
  7 * you may not use this file except in compliance with the License.
  8 * You may obtain a copy of the License at
  9 *
 10 *     http://www.apache.org/licenses/LICENSE-2.0
 11 *
 12 * Unless required by applicable law or agreed to in writing, software
 13 * distributed under the License is distributed on an "AS IS" BASIS,
 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15 * See the License for the specific language governing permissions and
 16 * limitations under the License.
 17 */
 18package org.fusesource.scalate.maven
 19
 20import java.io.File
 21import java.util.ArrayList
 22
 23import org.apache.maven.plugin.AbstractMojo
 24import org.apache.maven.project.MavenProject
 25import org.scala_tools.maven.mojo.annotations._
 26import java.net.{URL, URLClassLoader}
 27import collection.JavaConversions._
 28
 29
 30/**
 31 * This goal precompiles the Scalate templates into classes to be included
 32 * in your build.
 33 *
 34 * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
 35 */
 36@goal("precompile")
 37@phase("prepare-package")
 38@requiresProject
 39@requiresDependencyResolution("test")
 40class PrecompileMojo extends AbstractMojo {
 41
 42  @parameter
 43  @expression("${project}")
 44  @readOnly
 45  @required
 46  var project: MavenProject = _
 47
 48  @parameter
 49  @description("The directory where the templates files are located.")
 50  @expression("${basedir}/src/main/webapp")
 51  var warSourceDirectory: File = _
 52
 53  @parameter
 54  @description("The directory where resources are located.")
 55  @expression("${basedir}/src/main/resources")
 56  var resourcesSourceDirectory: File = _
 57
 58  @parameter
 59  @description("The directory where the scala code will be generated into.")
 60  @expression("${project.build.directory}/generated-sources/scalate")
 61  var targetDirectory: File = _
 62
 63  @parameter
 64  @description("The directory containing generated classes .")
 65  @expression("${project.build.outputDirectory}")
 66  var classesDirectory:File = _
 67
 68  @parameter
 69  @description("Additional template paths to compile.")
 70  var templates:ArrayList[String] = new ArrayList[String]()
 71
 72  @parameter
 73  @description("The class name of the render context.")
 74  var contextClass:String = _
 75
 76  @parameter
 77  @description("The class name of the Boot class to use.")
 78  var bootClassName:String = _
 79
 80  @parameter
 81  @description("The test project classpath elements.")
 82  @expression("${project.testClasspathElements}")
 83  var classPathElements: java.util.List[_] = _
 84
 85
 86  def execute() = {
 87
 88    //
 89    // Lets use project's classpath when we run the pre-compiler tool
 90    //
 91
 92    val urls: Array[URL] = classPathElements.map { d =>
 93      new File(d.toString).toURI.toURL
 94    }.toArray
 95
 96    getLog.debug("Found project class loader URLs: " + urls.toList)
 97    val loader = new URLClassLoader(urls, Thread.currentThread.getContextClassLoader)
 98
 99    val oldLoader = Thread.currentThread.getContextClassLoader
100    Thread.currentThread.setContextClassLoader(loader)
101    try {
102      // Structural Typing FTW (avoids us doing manual reflection)
103      type Precompiler = {
104        var sources: Array[File]
105        var workingDirectory: File
106        var targetDirectory: File
107        var templates: Array[String]
108        var info: {def apply(v1:String):Unit}
109        var contextClass: String
110        var bootClassName:String
111        def execute(): Unit
112      }
113
114      val precompilerClassName = "org.fusesource.scalate.support.Precompiler"
115      val precompiler = loader.loadClass(precompilerClassName).newInstance.asInstanceOf[Precompiler]
116
117      precompiler.info = (value:String)=>getLog.info(value)
118      
119      precompiler.sources = Array(this.warSourceDirectory, this.resourcesSourceDirectory)
120      precompiler.workingDirectory = this.targetDirectory
121      precompiler.targetDirectory = this.classesDirectory      
122      precompiler.templates = this.templates.toList.toArray
123      precompiler.contextClass = this.contextClass
124      precompiler.bootClassName = this.bootClassName
125      precompiler.execute
126    } finally {
127      Thread.currentThread.setContextClassLoader(oldLoader)
128    }
129  }
130}