Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autowiring BuildProperties bean from Gradle - NoSuchBeanDefinitionException

I am trying to obtain the version in my Java application from the Gradle build file. I am following the instructions here;

https://docs.spring.io/spring-boot/docs/current/reference/html/howto-build.html

build.gradle

buildscript {     repositories {         mavenCentral()     }     dependencies {         classpath("org.springframework.boot:spring-boot-gradle-         plugin:1.5.7.RELEASE")     } }  project.version = '0.1.0'  apply plugin: 'java' apply plugin: 'war' apply plugin: 'idea' apply plugin: 'org.springframework.boot'  springBoot  {     buildInfo() }  jar {     baseName = 'ci-backend' }  war {    baseName = 'ci-backend' }  repositories {     mavenCentral() }  sourceCompatibility = 1.8 targetCompatibility = 1.8  dependencies {     compile("org.springframework.boot:spring-boot-starter-web")     compile("org.springframework:spring-jdbc")     compile("joda-time:joda-time")      compile("com.opencsv:opencsv:3.9")     compile("org.springframework.batch:spring-batch-core")      testCompile('org.springframework.boot:spring-boot-starter-test')     providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')  } 

After building with gradle the build-info.properties file is present in build/resources/main/META-INF/build-info.properties

In my @RestController I am trying to autowire the build properties bean

@Autowired private BuildProperties buildProperties; 

I am getting the following error;

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.info.BuildProperties' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585) ... 41 more 

I would assume that the BuildProperties bean is automatically created when the build-info.properties is present. It does not seem to be the case.

like image 572
Jags Avatar asked Sep 27 '17 04:09

Jags


2 Answers

The problem I had may have been different, but I landed here trying to google the solution, so I'll post this here in case anybody else runs into the same problem. My error message was:

Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.info.BuildProperties' available

It was only when trying to run within IntelliJ, not when run with gradle from the command line. (AND this is possibly specific to SpringBoot)

I just needed to set "Delegate IDE build/run actions to gradle" from within "Build, Execution, Deployment->Build Tools->Gradle" which then got the "bootBuildInfo" task to run when building from the IDE.

like image 137
Brian Deacon Avatar answered Sep 28 '22 05:09

Brian Deacon


For a Maven project, in IntelliJ "Preferences...", under Build, Execution, Deployment > Build Tools > Maven > Runner, select the option "Delegate IDE build/run actions to Maven."

like image 39
izilotti Avatar answered Sep 28 '22 05:09

izilotti