How to include an external jar file into the jar with package?

前端 未结 7 2038
-上瘾入骨i
-上瘾入骨i 2020-12-08 02:26

My project has dependency on an external jar. I have created a directory lib and have copied the jar file into it. This is my build.sbt:

         


        
相关标签:
7条回答
  • 2020-12-08 02:41

    First of all, there is no need to specify unmanagedJars explicitly unless you have more complex configuration. Simply put your JAR files into the lib directory and they will be added to the classpath (docs).

    To package an executable JAR with all the necessary dependencies use one of the SBT plugins: sbt-onejar or sbt-assembly. package simply packages your classes into a jar and doesn't bother with including dependencies whatsoever.

    The instructions in the docs of the plugins should be easy to guide you how to use them.

    0 讨论(0)
  • 2020-12-08 02:44

    I was racking my brain with resolvers and settingKey vars, but it seems this is the most straight-forward way (https://www.scala-sbt.org/release/docs/Library-Dependencies.html):

    resolvers += "Local Maven Repository" at "file://"+Path.userHome.absolutePath+"/.m2/repository"

    then just add to libraryDependencies as you normally would

    libraryDependencies += "<group>" % "<artifact>" % "<version>"

    0 讨论(0)
  • 2020-12-08 02:46

    Try this

    libraryDependencies += "com.acme.common" % "commonclass" % "1.0" from "file:///Users/bwong/git/perf-tools/commonclass/target/scala-2.11/commonclass_2.11-1.0.jar"
    

    I "sbt package" the common classproject and then just add that library dependency in my build.sbt in the dependent projects.

    Thanks to http://flummox-engineering.blogspot.com/2014/06/sbt-use-jar-file-for-librarydependencies.html for this hack.

    0 讨论(0)
  • 2020-12-08 02:46

    one of the solutions is, when you build that jar in sbt, do sbt publish-local

    0 讨论(0)
  • 2020-12-08 02:58

    Add this line your build.sbt and clean & compile your project.

    project/libs, where project is the root directory of your project, put your jars in libs folder.

    unmanagedBase <<= baseDirectory { base => base / "libs" }
    
    0 讨论(0)
  • 2020-12-08 02:58

    tl;dr Move project/build.sbt one level up where lib directory is and use sbt-assembly plugin (as described in the other answer by Aleksey).

    If the project directory layout is as you use for your project, it's incorrect since build.sbt does not belong to the same build where lib does.

    Since sbt is recursive this project/build.sbt describes the project's build that governs the main one. Quoting the docs:

    The project directory is another project inside your project which knows how to build your project. The project inside project can (in theory) do anything any other project can do. Your build definition is an sbt project.

    And the turtles go all the way down. If you like, you can tweak the build definition of the build definition project, by creating a project/project/ directory.

    0 讨论(0)
提交回复
热议问题