使用SBT创建独立的jar [英] Create standalone jar using SBT
问题描述
我是Maven的重度用户,现在我逐渐将SBT用于一些项目.
I was a heavy Maven user and now I'm gradually using SBT for some of my projects.
我想知道如何使用SBT创建一个独立的Java项目?该项目应打包为一个JAR文件,并且该JAR文件将用作另一个SBT项目中的依赖项.
I'd like to know how could I use SBT to create a standalone Java project? This project should be packaged as a JAR file and this JAR file would be used as a dependency in another SBT project.
在Maven中,我可以在我的pom.xml
中告诉我在构建它时应产生什么类型的工件.我在SBT中可以做些类似的事情吗?
In Maven, I could tell in my pom.xml
what type of artifact it should produce when I build it. Is there something similar that I can do in SBT?
推荐答案
独立与使项目可用作依赖项或另一个项目之间是有区别的.在第一种情况下,您将使用诸如 sbt-assembly 之类的插件.它将要做的是创建一个一个 jar文件,其中包含项目类文件及其所有依赖项.如果编写应用程序,您将获得一个可双击的jar,可以在任何地方执行.
There is a difference between standalone and making a project useable as a dependency or another project. In the first case, you would use a plugin such as sbt-assembly. What it will do is create one jar file containing the project class files along with all of its dependencies. If you write an application, what you get is a double-clickable jar that you can execute from anywhere.
如果要将项目A用作另一个项目B的依赖项,则有不同的选择.您可以使用sbt package
(@Channing Walton的答案)打包A的类文件.然后,您可以将生成的.jar
文件拖放到项目B的lib
目录中.但是,如果A还需要库,则必须确保它们也位于项目B的库中.
If you want to use your project A as a dependency for another project B, you have different options. You could just package the class files of A, using sbt package
(answer of @Channing Walton). Then you could drop the resulting .jar
file in the lib
directory of project B. However, if A also requires libraries, you must make sure that they also end up in project B's libraries.
一种更好的方法是发布您的项目.您可以使用sbt publish-local
完全在本地计算机上执行此操作.它将把package
生成的jar存储在一个特殊的本地目录中,该目录可以从另一个项目中的sbt进行访问,以及一个包含A依赖项的POM文件.它将使用组ID(组织)和工件-ID(名称)和项目A的版本.例如,在build.sbt
:
A better approach is to publish your project. You can do that purely on your local machine, using sbt publish-local
. That will store the jar as produced by package
in a special local directory which can be accessed from sbt in another project, along with a POM file that contains the dependencies of A. It will use a group-ID (organization) and artifact-ID (name) and a version of your project A. For example, in build.sbt
:
name := "projecta"
version := "0.1.0-SNAPSHOT"
organization := "com.github.myname"
scalaVersion := "2.10.3"
publishMavenStyle := true
使用sbt publish-local
发布后,可以将以下依赖项添加到项目B:
After publishing with sbt publish-local
, you can add the following dependency to your project B:
libraryDependencies += "com.github.myname" %% "projecta" % "0.1.0-SNAPSHOT"
如果您有一个纯Java项目,则可以省略Scala版本后缀,即在Project A中:
If you have a pure Java project, you can omit the Scala version suffix, i.e. in Project A:
crossPaths := false
autoScalaLibrary := false
然后在项目B中:
libraryDependencies += "com.github.myname" % "projecta" % "0.1.0-SNAPSHOT"
(在组和工件ID之间仅使用一个%
字符).
(using only one %
character between group and artifact ID).
有关发布的更多信息,请参见 sbt文档.
More on publishing in the sbt documentation.
这篇关于使用SBT创建独立的jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!