0

我正在使用 SBT 作为我的构建工具在 IntelliJ 上开发一个 Scala 项目。我最近开始工作 sbt build。这是我的项目结构: 在此处输入图像描述

这是我的 build.sbt 文件:

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"

我的代码中有 Class.forName("org.postgresql.Driver) 以连接到数据库和查询。除此之外,我还在代码中添加了密码解密和记录器。我正在以以下格式运行 jar:

scala <jarname> <argument I use in my code>

这里的问题是,如果我只是按照上面提到的方式提交,我会看到 Postgres 驱动程序的 ClassNotFoundException。所以我将它添加到 jar 的类路径中并提交如下。

scala -cp /path/postgresql-42.1.4.jar <jarname> <argument I use in my code>

现在我得到了 Logger 的异常。所以我再次将它添加到类路径中,它变成:

scala -cp /path/postgresql-42.1.4.jar:/path/log4j-1.2.17.jar <jarname> <argument I use in my code>

现在我得到了 commons-codec 的例外,所以我也添加了它。scala -cp /path/postgresql-42.1.4.jar:/path/log4j-1.2.17.jar:/path/commons-codec-1.13.jar

现在 jar 运行良好,我可以看到结果。所以我将依赖项添加到 build.sbt 文件中。我还做了以下操作:

project structure -> Modules -> Dependencies -> + -> jars -> Add all the missing jars that are giving problems

如果我删除所有-cp参数并提交 jar:scala <jarname> <argument>它会再次返回 ClassNotFoundException 到 postgres jar。那么在 build.sbt 文件上添加依赖项然后再次将它们添加到类路径中的意义何在?是否有我丢失的设置或者我以错误的方式查看它?

编辑:根据建议,我创建了一个新项目并将所有代码复制到其中,并将插件添加到我在目录 /project/ 中创建addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.5")的新文件plugins.sbt中,如下图所示。 在此处输入图像描述

我可以在 sbt-plugins 目录中看到该插件。但是当我再次构建 jar 并导出时,它仍然显示 11kb jar 而不是胖 jar。

4

0 回答 0