scalasbtsbt-plugin

sbt scala tutorial for plugin tutorial is not adding in the grizzled plugin correctly


I'm trying to follow the steps listed here

https://www.scala-sbt.org/1.x/docs/Plugins.html#Using+a+library+in+a+build+definition+example

so I have project/plugins.sbt, (I changed this line b/c the original line wasn't installing due to prob the scala version mismatch)

// my line
libraryDependencies += "org.clapper" % "grizzled-scala_2.8.1" % "1.0.4"

// original line
libraryDependencies += "org.clapper" %% "grizzled-scala" % "1.0.4"

So now when I open my SBt, it appears to have pulled in the correct lib, but when I run sbt plugins, nothing shows up pertaining to Grizzled.

so When I eventually try to run sbt > eval grizzled.sys.os

I run into this problem:

enter image description here

I'm running 2.13.latest in the host project, What am I missing?


Solution

  • In sbt you have:

    What is a build definition?

    A build definition is defined in build.sbt, and it consists of a set of projects (of type Project). Because the term project can be ambiguous, we often call it a subproject in this guide.

    What is a Plugin?

    A plugin is a way to use external code in a build definition. A plugin can be a library used to implement a task (you might use Knockoff to write a markdown processing task). A plugin can define a sequence of sbt settings that are automatically added to all projects or that are explicitly declared for selected projects. For example, a plugin might add a proguard task and associated (overridable) settings. Finally, a plugin can define new commands (via the commands setting).


    In build.sbt you are going to declare the definition of your project (or multiple projects). You can set different attributes of your project such as the scala version, name of the project, authors, license, etc. Also you can define all the libraries that are required to build the project you define.

    Let's say that we want to create a project named hello-sbt, organization name Hello LLC, scala version 2.13.11 and scalatest as one of the dependency libraries. Our build.sbt should look like

    lazy val root = (project in file("."))
      .settings(
        name := "hello-sbt",
        organizationName := "Hello LLC"
        scalaVersion := "2.13.11",
        libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.16" % Test
      )
    

    Doing this, will allow us to put some scala files in src/main/scala and also execute them with the command sbt run (mean while there is a main method or the App trait is bein mixed). Also we will be able to execute the command sbt test because we are using a testing library (in this case scalatest) that will execute all the test we put in src/test/scala.

    Lets say that now we want to add an sbt plugin to our project, for example wartremover (it is a flexible Scala code linting tool). To install it, we only need to create a file named plugins.sbt inside the directory project and only add the following line

    addSbtPlugin("org.wartremover" % "sbt-wartremover" % "3.1.3")
    

    Just doing that we can use it in our build.sbt as it is detailed in the doc that explain how to install and setup.


    In the case of grizzled-scala, it's not a sbt plugin, it's a library. If we want to have that library available to be used in our build definition, adding as a dependency library of our project like this

    lazy val root = (project in file("."))
      .settings(
        name := "hello-sbt",
        organizationName := "Hello LLC"
        scalaVersion := "2.13.11",
        libraryDependencies ++= Seq(
          "org.clapper" %% "grizzled-scala" % "4.10.0",
          "org.scalatest" %% "scalatest" % "3.2.16" % Test
        )
      )
    

    will let us use the library as a dependency of the project, but not in the build definition. To use the library in the build definition file, you have to add it as a dependency but in the project/plugins.sbt file. Following the previous example I gave, the file with grizzled addded as dependency should look like

    addSbtPlugin("org.wartremover" % "sbt-wartremover" % "3.1.3")
    libraryDependencies += "org.clapper" %% "grizzled-scala" % "4.10.0"
    

    Once this is done, you can use the library in your build.sbt. For example, we can Define a new Task named printOperativeSystem that prints the name of the operative system that we are using. Now our build.sbt will look like

    import grizzled.sys.os
    
    lazy val root = (project in file("."))
      .settings(
        name := "hello-sbt",
        organizationName := "Hello LLC"
        scalaVersion := "2.13.11",
        libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.16" % Test
      )
    
    lazy val printOperativeSystem = taskKey[Unit]("Prints the name of the Operative System using grizzled library")
    printOperativeSystem := println(os.name)
    

    With this, we have a new task printOperativeSystem that we can execute from the command line

    > sbt printOperativeSystem
    Mac OS
    

    Or you can try to run eval grizzled.sys.os as it is showed in the docs you are following


    As a side note, I used grizzled-scala 4.10.0 because 1.0.4 is only released for scala 2.8.0 and 2.8.1. I think that could be the root cause of your problem. Also the error message you are receiving suggest to check version conflict. Remember that sbt is built on top of scala 2.12