SBT+spring-boot: “在 META-INF/spring.factories 中找不到自动配置类”

huangapple go评论109阅读模式
英文:

SBT+spring-boot: " No auto configuration classes found in META-INF/spring.factories"

问题

我有一个用Scala编写的Spring Boot应用程序。我正在使用SBT来构建项目。我已经使用"sbt-assembly"生成了一个包含所有必需依赖项的大而全的JAR文件。

sbt版本 = 1.3.5
scala版本 := "2.13.1"

以下是我的build.sbt文件

  1. import BuildUtils.readVersion
  2. 名称 := "DiscoveryEngine"
  3. 版本 := "0.1"
  4. scala版本 := "2.13.1"
  5. 组织 := "com.javasree.scala.discoveryengine"
  6. 发布Maven风格 := true
  7. 懒惰的val artifactoryRepo = settingKey[sbt.librarymanagement.Resolver]("Artifactory repository value")
  8. isSnapshot := {
  9. sys.props.getOrElse("DISCOVERY_ENGINE_SNAPSHOT", sys.env.getOrElse("DISCOVERY_ENGINE_SNAPSHOT", "true")) match {
  10. case "true" | "1" => true
  11. case "false" | "0" => false
  12. case _ => println("将默认值设置为true'SNAPSHOT'")
  13. true
  14. }
  15. }
  16. 在(Compile, packageDoc)中发布Artifact := false
  17. 在(Compile, packageSrc)中发布Artifact := false
  18. /*
  19. 如果环境变量(DISCOVERY_ENGINE_SNAPSHOT)设置为true,则构建将执行“快照”
  20. 否则将是“发布”
  21. * */
  22. 版本 := {
  23. readVersion("app-build")+
  24. (isSnapshot.value match {
  25. case true => "-SNAPSHOT"
  26. case false => ""
  27. })
  28. }
  29. artifactoryRepo := {
  30. val repoURL = "http://localhost:8081/artifactory"
  31. (isSnapshot.value match {
  32. case true => "jfrog-snapshot" at s"${repoURL}/javasree-sbt-snapshot;build.timestamp=" + new java.util.Date().getTime
  33. case false => "jfrog-remote" at s"${repoURL}/javasree-sbt-local"
  34. })
  35. }
  36. publishTo := Some( artifactoryRepo.value )
  37. credentials += Credentials("Artifactory Realm", "localhost", "admin", "password")
  38. assembly中的assemblyMergeStrategy := {
  39. case PathList("META-INF", xs @ _*) => MergeStrategy.discard
  40. case x => MergeStrategy.last
  41. }
  42. libraryDependencies ++= Seq(
  43. "com.google.code.gson" % "gson" % "2.8.2" % "provided"
  44. )++Dependencies.springDependencies

我对Scala和SBT还不太熟悉。其中一些代码是我通过谷歌搜索盲目添加的,只是为了确保我的构建能够正常工作。其中一个这样的代码是assembly mergestrategy。

当我遇到“重复项查找错误”时,我尝试了不同的mergeStrategy代码。经过多次尝试,我最终成功生成了大而全的JAR文件。最初,我的大而全的JAR文件并没有成功运行,因为它又出现了不同的“无法加载主类”的错误。

上述的构建脚本是我最终生成大而全的JAR文件的脚本,我能够运行这个JAR文件。这次它没有给我“无法加载主类”的错误,而是给了我一个新的错误,如下所示。

  1. $ java -jar DiscoveryEngine-assembly-1.0-SNAPSHOT.jar
  2. 00:44:54.232 [main] DEBUG org.springframework.web.context.support.StandardServletEnvironment - Activating profiles []
  3. . ____ _ __ _ _
  4. /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
  5. ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
  6. \\/ ___)| |_)| | | | | || (_| | ) ) ) )
  7. ' |____| .__|_| |_|_| |_\__, | / / / /
  8. =========|_|==============|___/=/_/_/_/
  9. :: Spring Boot :: (v1.0-SNAPSHOT)
  10. 00:44:54.323 [main] INFO scala.App - Starting App v1.0-SNAPSHOT on LAPTOP-U3JTGOFA with PID 5648 (H:\Projects\Scala\DiscoryEngine\target\scala-2.13\DiscoveryEngine-assembly-1.0-SNAPSHOT.jar started by sree in H:\Projects\Scala\DiscoryEngine\target\scala-2.13)
  11. 00:44:54.324 [main] DEBUG scala.App - Running with Spring Boot v1.0-SNAPSHOT, Spring v1.0-SNAPSHOT
  12. 00:44:54.324 [main] INFO scala.App - No active profile set, falling back to default profiles: default
  13. ...

