I am working on a java web application, managed by maven2. From time to time, we did some changes, and want to do new releases, of course with new version number. In the hom
I wanted to do this very same thing but I was not satisfied with any of the existing solutions, including using the Maven filtering approach, which is ok, but I am trying to move away from modifying existing code files during the build process so I ruled that approach out, although it is a reasonable approach.
The way I get my Maven project version into my JSP file is based on a similar approach to the one from here except that I don't create a Version.java file, instead I just have Maven write the version out to a properties file, such as "version.properties" like this:
version.properties:
app.version = 0.1
and have Maven put it on the classpath, for instance, in src/main/resources like this:
org.apache.maven.plugins
maven-antrun-plugin
1.7
run
generate-sources
Also, if you are using Spring Framework 3.x+ then you can add the following configuration to load properties in version.properties if it exists, otherwise just show "v0.0" or whatever:
@Configuration
@EnableWebMvc
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class WebHomeConfig extends WebMvcConfigurerAdapter implements
ApplicationContextAware {
private ApplicationContext _appContext;
/*
* (non-Javadoc)
*
* @see
* org.springframework.context.ApplicationContextAware#setApplicationContext
* (org.springframework.context.ApplicationContext)
*/
@Override
public void setApplicationContext(ApplicationContext appContext)
throws BeansException {
_appContext = appContext;
}
@Bean
public ViewResolver getViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.getAttributesMap().put("appVersion", appVersion);
return resolver;
}
/**
* Since we don't have any controller logic, simpler to just define
* controller for page using View Controller. Note: had to extend
* WebMvcConfigurerAdapter to get this functionality
*
* @param registry
*/
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("home");
}
/**
* The application version.
*/
@Value("${app.version:0.0}")
protected String appVersion;
@Bean
public static PropertySourcesPlaceholderConfigurer configurer() {
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
configurer.setIgnoreResourceNotFound(true);
configurer.setLocations(new Resource[] {
new ClassPathResource("version.properties")});
return configurer;
}
}
And finally, in your /WEB-INF/views/home.jsp you can have something like:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
Service Status
Service API
The service is up and running! (v${appVersion})
And this would of course render as:
The service is up and running! (v0.1)
NOTE: If you don't use the JavaConfig classes to configure Spring Framework then you can do the same thing with Spring XML configuration.