Maven

Get your application version with Spring Boot

Posted on by  
Ties van de Ven

There are a few ways to get your application version with Spring Boot. One approach is using Maven resource filtering to add the version number as an enviroment variable in the application.yml during the build. Another approach is to use the Implementation-Version stored in the manifest file.

First of all, you need to enable resource filtering for your application.yml

Then you need to add a maven parameter to the application.yml. @ is used here instead of the standard ${} notation to prevent conflicts

And we can then retrieve it using @Value

The upside of this approach is that you always have a version number even when you start your application from your IDE, also by putting your variable in info.app.version, if you use Spring boot actuator, your version will be automatically available under the /info endpoint. However, this approach does require quite a bit of configuration.

Continue reading →

Package-only dependencies in Maven

Posted on by  
Tom Wetjens

Sometimes you have a Maven project that needs dependencies for running tests that you do not want ending up in the final packaged WAR. We all know the test directive in the POM that accomplishes this. You might also have dependencies that are only required at runtime and need to be in the WAR but not on the compile classpath. Normally you would use the runtime directive in the POM. Consider a situation where we have a dependency that we want to be available at runtime (in the WAR), but not on the classpath during the execution of our tests. A nice example of this is logging implementations: we want to use the slf4j-simple implementation for running unit tests, but we want logback-classic to be packaged in the WAR. To accomplish this, you can use the maven-dependency-plugin as illustrated in the following POM snippet:

 org.slf4j
            slf4j-api
            1.7.7 

        
        junit
            junit
            4.11
            test 
        org.slf4j
            slf4j-simple
            1.7.7
            test 
    org.apache.maven.plugins
                maven-dependency-plugin
                package-only-deps
                        
                        prepare-package
                        copy 
                        ch.qos.logback
                                    logback-classic
                                    1.1.2 
                            ${project.build.directory}/${project.build.finalName}/WEB-INF/lib 

Continue reading →

shadow-left