Re: [sbt] Determining the OS in an sbt build definition

1,251 views
Skip to first unread message

Martin Grigorov

unread,
Jul 3, 2012, 7:05:33 AM7/3/12
to simple-b...@googlegroups.com
Hi,

Did you try with normal Java way: System.getProperty("os.name"); ?

On Tue, Jul 3, 2012 at 12:53 PM, brett <br...@jemstep.com> wrote:
> hi,
>
> I have an os-specific dependency for a project, and would like to use
> classifiers to determine which dependency to pull down.
>
> e.g, i have 2 artifacts in my repo, artifact-3.0.0-SNAPSHOT-linux.jar and
> artifact-3.0.0-SNAPSHOT-mac.jar:
>
> // os should be "linux" or "mac"
> os = determineOS()
>
> libraryDependencies ++= Seq(
> "com.company.grp" % "artifact" % " 3.0.0-SNAPSHOT" classifier os
> )
>
> Any guidance on how to get this in sbt?
>
> Thanks, brett
>
> --
> 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/-/Mu_Qjp5PoJkJ.
> 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.

Marko Elezović

unread,
Jul 4, 2012, 6:55:49 PM7/4/12
to simple-b...@googlegroups.com
Don't forget that you can also do a sys.props("os.name") which is a bit more sexy.

I use something similar to this for my evil purposes:

case class OS(name: OS.Name.Name, arch: OS.Arch.Arch)

object OS {

  object Name extends Enumeration {
    type Name = Value
    val Windows, Linux, Mac = Value
  }

  object Arch extends Enumeration {
    type Arch = Value
    val X86, AMD64, PPC = Value
  }

  val here = {
    val name = sys.props.get("os.name").flatMap(n =>
      OS.Name.values.find(v => n.contains(v.toString))
    ).getOrElse(sys.error("Unknown OS name!"))

    val arch = sys.props.get("os.arch").flatMap(a =>
      OS.Arch.values.find(_.toString.equalsIgnoreCase(a))
    ).getOrElse(sys.error("Unknown OS arch!"))

    OS(name, arch)
  }

There is also some rather nice code available in the ...

http://commons.apache.org/lang/api-3.1/org/apache/commons/lang3/SystemUtils.html

... which is something you could take a peek at if you need more fine grained switching via properties like
IS_OS_AIX, IS_OS_FREE_BSD, IS_OS_IRIX, IS_OS_OS2, etc...

Regards,
Marko

On 4.7.2012. 11:42, brett wrote:


On Tuesday, 3 July 2012 13:05:33 UTC+2, Martin Grigorov wrote:
Hi,

Did you try with normal Java way: System.getProperty("os.name"); ?

no, blindingly obvious :( thank you

"com.mycompany.grp" % "artifact" % "3.0.0-SNAPSHOT" classifier System.getProperty("os.name").toLowerCase.substring(0,3)


--
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/-/0YyoWNOP6BMJ.
Reply all
Reply to author
Forward
0 new messages