我确信这与我的sbt-assembly插件和我的MergeStrategy有关,但是在Stack Overflow的其他线程中没有一个解决方案能够解决我的问题,我别无选择,只能向Stack Overflow的专家们寻求帮助,希望能够得到正确的答案。提前感谢您的帮助。

英文:

I have a spring boot application written in Scala. I am using SBT to build the project. I have used "sbt-assembly" to generate the big fat jar with all the required dependencies added as one.

sbt.version = 1.3.5
scalaVersion := "2.13.1"

below is my build.sbt file

  1. import BuildUtils.readVersion
  2. name := "DiscoveryEngine"
  3. version := "0.1"
  4. scalaVersion := "2.13.1"
  5. organization := "com.javasree.scala.discoveryengine"
  6. publishMavenStyle := true
  7. lazy val artifactoryRepo = settingKey[sbt.librarymanagement.Resolver]("Artifactory repository value")
  8. isSnapshot := {
  9. sys.props.getOrElse("DISCOVERY_ENGINE_SNAPSHOT", sys.env.getOrElse("DISCOVERY_ENGINE_SNAPSHOT", "true")) match {
  10. case "true" | "1" => true
  11. case "false" | "0" => false
  12. case _ => println("taking the default value as true for 'SNAPSHOT'")
  13. true
  14. }
  15. }
  16. publishArtifact in (Compile, packageDoc) := false
  17. publishArtifact in (Compile, packageSrc) := false
  18. /*
  19. if env variable(DISCOVERY_ENGINE_SNAPSHOT) is set to true then build will do "snapshot"
  20. or else it will be "release"
  21. * */
  22. version := {
  23. readVersion("app-build")+
  24. (isSnapshot.value match {
  25. case true => "-SNAPSHOT"
  26. case false => ""
  27. })
  28. }
  29. artifactoryRepo := {
  30. val repoURL = "http://localhost:8081/artifactory"
  31. (isSnapshot.value match {
  32. case true => "jfrog-snapshot" at s"${repoURL}/javasree-sbt-snapshot;build.timestamp=" + new java.util.Date().getTime
  33. case false => "jfrog-remote" at s"${repoURL}/javasree-sbt-local"
  34. })
  35. }
  36. publishTo := Some( artifactoryRepo.value )
  37. credentials += Credentials("Artifactory Realm", "localhost", "admin", "password")
  38. assemblyMergeStrategy in assembly := {
  39. case PathList("META-INF", xs @ _*) => MergeStrategy.discard
  40. case x => MergeStrategy.last
  41. }
  42. libraryDependencies ++= Seq(
  43. "com.google.code.gson" % "gson" % "2.8.2" % "provided"
  44. )++Dependencies.springDependencies

I am new to scala and sbt. some of the stuff were added blindly as a result of google search. just to make sure my build works. One such code is the assembly mergestrategy.

