Skip to content

Commit

Permalink
Starting on multi-module project support.
Browse files Browse the repository at this point in the history
  • Loading branch information
samskivert committed Nov 21, 2011
1 parent e3a0585 commit 2781ea8
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 34 deletions.
44 changes: 44 additions & 0 deletions src/main/scala/samskivert/POMPlugin.scala
@@ -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
}
}
34 changes: 0 additions & 34 deletions src/main/scala/samskivert/POMSettings.scala

This file was deleted.

48 changes: 48 additions & 0 deletions src/main/scala/samskivert/ProjectBuilder.scala
@@ -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.