sbt 如何生成多个具有不同分类器和不同主类的 jar? [英] How can sbt generate several jars with different classifiers and different main classes?

查看:23
本文介绍了sbt 如何生成多个具有不同分类器和不同主类的 jar?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 sbt 来打包我的项目.但是,我的项目中有两个主要类基于相同的来源.我想为这两个主要类创建两个具有不同分类器的 jar.由于这两个主要类共享项目中的大部分源代码,因此我无法将它们分成两个项目.因此,有人可以帮我修改 build.sbt 以实现这样的目标吗?

I am using sbt to package my project. However there are two main classes in my project based on the same source. And I want to create two jars with different classifiers for these two main classes. Since these two main classes shares most source code in the project, I cannot separate them into two projects. Therefore, could anyone help me modify the build.sbt to achieve such a goal?

推荐答案

TL;DR:您需要做的就是在 sbt 中定义 mainClass.

TL;DR: All you need to do is define the mainClass in sbt.

现在让我们描述如何去做.

Now let's describe how to do it.

在根目录,我们需要创建sbt.build.其内容将是:

At the root, we need to create sbt.build. Its content is going to be:

lazy val root = project.settings(
    version := "0.0.1-SNAPSHOT",
    scalaVersion := "2.12.12"
).aggregate(moduleA, moduleB)

lazy val commons = project
lazy val moduleA = project.settings(mainClass in (Compile, packageBin) := Some("MainClassA")).dependsOn(commons)
lazy val moduleB = project.settings(mainClass in (Compile, packageBin) := Some("MainClassB")).dependsOn(commons)

您可以阅读更多关于 sbt 聚合,以及关于 如何指定要运行的主要方法/类.该项目的结构如下:

You can read more about sbt aggregation, and about How to specify a main method/class to run. The project is going to be structured as follows:

/
|-build.sbt
|-project
    |-plugins.sbt
|-commons
    |-src
        |-main
            |scala
                |-SomeClass.scala
|-moduleA
    |-src
        |-main
            |-scala
                |-MainClassA.scala
|-moduleB
    |-src
        |-main
            |-scala
                |-MainClassB.scala

SomeClass.scala 的内容是:

object SomeClass {
  def doPrint() = {
    println("I am in commons")
  }
}

MainClassA.scala的内容是(假设scala版本到2.12,scala 2.13不需要扩展App annymore):

The content of MainClassA.scala is(assuming scala version is up to 2.12 . In scala 2.13 you do not need to extend App annymore.):

object MainClassA extends App {

  override def main(args: Array[String]): Unit = {
      SomeClass.doPrint()
      println("Hello MainClassA")
  }
}

MainClassB.scala 的内容是(这里注释相同):

The content of MainClassB.scala is(same comment here):

object MainClassB extends App {

  override def main(args: Array[String]): Unit = {
      SomeClass.doPrint()
      println("Hello MainClassB")
  }
}

plugins.sbt 的内容为:

addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.15.0")

现在,一切准备就绪,我们需要执行.我们安装了 sbt-assembly 插件,因此我们可以运行:sbt assembly.

Now, we have everything ready, and we need to execute. We installed the sbt-assembly plugin, hence we can run: sbt assembly.

这将创建一些文件,但我们会对 2 感兴趣:

This will create some files, but we will be interested in 2:

./moduleA/target/scala-2.12/moduleA-assembly-0.1.0-SNAPSHOT.jar 
./moduleB/target/scala-2.12/moduleB-assembly-0.1.0-SNAPSHOT.jar

然后,当运行 java -jar ./moduleA/target/scala-2.12/moduleA-assembly-0.1.0-SNAPSHOT.jar 时,我们得到:

Then, when running java -jar ./moduleA/target/scala-2.12/moduleA-assembly-0.1.0-SNAPSHOT.jar we get:

I am in commons
Hello MainClassA

并且,当运行 java -jar ./moduleB/target/scala-2.12/moduleB-assembly-0.1.0-SNAPSHOT.jar 时,我们得到:

And, when running java -jar ./moduleB/target/scala-2.12/moduleB-assembly-0.1.0-SNAPSHOT.jar we get:

I am in commons
Hello MainClassB

附言真的不建议从同一个构建中创建多个 jar.它会造成巨大的风险,即错过依赖项并获得缺少方法/类的运行时异常.

P.S. It is really not recommended to create multiple jars from the same build. It creates a huge risk to miss dependencies and get a runtime exception of missing methods/classes.

这篇关于sbt 如何生成多个具有不同分类器和不同主类的 jar?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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