How to build classpath for sbt project from executing test sbt 0.7.7 -> sbt 0.12.1

130 views
Skip to first unread message

Michael Franz

unread,
Jan 11, 2013, 12:48:19 PM1/11/13
to simple-b...@googlegroups.com

Hi,

 I have been looking for a solution to my problem and found some relevant questions (with answers) and was hoping to clarify and see if there was another solution.

 We have a test that we run from sbt (sbt test) fires up 2 other java process.  These processes need to have the same classpath as the project.  When using sbt 0.7.7 we were able to just build a classpath based on lib and lib_managed.  Added every jar in lib and lib_managed to the classpath at launch.  Now that we are migrating to sbt 0.12.1 lib_managed has been replaced with the ivy cache.  Adding every jar in the ivy cache might work, but I am sure that will lead to pain down the road (duplicate version, more jars than we actually need, conflicts, …) 


 What I need is a way to get the project classpath from within a running test so that I can pass it along to my spawned java processes.  I have read through the following questions and answers, but was hoping my solution was not to generate a start script for my spawned processes.

https://groups.google.com/d/topic/simple-build-tool/RkaEEq_qcdM/discussion

https://groups.google.com/d/topic/simple-build-tool/GLm0PS65cfg/discussion

https://groups.google.com/d/topic/simple-build-tool/28LYFHGBbJI/discussion

 Which lead to stack over flow

http://stackoverflow.com/questions/7449312/create-script-with-classpath-from-sbt

http://stackoverflow.com/questions/7134993/how-do-i-run-an-sbt-main-class-from-the-shell-as-normal-command-line-program

Another aspect of this, is when we run the tests from within IntelliJ we have to construct the classpath differently in the sbt 0.7.7 project, but use the same for sbt 0.12.1 (the IntelliJ project generation plugin works a little differently between 0.7.7 and 0.12.1).  I am not that concerned about the intellij aspect as I can grab the java.class.path within the test and use it directly.


So, is there a way to get the project classpath within a running test?


Thank you


Michael

Mark Harrah

unread,
Jan 12, 2013, 11:30:08 AM1/12/13
to simple-b...@googlegroups.com
On Fri, 11 Jan 2013 09:48:19 -0800 (PST)
Michael Franz <mvf...@gmail.com> wrote:

>
>
> Hi,
>
> I have been looking for a solution to my problem and found some relevant
> questions (with answers) and was hoping to clarify and see if there was
> another solution.
>
> We have a test that we run from sbt (sbt test) fires up 2 other java
> process. These processes need to have the same classpath as the project.
> When using sbt 0.7.7 we were able to just build a classpath based on lib
> and lib_managed. Added every jar in lib and lib_managed to the classpath
> at launch. Now that we are migrating to sbt 0.12.1 lib_managed has been
> replaced with the ivy cache. Adding every jar in the ivy cache might work,
> but I am sure that will lead to pain down the road (duplicate version, more
> jars than we actually need, conflicts, …)
>
>
> What I need is a way to get the project classpath from within a running
> test so that I can pass it along to my spawned java processes. I have read
> through the following questions and answers, but was hoping my solution was
> not to generate a start script for my spawned processes.
>
> https://groups.google.com/d/topic/simple-build-tool/RkaEEq_qcdM/discussion
>
> https://groups.google.com/d/topic/simple-build-tool/GLm0PS65cfg/discussion
>
> https://groups.google.com/d/topic/simple-build-tool/28LYFHGBbJI/discussion

The last thread only involves a start script because that was what the user was doing. In general, you get the classpath from the fullClasspath in Test task. This gives you a value of type Classpath, from which you can call .files on it to get a Seq[File].

The main work is usually passing that information to the test framework because test frameworks have different ways of doing it. The post from Josh C. links to his article about how to do it for ScalaTest.

One way to do it that works for all test frameworks is to create a resource file containing the class path and then load it from the test.

> Which lead to stack over flow
>
> http://stackoverflow.com/questions/7449312/create-script-with-classpath-from-sbt
>
> http://stackoverflow.com/questions/7134993/how-do-i-run-an-sbt-main-class-from-the-shell-as-normal-command-line-program
>
> Another aspect of this, is when we run the tests from within IntelliJ we
> have to construct the classpath differently in the sbt 0.7.7 project, but
> use the same for sbt 0.12.1 (the IntelliJ project generation plugin works a
> little differently between 0.7.7 and 0.12.1). I am not that concerned
> about the intellij aspect as I can grab the java.class.path within the test
> and use it directly.

One solution is to fork tests and the java.class.path will be just your application classes and dependencies.

-Mark

> So, is there a way to get the project classpath within a running test?
>
>
> Thank you
>
>
> Michael
>
> --
> 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/-/YnIZ80CgUxgJ.
> 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