Help printing classpath in sbt 0.11.2

341 views
Skip to first unread message

Sean Rhea

unread,
Jan 5, 2012, 4:10:03 PM1/5/12
to simple-b...@googlegroups.com
All,

My project definition in sbt 0.7.7 had this code:

  trait PrintClassPath extends DefaultProject {
    lazy val printClasspath = task {
      val cwd = (new java.io.File (".")).getCanonicalPath
      println("CLASSPATH=$DIR/../../project/boot/scala-2.9.1/lib/scala-library.jar")
      this.runClasspath.getPaths.toList.sort((a,b) => a < b).foreach { p =>
        println("CLASSPATH=$CLASSPATH:$DIR/../.." + p.replaceFirst(cwd, ""))
      }
      None
    }     
  }

  lazy val foo = project("foo", "foo", new DefaultProject(_) with PrintClassPath)

The idea was to print out a bunch of shell code that would set the CLASSPATH variable, so that I can launch my programs without using sbt.  This is mainly useful if I want to constrain a program's memory usage.  For example, if I want to run my code with -Xmx16M, that's usually not enough memory to run sbt.  So instead I do something like:

  echo "#!/bin/sh" > /tmp/foo.sh
  echo 'DIR=${0%foo.sh}' >> /tmp/foo.sh
  sbt 'project foo' print-classpath | grep CLASSPATH >> /tmp/foo.sh
  echo "exec java -Xmx16M -cp $CLASSPATH Foo $@" >> /tmp/foo.sh
  /tmp/foo.sh arg1 arg2 arg3 ...

Does that make sense?

I'm trying to port this functionality to sbt 0.11.2, but I honestly can't make heads or tails of the Getting Started Guide.  I'm pretty sure I want a task that reads the value of dependency-classpath and iterates over its values like the above code, but I can't quite figure out how to do that or where to put the code.  Can someone please point me in the right direction?

Sean
--
Construct the world in which you want to live.  Then live there.

Chris Twiner

unread,
Jan 5, 2012, 4:45:34 PM1/5/12
to simple-b...@googlegroups.com
If its ok to have sbt running as well then follow the guide here about
forking: https://github.com/harrah/xsbt/wiki/Forking

I can't find the other docs of relevance (I blame tiredness) so:

in your .scala project build:

val runsInLowMemory = TaskKey[Unit]("runs-in-low-memory")

in your project build (as settings) or direct in a project/subproject .sbt file:

// Test here will use the classpath of main plus test I *think*
Compile is just your main
fullRunTask(runsInLowMemory, Test, "mainClassName", args*)

javaOptions in runsInLowMemory += "-Xmx16M"

fork in runsInLowMemory := true

then you can do run-in-low-memory and it will fork with the correct
path. (use ++= if you want to add more than one option to the jvm).

I've likely made a mistake somewhere (tiredness again) but that's the idea.

It takes a while for xsbt to start to make sense but it really is very cool.

> --
> You received this message because you are subscribed to the Google Groups
> "simple-build-tool" group.
> To post to this group, send email to simple-b...@googlegroups.com.
> To unsubscribe from this group, send email to
> simple-build-t...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/simple-build-tool?hl=en.

James Moore

unread,
Jan 5, 2012, 5:44:15 PM1/5/12
to simple-b...@googlegroups.com

Sean Rhea

unread,
Jan 5, 2012, 5:59:12 PM1/5/12
to simple-b...@googlegroups.com
Perfect. That first link is exactly what I need. Thanks, James!

Sean

> --
> You received this message because you are subscribed to the Google Groups
> "simple-build-tool" group.

> To view this discussion on the web visit
> https://groups.google.com/d/msg/simple-build-tool/-/iDZYto2ncl4J.


>
> To post to this group, send email to simple-b...@googlegroups.com.
> To unsubscribe from this group, send email to
> simple-build-t...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/simple-build-tool?hl=en.

--

Reply all
Reply to author
Forward
0 new messages