3

I have an SBT project that contains multiple classes with main, i.e. MyClass extends App. One-jar works fine when there is only one such class. If it finds multiple classes it prompts me to choose which one I want to package:

> one-jar

Multiple main classes detected, select one to run:

 [1] com.smth.AppOne
 [2] com.smth.AppTwo

Enter number: 

I'd like to configure one-jar to automatically package all main classes. In documentation it defines default main class as mainClass in run in Compile, so it looks like it expects only one value.

If this is not possible I'm curious why not. :)

For now I can only think of some hacks like creating a surrogate project for each jar or setting a mainClass in SBT each time before calling one-jar (multiple times per build). These approaches obviously have their deficiencies.

yǝsʞǝla
  • 16,272
  • 2
  • 44
  • 65

4 Answers4

3

Use the following to set up the default main class:

mainClass in (Compile, run) := Some("com.smth.AppOne")

Source: This stackoverflow Q&A

Community
  • 1
  • 1
Mikaël Mayer
  • 10,425
  • 6
  • 64
  • 101
2

It's not about SBT or onejar plugin. When you pack your project into the jar file, both main classes would be packed. JAR File specification defines that you can have as many classes with main() method as you want, but there should be only one class with main() method defined in Main-Class attribute per JAR

4lex1v
  • 21,367
  • 6
  • 52
  • 86
  • Yep, so what I'm trying to achieve is for onejar to create one `*.jar` file per each class with `main()` that it finds in my src. It might happen that jars will be identical, but `Main-class` attr will be set to a different value in each one. Onejar can clearly discover these candidates. All I want is for onejar to package each one in a separate jar. – yǝsʞǝla Dec 17 '13 at 14:07
  • @AlekseyIzmailov There is a task `discoveredMainClasses in Compile`, make your custom task which depends on it and for each main class run onejar – 4lex1v Dec 17 '13 at 14:27
  • Thanks @Alexlv, I'll try that. I was hoping to avoid creating custom tasks :). – yǝsʞǝla Dec 17 '13 at 14:30
  • @AlekseyIzmailov If you don't want to make a custom task, then do a simple command. You can avoid task dependency and call execute other task with `Project.runTask` – 4lex1v Dec 17 '13 at 14:54
0

From documentation:

mainClass in oneJar := Some("com.acme.Woozler")

Try to add

mainClass in oneJar := Some("com.smth.AppOne")

in you configuration

1esha
  • 1,722
  • 11
  • 17
  • This does not solve the problem at least because I have to manually specify all class names. Furthermore I think it will just overwrite the setting instead of adding to a Seq of values (I didn't try though). – yǝsʞǝla Dec 17 '13 at 14:10
0

I would try sbt multi-project builds. You should be able to set a main class per project.

Brandon Bradley
  • 599
  • 4
  • 11