如何在jsp页面中获取maven项目版本号?

我正在研究由maven2管理的java Web应用程序。 我们不时做了一些更改,并希望做新版本,当然还有新的版本号。 在主页(jsp)中,有文字之类的

version: 2.3.3... 

是否有可能,每次我发布新版本时,我只更改pom.xml中的 ,jsp中的版本号可以由此maven $ {project.version}自动填充?

我试过maven配置文件,但它似乎不起作用。

有任何想法吗?

谢谢。

这可能是愚蠢的,但我会使用像这个例子中的.properties文件,而不是直接过滤JSP。

您可以使用项目筛选来处理JSP,因为它被复制到目标位置。 如果使用${project.version}指定JSP,并且将包含文件夹指定为filter位置,则应在打包时将值替换为JSP。

例如,将其添加到POM可以过滤src / main / resources:

   src/main/resources true   

更新:对于war包装,您可能需要配置war插件来进行过滤。 有关更多详细信息和示例,请参阅war-plugin 文档的Filtering部分。

基本上这个过程是一样的,但它是在war插件下面定义的,所以你有这样的东西:

   org.apache.maven.plugins maven-war-plugin 2.0    src/main/resources true      

这篇文章已经创建了一段时间,但我希望它会有所帮助。 它将获得从Maven生成的属性:

 <%@ page import="java.util.*"%> <% java.io.InputStream inputStream = getServletContext().getResourceAsStream("/META-INF/maven/com.filhetallard.fam.ged/famdox/pom.properties"); Properties mavenProperties= new Properties(); mavenProperties.load(inputStream ); String version = (String) mavenProperties.get("version"); String name = (String) mavenProperties.get("artifactId"); %>  Application <%= name %> v<%= version %> 

不幸的是,有一些缺点:

  • 您必须在代码中明确地编写groupId和artifactId
  • 如果您将web-app直接从目标/部署到服务器,它将无法找到该文件,因为在打包之前,该文件位于maven-archiver目录中,而不是META-INF中。

问候。

我想做同样的事情,但我不满意任何现有的解决方案,包括使用Maven过滤方法,这是好的,但我试图摆脱在构建过程中修改现有的代码文件所以我统治虽然这是一种合理的方法,但这种方法很明显。

我将我的Maven项目版本放入我的JSP文件的方式是基于与此类似的方法,除了我没有创建一个Version.java文件,而是让Maven将版本写入属性文件,比如像这样的“version.properties”:

version.properties:

 app.version = 0.1 

并让Maven将它放在类路径上,例如,在src / main / resources中,如下所示:

   org.apache.maven.plugins maven-antrun-plugin 1.7    run  generate-sources              

此外,如果您使用的是Spring Framework 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"%>     Service Status   

Service API

The service is up and running! (v${appVersion})

这当然会呈现为:

该服务已启动并运行! (V0.1)

注意:如果您不使用JavaConfig类来配置Spring Framework,那么您可以使用Spring XML配置执行相同的操作。

我用这个插件,

http://code.google.com/p/maven-substitute-plugin/

你可以在Java中做这样的事情,

  public final static String projectVersion = "@PROJECT_VERSION@"; 

将此值传递给JSP很简单。

父pom.xml:

   dev local   

资源过滤(占位符在这里被pom-property值替换)

   src/main/resources  conf/version.properties  true   

webContext.xml中的Bean和属性占位符配置:

       

那么你的bean就像这样

 @Component public class BuildVersion { private String buildBranch; private String buildVersion; private String buildRevision; public String getBuildRevision() { return buildRevision; } public void setBuildRevision(String buildRevision) { this.buildRevision = buildRevision; } public String getBuildVersion() { return buildVersion; } public void setBuildVersion(String buildVersion) { this.buildVersion = buildVersion; } public String getBuildBranch() { return buildBranch; } public void setBuildBranch(String buildBranch) { this.buildBranch = buildBranch; } } 

这里有你的JSP片段:

 <%@ page language="java" import="java.util.*, org.springframework.context.ApplicationContext, org.springframework.web.context.support.WebApplicationContextUtils, de.smava.kredithai.cfg.BuildVersion" %> <% ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(pageContext.getServletContext()); BuildVersion buildVersion = (BuildVersion) applicationContext.getBean("buildVersion"); String branch = (String) buildVersion.getBuildBranch(); String version = (String) buildVersion.getBuildVersion(); String revision = (String) buildVersion.getBuildRevision(); if (request.getParameter("branch") != null){ out.println(branch); } else if (request.getParameter("version") != null){ out.println(version); } else if (request.getParameter("link") != null){ out.println("" + branch + " build " + version + ""); } else { out.println(branch + " build " + version + " rev. " + revision); } %> 

使用maven-replacer-plugin

像这样在pom.xml中包含插件:

   com.google.code.maven-replacer-plugin replacer (version)   prepare-package  replace     true target/someapp/jsp/helloWorld.jsp  target/someapp/jsp/helloWorld-updated.jsp  false $BUILD_NUMBER$ ${buildNumber}   

现在,在指定文件中具有令牌$BUILD_NUMBER$的任何位置,令牌都将被替换。

我会把.jsp的值交给

 String version = getClass().getPackage().getImplementationVersion(); 

例如,它看起来像1.0.0-SNAPSHOT

如果您只是获取空值 ,则可能需要将类路径添加到war的清单中

  org.apache.maven.plugins maven-war-plugin 2.5    true     

为类加载器提取它。

在http://mojo.codehaus.org/jspc/jspc-maven-plugin/usage.html中

它说明了这一点:
非WAR项目

您还可以将此插件与非war项目一起使用,例如,validationJSP。 它们将被编译,但不包含在您的最终工件中,并且不会生成或修改任何web.xml文件。

如果您只想validation和编译JSP而不实际包含war项目中生成的代码,您还可以使用includeInProject参数设置为false。

您可以在JSP文件中使用它(在我的示例中为template.jsp)

   

然后在项目的pom.xml中,您必须激活过滤:

    template.jsp  src/main/webapp/jsp/template jsp/template/ true    

并且您获取了替换了变量版本的JSP。