Reputation: 63
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>build-info</id>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
this the main class
public class GetSpaOsmiumVersionClient implements CommandLineRunner{
@Autowired
BuildProperties buildProperties;
public static void main( String[] args ){
SpringApplication app = new SpringApplication(GetSpaOsmiumVersionClient.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args);
}
@Override
public void run(String... args) throws Exception {
Options options = new Options();
options.addOption("h", "help", false, "prints the help content");
options.addOption("v", "version", false, "version spa osmium");
try{
//Etape 2: Analyse de la ligne de commande
CommandLineParser parser = new DefaultParser();
CommandLine commandLine = parser.parse(options, args);
if(commandLine.hasOption("v")){
buildProperties.getVersion();
}else {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp( "App" , options );
System.exit(1);
}
Upvotes: 1
Views: 2286
Reputation: 10848
Found this question in Google Search for a similar issue, so I'll leave this answer so other fellow developers can save some time.
The error message in the question title
Unsatisfied dependency expressed through field 'buildProperties'
is often caused by a misunderstanding on how the property BuildProperties
works.
Basically, BuildProperties
only works if we have executed the 'build-info' goal of Maven Spring Boot Plugin (aka, run the following command in cmd
):
mvn spring-boot:build-info
The reason is that BuildProperties is NOT something built-in, but a product of that Maven goal. When that goal is executed, a file build-info.properties is generated - and the code will read from that file.
Usually Maven project is set up so that it will automatically execute that goal on pipeline (in plugins
part, see picture below). However when we trigger a run on local IDE, that goal isn't automatically executed, hence the problem.
Detailed explanation on how it works can be found in this reference: https://www.vojtechruzicka.com/spring-boot-version/
If you prefer to work with IDE (eg: IntelliJ) instead of command line, you might find Maven tool window. Your job is to "run" the "build-info" goal before starting the server.
Example for IntelliJ: https://www.jetbrains.com/help/idea/work-with-maven-goals.html
Upvotes: 4