使用 SBT 创建独立 jar [英] Create standalone jar using SBT

查看:24
本文介绍了使用 SBT 创建独立 jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个重度 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(组织)和 artifact-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版本后缀,即在项目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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