如何在构建中设置主类?

发布于 2024-11-17 06:41:33 字数 557 浏览 3 评论 0原文

在 sbt run 后,我有多种主类选择。

我想设置一个主类,所以我在 build.sbt 中编写:

mainClass := Some("aMainClass")

但是 sbt 失败:

build.sbt:1: error: not found: value aMainClass

我也尝试过 project/Project.scala 文件:

import sbt._
  class ExecutableProject(info: ProjectInfo) extends DefaultProject(info)  {
  override def mainClass = Some("aMainClass")
}

错误:

 project/Project.scala:3: not found: type aMainClass

如何在构建中设置主类?

Upon sbt run I have multiple choices of main class.

I would like to set a main class so I've writen in build.sbt:

mainClass := Some("aMainClass")

But sbt fails with:

build.sbt:1: error: not found: value aMainClass

I've also tried with project/Project.scala file :

import sbt._
  class ExecutableProject(info: ProjectInfo) extends DefaultProject(info)  {
  override def mainClass = Some("aMainClass")
}

error :

 project/Project.scala:3: not found: type aMainClass

How to set the main class in a build?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云淡月浅 2024-11-24 06:41:33

主类必须与包完全限定:

Compile/mainClass := Some("myPackage.aMainClass")

这将适用于运行,并且在使用包任务时它将在清单中设置主类。这些任务的主类可以单独设置,如下所示:

mainClass in (Compile, run) := Some("myPackage.aMainClass")
mainClass in (Compile, packageBin) := Some("myPackage.anotherMainClass")

注意:

mainClass := Some("myPackage.aMainClass")

不执行任何操作。如果您将其放入构建文件中,您将不会收到任何警告,表明它不执行任何操作。

The main Class must be fully qualified with the package:

Compile/mainClass := Some("myPackage.aMainClass")

This will work for run and it will set the Main-Class in the Manifest when using the package task. The main class for these tasks can be set separately as in:

mainClass in (Compile, run) := Some("myPackage.aMainClass")
mainClass in (Compile, packageBin) := Some("myPackage.anotherMainClass")

Note:

mainClass := Some("myPackage.aMainClass")

does nothing. If you put this in your build file you will receive no warning that it does nothing.

娇纵 2024-11-24 06:41:33

据我所知,sbt 期望您的项目中有一个完全限定的类/对象名称。例如,如果你的主类是这样的:

package prog

object Main extends App {
    // Hic sunt dracones
}

那么你必须给你的主类这样:

mainClass := Some("prog.Main")

你会得到一个类型错误,因为该类型不是简单地找到的。

As far as I know, sbt expects here a fully qualified class/object name within your project. For example, if your main class is like this:

package prog

object Main extends App {
    // Hic sunt dracones
}

then you would have to give your main class like this:

mainClass := Some("prog.Main")

You get a type error because that type is not simply found.

樱娆 2024-11-24 06:41:33

我的 sbt 版本是 sbt 1.5.4 (Ubuntu Java 11.0.11)。

我发现 mainClass in (Compile, run) :=Some("Hello") 在我的 build.sbt 中不起作用。

我终于在 中找到了 1.x 版本的官方示例在这里

// set the main class for packaging the main jar
// 'run' will still auto-detect and prompt
// change Compile to Test to set it for the test jar
Compile / packageBin / mainClass := Some("myproject.MyMain"),

// set the main class for the main 'run' task
// change Compile to Test to set it for 'test:run'
Compile / run / mainClass := Some("myproject.MyMain"),

您可以将其添加到您的设置中。这对我有用。

但我不知道如何将这些命令集成到一个中。

My sbt version is sbt 1.5.4 (Ubuntu Java 11.0.11).

And I find mainClass in (Compile, run) :=Some("Hello") doesn't work in my build.sbt.

I finally find the offical example for 1.x version in here

// set the main class for packaging the main jar
// 'run' will still auto-detect and prompt
// change Compile to Test to set it for the test jar
Compile / packageBin / mainClass := Some("myproject.MyMain"),

// set the main class for the main 'run' task
// change Compile to Test to set it for 'test:run'
Compile / run / mainClass := Some("myproject.MyMain"),

you can add it to your settings. It works for me.

But I don't know how to integrate those commands into one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文