I am working on a Scala project on IntelliJ with SBT as my build tool. I started working sbt build recently.
This is my project structure:
This is my build.sbt file:
name := "AnalyzeTables"
version := "0.1"
scalaVersion := "2.11.8"
// https://mvnrepository.com/artifact/org.postgresql/postgresql
libraryDependencies += "org.postgresql" % "postgresql" % "42.1.4"
// https://mvnrepository.com/artifact/commons-codec/commons-codec
libraryDependencies += "commons-codec" % "commons-codec" % "1.13"
// https://mvnrepository.com/artifact/org.apache.commons/commons-lang3
libraryDependencies += "org.apache.commons" % "commons-lang3" % "3.8.1"
// https://mvnrepository.com/artifact/log4j/log4j
//libraryDependencies += "log4j" % "log4j" % "1.2.17"
I have Class.forName("org.postgresql.Driver) in my code to connect to database and query. Along with that, I have password decryption & logger added in the code. I am running the jar in the below format:
scala <jarname> <argument I use in my code>
The problem here is if I just submit in the way I mentioned above, I see ClassNotFoundException for postgres driver. So I add it to the classpath of the jar and submit it as below.
scala -cp /path/postgresql-42.1.4.jar <jarname> <argument I use in my code>
Now I get exception for Logger. So I add it to classpath again and it becomes:
scala -cp /path/postgresql-42.1.4.jar:/path/log4j-1.2.17.jar <jarname> <argument I use in my code>
Now I get exception for commons-codec, so I added that as well. scala -cp /path/postgresql-42.1.4.jar:/path/log4j-1.2.17.jar:/path/commons-codec-1.13.jar
Now that jar is running fine and I can see the result. So I have the dependencies added to the build.sbt file. I also did the below operation:
project structure -> Modules -> Dependencies -> + -> jars -> Add all the missing jars that are giving problems
If I remove all the -cp
parameters and submit the jar with just: scala <jarname> <argument>
it goes back again to ClassNotFoundException to postgres jar.
So what is the point of adding dependencies on build.sbt file and then add them in the classpath again ?
Is there ay setting I am missing or am I looking it in the wrong way ?
Edit:
After suggestions I created a new project & copied all the code into it and added the plugin addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.5")
in a new file plugins.sbt
file which I created in the dir /project/ as shown in the image below.
I can see the plugin in the sbt-plugins dir. But when I build the jar once again and export, it still shows 11kb jar instead of a fat jar.