Skip to content

Commit 79ccdf6

Browse files
authored
FIX #1491 DockerPlugin doesn't work with multiple main classes (#1535)
* FIX #1491 DockerPlugin doesn't work with multiple main classes by default * scalafmt
1 parent f209010 commit 79ccdf6

8 files changed

Lines changed: 49 additions & 5 deletions

File tree

src/main/resources/com/typesafe/sbt/packager/archetypes/scripts/ash-template

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,16 @@ ${{template_declares}}
109109

110110
process_args "$@"
111111

112+
# Fallback to custom mainclass if main class is not provided (this is the case if the JAR contains multiple apps)
113+
if [ "$app_mainclass" = "" ] || [ $custom_mainclass ];then
114+
if [ "$custom_mainclass" = "" ]; then
115+
echo "You need to pass -main argument."
116+
exit 1
117+
fi
118+
119+
app_mainclass=$custom_mainclass
120+
fi
121+
112122
java_cmd="$(get_java_cmd)"
113123

114124
# If a configuration file exist, read the contents to $opts

src/main/resources/com/typesafe/sbt/packager/archetypes/scripts/bash-template

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,16 @@ run() {
246246
mainclass=("${app_mainclass[@]}")
247247
fi
248248

249+
# Fallback to custom mainclass if main class is not provided (this is the case if the JAR contains multiple apps)
250+
if [ "$app_mainclass" = "" ] || [ $custom_mainclass ];then
251+
if [ "$custom_mainclass" = "" ]; then
252+
echo "You need to pass -main argument."
253+
exit 1
254+
fi
255+
256+
app_mainclass=$custom_mainclass
257+
fi
258+
249259
# 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.
250260
if [[ "$JAVA_OPTS" != "" ]]; then
251261
java_opts="${JAVA_OPTS}"

src/main/scala/com/typesafe/sbt/packager/archetypes/scripts/CommonStartScriptGenerator.scala

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,20 @@ trait CommonStartScriptGenerator {
8989
): Seq[(File, String)] = {
9090
val classAndScriptNames = ScriptUtils.createScriptNames(discoveredMainClasses)
9191
ScriptUtils.warnOnScriptNameCollision(classAndScriptNames, log)
92-
classAndScriptNames.map {
93-
case (qualifiedClassName, scriptName) =>
94-
val newConfig = config.withScriptName(scriptName)
95-
createMainScript(qualifiedClassName, newConfig, targetDir, discoveredMainClasses)
96-
}
92+
93+
classAndScriptNames
94+
.find {
95+
case (_, script) => script == config.executableScriptName
96+
}
97+
.map(_ => classAndScriptNames)
98+
.getOrElse(
99+
classAndScriptNames ++ Seq("" -> config.executableScriptName)
100+
) // empty string to enforce the custom class in scripts
101+
.map {
102+
case (qualifiedClassName, scriptName) =>
103+
val newConfig = config.withScriptName(scriptName)
104+
createMainScript(qualifiedClassName, newConfig, targetDir, discoveredMainClasses)
105+
}
97106
}
98107

99108
private[this] def mainScriptName(config: ScriptConfig): String =
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
enablePlugins(JavaAppPackaging, AshScriptPlugin)
2+
3+
name := "multi-main-name"
4+
5+
version := "0.1.0"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
addSbtPlugin("com.github.sbt" % "sbt-native-packager" % sys.props("project.version"))
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
object Main1 extends App {
2+
println("main1")
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
object Main2 extends App {
2+
println("main2")
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Stage the distribution and ensure files show up.
2+
> docker:stage
3+
$ exec grep -q -F 'RUN ["chmod", "u+x,g+x", "/4/opt/docker/bin/multi-main-name"]' target/docker/stage/Dockerfile

0 commit comments

Comments
 (0)