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

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

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文件

import BuildUtils.readVersion

名称 := "DiscoveryEngine"
版本 := "0.1"
scala版本 := "2.13.1"
组织 := "com.javasree.scala.discoveryengine"
发布Maven风格 := true

懒惰的val artifactoryRepo = settingKey[sbt.librarymanagement.Resolver]("Artifactory repository value")

isSnapshot := {
  sys.props.getOrElse("DISCOVERY_ENGINE_SNAPSHOT", sys.env.getOrElse("DISCOVERY_ENGINE_SNAPSHOT", "true")) match {
    case "true" | "1" => true
    case "false" | "0" => false
    case _ => println("将默认值设置为true为'SNAPSHOT'")
      true
  }
}

在(Compile, packageDoc)中发布Artifact := false
在(Compile, packageSrc)中发布Artifact := false

/*
如果环境变量(DISCOVERY_ENGINE_SNAPSHOT)设置为true,则构建将执行“快照”
否则将是“发布”
* */
版本 := {
  readVersion("app-build")+
    (isSnapshot.value match {
      case true => "-SNAPSHOT"
      case false => ""
    })
}

artifactoryRepo := {
  val repoURL = "http://localhost:8081/artifactory"
  (isSnapshot.value match {
    case true => "jfrog-snapshot" at s"${repoURL}/javasree-sbt-snapshot;build.timestamp=" + new java.util.Date().getTime
    case false => "jfrog-remote" at s"${repoURL}/javasree-sbt-local"
  })
}

publishTo := Some( artifactoryRepo.value )
credentials += Credentials("Artifactory Realm", "localhost", "admin", "password")

在assembly中的assemblyMergeStrategy := {
  case PathList("META-INF", xs @ _*) => MergeStrategy.discard
  case x => MergeStrategy.last
}

libraryDependencies ++= Seq(
    "com.google.code.gson" % "gson" % "2.8.2" % "provided"
  )++Dependencies.springDependencies

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

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

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

$ java -jar DiscoveryEngine-assembly-1.0-SNAPSHOT.jar
00:44:54.232 [main] DEBUG org.springframework.web.context.support.StandardServletEnvironment - Activating profiles []

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::         (v1.0-SNAPSHOT)

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)
00:44:54.324 [main] DEBUG scala.App - Running with Spring Boot v1.0-SNAPSHOT, Spring v1.0-SNAPSHOT
00:44:54.324 [main] INFO scala.App - No active profile set, falling back to default profiles: default
...

我确信这与我的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

import BuildUtils.readVersion

name := "DiscoveryEngine"
version := "0.1"
scalaVersion := "2.13.1"
organization := "com.javasree.scala.discoveryengine"
publishMavenStyle := true

lazy val artifactoryRepo = settingKey[sbt.librarymanagement.Resolver]("Artifactory repository value")

isSnapshot := {
  sys.props.getOrElse("DISCOVERY_ENGINE_SNAPSHOT", sys.env.getOrElse("DISCOVERY_ENGINE_SNAPSHOT", "true")) match {
    case "true" | "1" => true
    case "false" | "0" => false
    case _ => println("taking the default value as true for 'SNAPSHOT'")
      true
  }
}

publishArtifact in (Compile, packageDoc) := false
publishArtifact in (Compile, packageSrc) := false

/*
if env variable(DISCOVERY_ENGINE_SNAPSHOT) is set to true then build will do "snapshot"
or else it will be "release"
* */
version := {
  readVersion("app-build")+
    (isSnapshot.value match {
      case true => "-SNAPSHOT"
      case false => ""
    })
}

artifactoryRepo := {
  val repoURL = "http://localhost:8081/artifactory"
  (isSnapshot.value match {
    case true => "jfrog-snapshot" at s"${repoURL}/javasree-sbt-snapshot;build.timestamp=" + new java.util.Date().getTime
    case false => "jfrog-remote" at s"${repoURL}/javasree-sbt-local"
  })
}

publishTo := Some( artifactoryRepo.value )
credentials += Credentials("Artifactory Realm", "localhost", "admin", "password")

assemblyMergeStrategy in assembly := {
  case PathList("META-INF", xs @ _*) => MergeStrategy.discard
  case x => MergeStrategy.last
}

  libraryDependencies ++= Seq(
    "com.google.code.gson" % "gson" % "2.8.2" % "provided"
  )++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.

$ java -jar DiscoveryEngine-assembly-1.0-SNAPSHOT.jar
00:44:54.232 [main] DEBUG org.springframework.web.context.support.StandardServletEnvironment - Activating profiles []

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::         (v1.0-SNAPSHOT)

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)
00:44:54.324 [main] DEBUG scala.App - Running with Spring Boot v1.0-SNAPSHOT, Spring v1.0-SNAPSHOT
00:44:54.324 [main] INFO scala.App - No active profile set, falling back to default profiles: default
00:44:54.325 [main] DEBUG org.springframework.boot.SpringApplication - Loading source class com.javasree.scala.discoveryengine.DiscoveryEngine
00:44:54.367 [main] DEBUG org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext - Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@1445d7f
00:44:54.378 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
00:44:54.477 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
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.
	at org.springframework.util.Assert.notEmpty(Assert.java:464)
	at org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.getCandidateConfigurations(AutoConfigurationImportSelector.java:173)
	at org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.getAutoConfigurationEntry(AutoConfigurationImportSelector.java:116)
	at org.springframework.boot.autoconfigure.AutoConfigurationImportSelector$AutoConfigurationGroup.process(AutoConfigurationImportSelector.java:396)
	at org.springframework.context.annotation.ConfigurationClassParser$DeferredImportSelectorGrouping.getImports(ConfigurationClassParser.java:875)
	at org.springframework.context.annotation.ConfigurationClassParser$DeferredImportSelectorGroupingHandler.processGroupImports(ConfigurationClassParser.java:801)
	at org.springframework.context.annotation.ConfigurationClassParser$DeferredImportSelectorHandler.process(ConfigurationClassParser.java:771)
	at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:185)
	at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:315)
	at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:232)
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:275)
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:95)
	at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:705)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:531)
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:743)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:390)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:312)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1214)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1203)
	at com.javasree.scala.discoveryengine.DiscoveryEngine$.delayedEndpoint$com$javasree$scala$discoveryengine$DiscoveryEngine$1(DiscoveryEngine.scala:13)
	at com.javasree.scala.discoveryengine.DiscoveryEngine$delayedInit$body.apply(DiscoveryEngine.scala:12)
	at scala.Function0.apply$mcV$sp(Function0.scala:39)
	at scala.Function0.apply$mcV$sp$(Function0.scala:39)
	at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:17)
	at scala.App.$anonfun$main$1(App.scala:73)
	at scala.App.$anonfun$main$1$adapted(App.scala:73)
	at scala.collection.IterableOnceOps.foreach(IterableOnce.scala:553)
	at scala.collection.IterableOnceOps.foreach$(IterableOnce.scala:551)
	at scala.collection.AbstractIterable.foreach(Iterable.scala:921)
	at scala.App.main(App.scala:73)
	at scala.App.main$(App.scala:71)
	at com.javasree.scala.discoveryengine.DiscoveryEngine$.main(DiscoveryEngine.scala:12)
	at com.javasree.scala.discoveryengine.DiscoveryEngine.main(DiscoveryEngine.scala)
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

Sbt universal:packageBin

And I executed the JAR with the following command

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

Sbt univarsal:packageBin

And I run the jar by command

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:

确定