简体   繁体   中英

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

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.

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM