Skip to content

Commit

Permalink
FIX #1491 DockerPlugin doesn't work with multiple main classes (#1535)
Browse files Browse the repository at this point in the history
* FIX #1491 DockerPlugin doesn't work with multiple main classes by default

* scalafmt
  • Loading branch information
muuki88 authored Feb 9, 2023
1 parent f209010 commit 79ccdf6
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ ${{template_declares}}

process_args "$@"

# Fallback to custom mainclass if main class is not provided (this is the case if the JAR contains multiple apps)
if [ "$app_mainclass" = "" ] || [ $custom_mainclass ];then
if [ "$custom_mainclass" = "" ]; then
echo "You need to pass -main argument."
exit 1
fi

app_mainclass=$custom_mainclass
fi

java_cmd="$(get_java_cmd)"

# If a configuration file exist, read the contents to $opts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,16 @@ run() {
mainclass=("${app_mainclass[@]}")
fi

# Fallback to custom mainclass if main class is not provided (this is the case if the JAR contains multiple apps)
if [ "$app_mainclass" = "" ] || [ $custom_mainclass ];then
if [ "$custom_mainclass" = "" ]; then
echo "You need to pass -main argument."
exit 1
fi

app_mainclass=$custom_mainclass
fi

# Now we check to see if there are any java opts on the environment. These get listed first, with the script able to override them.
if [[ "$JAVA_OPTS" != "" ]]; then
java_opts="${JAVA_OPTS}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,20 @@ trait CommonStartScriptGenerator {
): Seq[(File, String)] = {
val classAndScriptNames = ScriptUtils.createScriptNames(discoveredMainClasses)
ScriptUtils.warnOnScriptNameCollision(classAndScriptNames, log)
classAndScriptNames.map {
case (qualifiedClassName, scriptName) =>
val newConfig = config.withScriptName(scriptName)
createMainScript(qualifiedClassName, newConfig, targetDir, discoveredMainClasses)
}

classAndScriptNames
.find {
case (_, script) => script == config.executableScriptName
}
.map(_ => classAndScriptNames)
.getOrElse(
classAndScriptNames ++ Seq("" -> config.executableScriptName)
) // empty string to enforce the custom class in scripts
.map {
case (qualifiedClassName, scriptName) =>
val newConfig = config.withScriptName(scriptName)
createMainScript(qualifiedClassName, newConfig, targetDir, discoveredMainClasses)
}
}

private[this] def mainScriptName(config: ScriptConfig): String =
Expand Down
5 changes: 5 additions & 0 deletions src/sbt-test/docker/multiple-main-classes/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
enablePlugins(JavaAppPackaging, AshScriptPlugin)

name := "multi-main-name"

version := "0.1.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("com.github.sbt" % "sbt-native-packager" % sys.props("project.version"))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object Main1 extends App {
println("main1")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object Main2 extends App {
println("main2")
}
3 changes: 3 additions & 0 deletions src/sbt-test/docker/multiple-main-classes/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Stage the distribution and ensure files show up.
> docker:stage
$ exec grep -q -F 'RUN ["chmod", "u+x,g+x", "/4/opt/docker/bin/multi-main-name"]' target/docker/stage/Dockerfile

0 comments on commit 79ccdf6

Please sign in to comment.