I have used different mergeStrategy code when i faced "deduplication found error". after several trail and error method i finally was able generate the fat jar. Initially my fat jar did not ran successfully as it failed again with a different error "failed to load main ....".
The above build script is my final script that is generating the fat jar and i was able to run the jar. This time it is not giving "failed to load main..." error but instead giving me new different error as below.

  1. $ java -jar DiscoveryEngine-assembly-1.0-SNAPSHOT.jar
  2. 00:44:54.232 [main] DEBUG org.springframework.web.context.support.StandardServletEnvironment - Activating profiles []
  3. . ____ _ __ _ _
  4. /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
  5. ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
  6. \\/ ___)| |_)| | | | | || (_| | ) ) ) )
  7. ' |____| .__|_| |_|_| |_\__, | / / / /
  8. =========|_|==============|___/=/_/_/_/
  9. :: Spring Boot :: (v1.0-SNAPSHOT)
  10. 00:44:54.323 [main] INFO scala.App - Starting App v1.0-SNAPSHOT on LAPTOP-U3JTGOFA with PID 5648 (H:\Projects\Scala\DiscoryEngine\target\scala-2.13\DiscoveryEngine-assembly-1.0-SNAPSHOT.jar started by sree in H:\Projects\Scala\DiscoryEngine\target\scala-2.13)
  11. 00:44:54.324 [main] DEBUG scala.App - Running with Spring Boot v1.0-SNAPSHOT, Spring v1.0-SNAPSHOT
  12. 00:44:54.324 [main] INFO scala.App - No active profile set, falling back to default profiles: default
  13. 00:44:54.325 [main] DEBUG org.springframework.boot.SpringApplication - Loading source class com.javasree.scala.discoveryengine.DiscoveryEngine
  14. 00:44:54.367 [main] DEBUG org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext - Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@1445d7f
  15. 00:44:54.378 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
  16. 00:44:54.477 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
  17. java.lang.IllegalArgumentException: No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.
  18. at org.springframework.util.Assert.notEmpty(Assert.java:464)
  19. at org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.getCandidateConfigurations(AutoConfigurationImportSelector.java:173)
  20. at org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.getAutoConfigurationEntry(AutoConfigurationImportSelector.java:116)
  21. at org.springframework.boot.autoconfigure.AutoConfigurationImportSelector$AutoConfigurationGroup.process(AutoConfigurationImportSelector.java:396)
  22. at org.springframework.context.annotation.ConfigurationClassParser$DeferredImportSelectorGrouping.getImports(ConfigurationClassParser.java:875)
  23. at org.springframework.context.annotation.ConfigurationClassParser$DeferredImportSelectorGroupingHandler.processGroupImports(ConfigurationClassParser.java:801)
  24. at org.springframework.context.annotation.ConfigurationClassParser$DeferredImportSelectorHandler.process(ConfigurationClassParser.java:771)
  25. at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:185)
  26. at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:315)
  27. at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:232)
  28. at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:275)
  29. at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:95)
  30. at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:705)
  31. at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:531)
  32. at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
  33. at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:743)
  34. at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:390)
  35. at org.springframework.boot.SpringApplication.run(SpringApplication.java:312)
  36. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1214)
  37. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1203)
  38. at com.javasree.scala.discoveryengine.DiscoveryEngine$.delayedEndpoint$com$javasree$scala$discoveryengine$DiscoveryEngine$1(DiscoveryEngine.scala:13)
  39. at com.javasree.scala.discoveryengine.DiscoveryEngine$delayedInit$body.apply(DiscoveryEngine.scala:12)
  40. at scala.Function0.apply$mcV$sp(Function0.scala:39)
  41. at scala.Function0.apply$mcV$sp$(Function0.scala:39)
  42. at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:17)
  43. at scala.App.$anonfun$main$1(App.scala:73)
  44. at scala.App.$anonfun$main$1$adapted(App.scala:73)
  45. at scala.collection.IterableOnceOps.foreach(IterableOnce.scala:553)
  46. at scala.collection.IterableOnceOps.foreach$(IterableOnce.scala:551)
  47. at scala.collection.AbstractIterable.foreach(Iterable.scala:921)
  48. at scala.App.main(App.scala:73)
  49. at scala.App.main$(App.scala:71)
  50. at com.javasree.scala.discoveryengine.DiscoveryEngine$.main(DiscoveryEngine.scala:12)
  51. at com.javasree.scala.discoveryengine.DiscoveryEngine.main(DiscoveryEngine.scala)
  52. 00:44:54.479 [main] DEBUG org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext - Closing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@1445d7f, started on Tue May 05 00:44:54 IST 2020

I am sure it is some thing to do with my sbt-assembly plugin and with my MergeStrategy but non of the solutions in the other thread of SO solved my issue and i left with no other choice other than asking the experts in SO and cross my fingers to get the right answer. Thank you in advance.

答案1

得分: 2

None of the Merge strategy properly merge spring.factories files.

So here I did

I created an assembly JAR with Scala and without library dependencies

Sbt assembly

Then I used the sbt package native plugin to collect all of the dependency JARs in my my_lib folder

  1. Sbt universal:packageBin

And I executed the JAR with the following command

  1. java -classpath "my_lib/*;my jar" my.main
英文:

None of the Merge strategy properly merge spring.factories files.

So here I did

I created assembly jar with scala and without library dependencies

Sbt assembly

Then I use sbt package native plugin to get all of the dependency jar in my my_lib folder

  1. Sbt univarsal:packageBin

And I run the jar by command

  1. Java -classpath "my_lib/*;my jar" my.main

huangapple
  • 本文由 发表于 2020年5月5日 03:16:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/61599894.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定