Permalink
Browse files

Starting on multi-module project support.

  • Loading branch information...
1 parent e3a0585 commit 2781ea8926563bd06c570a399b529ff9cbb822f9 @samskivert committed Nov 21, 2011
@@ -0,0 +1,44 @@
+//
+// sbt-pom-plugin - Makes Maven project metadata usable from SBT
+// http://github.com/samskivert/sbt-pom-plugin
+
+package samskivert
+
+import java.io.File
+
+import sbt._
+import sbt.Keys._
+
+import pomutil.{POM, Dependency}
+
+/**
+ * Makes Maven project metadata usable from SBT.
+ */
+object POMPlugin extends Plugin
+{
+ /** Reads SBT settings from the POM file at the specified path. */
+ def pomToSettings (path :String) :Seq[Setting[_]] = pomToSettings(new File(path))
+
+ /** Reads SBT settings from the supplied POM file. */
+ def pomToSettings (file :File) :Seq[Setting[_]] =
+ pomToSettings(POM.fromFile(file).getOrElse(sys.error("Invalid POM file: " + file)))
+
+ /** Reads SBT settings from the supplied POM.
+ * @param excludeDepends if true, dependency configuration omitted from settings. */
+ def pomToSettings (pom :POM, excludeDepends :Boolean = false) :Seq[Setting[_]] = {
+ val meta = Seq(
+ organization := pom.groupId,
+ name := pom.artifactId,
+ version := pom.version
+ // TODO: add support for various standard build settings
+ )
+ if (excludeDepends) meta
+ else meta ++ Seq(libraryDependencies ++= pom.depends.map(toIvyDepend))
+ }
+
+ /** Converts a Maven dependency to an Ivy dependency. */
+ def toIvyDepend (depend :Dependency) = {
+ // TODO: handle type, classifier, scope, etc.
+ depend.groupId % depend.artifactId % depend.version
+ }
+}
@@ -1,34 +0,0 @@
-//
-// $Id$
-
-package samskivert
-
-import java.io.File
-
-import sbt._
-import sbt.Keys._
-
-import pomutil.{POM, Dependency}
-
-/**
- * Extracts SBT settings from Maven POM files.
- */
-object POMSettings extends Plugin
-{
- def pomToSettings (path :String) :Seq[Setting[_]] = pomToSettings(new File(path))
-
- def pomToSettings (file :File) :Seq[Setting[_]] = {
- val pom = POM.fromFile(file).getOrElse(sys.error("Unable to read POM " + file))
- Seq(
- organization := pom.groupId,
- name := pom.artifactId,
- version := pom.version,
- libraryDependencies ++= pom.depends.map(toIvyDepend)
- )
- }
-
- def toIvyDepend (depend :Dependency) = {
- // TODO: handle type, classifier, scope, etc.
- depend.groupId % depend.artifactId % depend.version
- }
-}
@@ -0,0 +1,48 @@
+//
+// sbt-pom-plugin - Makes Maven project metadata usable from SBT
+// http://github.com/samskivert/sbt-pom-plugin
+
+package samskivert
+
+import java.io.File
+
+import sbt._
+import sbt.Keys._
+
+import pomutil.{POM, Dependency}
+
+/**
+ * Handles multimodule projects. Sub-module interdependencies will automatically be wired up as SBT
+ * project dependencies. Other Maven dependencies will be mapped to Ivy dependencies.
+ *
+ * Instantiate a project builder with the top-level POM. Use it to create SBT sub-projects like so:
+ * {{{
+ * val builder = new ProjectBuilder("pom.xml")
+ * lazy val core = builder("core", extraSettings)
+ * lazy val tools = builder("tools", extraSettings)
+ * lazy val client = builder("client", extraSettings)
+ * }}}
+ * The stock SBT settings will be automatically included, and `extraSettings` is optional.
+ */
+class ProjectBuilder (path :String)
+{
+ /** Creates an SBT project for the specified sub-module. */
+ def apply (name :String, settings :Seq[Setting[_]]) :Project = {
+ val pom = _modules.getOrElse(name, sys.error("No sub-module POM in " + name + "."))
+
+ // extract any sibling dependencies and turn them into project dependencies
+ sys.error("TODO")
+ }
+
+ private def resolveSubPOM (name :String) = {
+ val pomFile = new File(new File(name), "pom.xml")
+ POM.fromFile(pomFile) map(p => Some(name -> p)) getOrElse {
+ System.err.println("Failed to read sub-module POM: " + pomFile)
+ None
+ }
+ }
+
+ private val _pom = POM.fromFile(new File(path)).getOrElse(
+ sys.error("Unable to load POM from " + path))
+ private val _modules = _pom.modules.flatMap(resolveSubPOM).toMap
+}

0 comments on commit 2781ea8

Please sign in to comment.