scalaintellij-ideasbth2

Add a dependency for code in build.sbt (H2)


Issue with Starting H2 Database Browser in Play Framework

I'm currently working with the Play Framework and have encountered an issue with starting the H2 database browser. Here's the background:

  1. Previous Setup:

    • I used to start the H2 database browser using the terminal with the following commands:
      sbt
      h2-browser
      run
      
  2. Switched to IntelliJ:

    • I later configured the Play application to run from IntelliJ, which improved my development workflow.
  3. H2 Browser Configuration:

    • However, I faced a challenge with starting the H2 database browser within IntelliJ. To tackle this, I turned to ChatGPT for assistance.
  4. Adding H2 Dependency:

    • I added the H2 dependency to the build.sbt file as follows:
      libraryDependencies += "com.h2database" % "h2" % "2.1.214"
      lazy val startH2Browser = taskKey[Unit]("Starts the H2 Browser")
      startH2Browser := {
        println("Starting H2 Browser...")
        org.h2.tools.Server.createWebServer("-web", "-webAllowOthers", "-webPort","8082").start()
      }
      
      playRunHooks += {
        new play.sbt.PlayRunHook {
          override def beforeStarted(): Unit = {
            startH2Browser.value
          }
        }
      }
      
      
  5. Encountered Error:

    • Unfortunately, when I tried to start the H2 browser, I encountered the following error:
      build.sbt:55: error: object h2 is not a member of package org
        org.h2.tools.Server.createWebServer("-web", "-webAllowOthers", "-webPort", "8082").start()
            ^
      [error] Type error in expression
      Project loading failed: (r)etry, (q)uit, (l)ast, or (i)gnore?
      
  6. Confirmation of H2 Package:

    • I would like to confirm that SBT has successfully indexed and downloaded the H2 package.

Can someone please advise me on how to properly import the H2 dependency in the build.sbt file and resolve this issue?


Solution

  • by forking sbt command process and running h2-browser

    lazy val startH2Browser = taskKey[Unit]("Starts the H2 Browser")
    startH2Browser := {
      println("Starting H2 Browser...")
      Command.process("h2-browser",state.value)
    }
    playRunHooks += {
      new play.sbt.PlayRunHook {
        override def beforeStarted(): Unit = {
          startH2Browser.value
        }
      }
    }