Adrian
Adrian

Reputation: 5681

How to run jar generated by sbt package via sbt

I have a jar that was created by running sbt package

I've already set the main file in the jar in build.sbt

mainClass in (Compile, packageBin) := Some("com.company.mysql.Main")
addCommandAlias("updatemysql", "runMain com.company.mysql.Main")

I've tried

sbt "runMain target/scala-2.12/update-mysql_2.12-0.1-SNAPSHOT.jar"
sbt target/scala-2.12/update-mysql_2.12-0.1-SNAPSHOT.jar com.company.mysql.Main
sbt target/scala-2.12/update-mysql_2.12-0.1-SNAPSHOT.jar:com.company.mysql.Main
sbt update-mysql-assembly-0.1-SNAPSHOT.jar/run

sbt run update-mysql-assembly-0.1-SNAPSHOT.jar 

^ this gives No main class detected even though main class is set in build.sbt as shown a few lines above.

I need to run the jar through sbt because it's the only way I know how to overwrite config file that is contained in the jar using -Dpath.to.config.param=new_value

Upvotes: 1

Views: 984

Answers (1)

Mateusz Kubuszok
Mateusz Kubuszok

Reputation: 27595

In sbt run and runMain use classpath containing all the dependencies as well as folders with outputs of compilation tasks - which means that none of them takes JAR as an argument.

I think it would be possible to run this particular JAR from sbt by writing a custom task that would depend on output of package task (that is JAR filepath value) and run it as external process... though from the question it seems that this is not the actual problem.

The actual problem is running the JAR with flags passed into JVM instead of program itself which can be achieved by something like:

# clean assembly ensures that there is only 1 JAR in target
# update-mysql_2.12-*.jar picks the only JAR no matter what is its version
# -D arguments NEED to be passed before -jar to pass it to JVM and not the JAR
sbt clean assembly && \
java -Dpath.to.config.param=new_value -jar target/scala-2.12/update-mysql_2.12-*.jar

Upvotes: 2

Related Questions