/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. */
  18. package org.fusesource.scalate.maven
  19. import java.io.File
  20. import java.util.ArrayList
  21. import org.apache.maven.plugin.AbstractMojo
  22. import org.apache.maven.project.MavenProject
  23. import org.scala_tools.maven.mojo.annotations._
  24. import java.net.{URL, URLClassLoader}
  25. import collection.JavaConversions._
  26. /**
  27. * This goal precompiles the Scalate templates into classes to be included
  28. * in your build.
  29. *
  30. * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
  31. */
  32. @goal("precompile")
  33. @phase("prepare-package")
  34. @requiresProject
  35. @requiresDependencyResolution("test")
  36. class PrecompileMojo extends AbstractMojo {
  37. @parameter
  38. @expression("${project}")
  39. @readOnly
  40. @required
  41. var project: MavenProject = _
  42. @parameter
  43. @description("The directory where the templates files are located.")
  44. @expression("${basedir}/src/main/webapp")
  45. var warSourceDirectory: File = _
  46. @parameter
  47. @description("The directory where resources are located.")
  48. @expression("${basedir}/src/main/resources")
  49. var resourcesSourceDirectory: File = _
  50. @parameter
  51. @description("The directory where the scala code will be generated into.")
  52. @expression("${project.build.directory}/generated-sources/scalate")
  53. var targetDirectory: File = _
  54. @parameter
  55. @description("The directory containing generated classes .")
  56. @expression("${project.build.outputDirectory}")
  57. var classesDirectory:File = _
  58. @parameter
  59. @description("Additional template paths to compile.")
  60. var templates:ArrayList[String] = new ArrayList[String]()
  61. @parameter
  62. @description("The class name of the render context.")
  63. var contextClass:String = _
  64. @parameter
  65. @description("The class name of the Boot class to use.")
  66. var bootClassName:String = _
  67. @parameter
  68. @description("The test project classpath elements.")
  69. @expression("${project.testClasspathElements}")
  70. var classPathElements: java.util.List[_] = _
  71. def execute() = {
  72. //
  73. // Lets use project's classpath when we run the pre-compiler tool
  74. //
  75. val urls: Array[URL] = classPathElements.map { d =>
  76. new File(d.toString).toURI.toURL
  77. }.toArray
  78. getLog.debug("Found project class loader URLs: " + urls.toList)
  79. val loader = new URLClassLoader(urls, Thread.currentThread.getContextClassLoader)
  80. val oldLoader = Thread.currentThread.getContextClassLoader
  81. Thread.currentThread.setContextClassLoader(loader)
  82. try {
  83. // Structural Typing FTW (avoids us doing manual reflection)
  84. type Precompiler = {
  85. var sources: Array[File]
  86. var workingDirectory: File
  87. var targetDirectory: File
  88. var templates: Array[String]
  89. var info: {def apply(v1:String):Unit}
  90. var contextClass: String
  91. var bootClassName:String
  92. def execute(): Unit
  93. }
  94. val precompilerClassName = "org.fusesource.scalate.support.Precompiler"
  95. val precompiler = loader.loadClass(precompilerClassName).newInstance.asInstanceOf[Precompiler]
  96. precompiler.info = (value:String)=>getLog.info(value)
  97. precompiler.sources = Array(this.warSourceDirectory, this.resourcesSourceDirectory)
  98. precompiler.workingDirectory = this.targetDirectory
  99. precompiler.targetDirectory = this.classesDirectory
  100. precompiler.templates = this.templates.toList.toArray
  101. precompiler.contextClass = this.contextClass
  102. precompiler.bootClassName = this.bootClassName
  103. precompiler.execute
  104. } finally {
  105. Thread.currentThread.setContextClassLoader(oldLoader)
  106. }
  107. }
  108. }