我想做同样的事情,但我不满意任何现有的解决方案,包括使用Maven过滤方法,这是好的,但我试图摆脱修改现有的代码文件建立过程,所以我裁定这种方法,尽管这是一个合理的方法。
将我的Maven项目版本加入到我的JSP文件中的方式基于与here类似的方法,只是我没有创建Version.java文件,而只是让Maven将版本写入属性文件,如 “version.properties” 是这样的:
version.properties:
app.version = 0.1
,并有Maven的把它的类路径,例如,在这样的src/main /资源:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<goals>
<goal>run</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<!-- Idea from link: http://stackoverflow.com/questions/2469922/generate-a-version-java-file-in-maven -->
<target>
<property name="resources.dir" value="${project.build.sourceDirectory}/../resources" />
<property name="version.filename" value="version.properties" />
<property name="buildtime" value="${maven.build.timestamp}" />
<echo message="Writing project version string to ${resources.dir}/${version.filename} ..." />
<echo file="${resources.dir}/${version.filename}" message="app.version = ${project.version}${line.separator}" />
</target>
</configuration>
</execution>
</executions>
</plugin>
此外,如果你正在使用Spring框架3.X +,那么你可以添加以下的配置,如果它存在于version.properties加载性能,否则只显示“V0.0”或什么:
@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;
}
}
最后,在你的/WEB-INF/views/home.jsp你可以有这样的:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Service Status</title>
</head>
<body>
<h1>Service API</h1>
<p>The service is up and running! (v${appVersion})</p>
</body>
</html>
当然这将呈现为:
服务是向上和银行经营ING! (v0.1)
注意:如果您不使用JavaConfig类来配置Spring Framework,那么您可以使用Spring XML配置做同样的事情。
顺便说一句,我用资源过滤和maven配置文件的其他配置文件,例如那些春天xmls,他们正在工作。但对于jsp,没有工作。 – Kent 2009-10-06 11:37:40