2009-10-06 107 views
25

我正在研究一个由maven2管理的java web应用程序。我们不时地做了一些改变,并且想要新版本,当然还有新的版本号。在主页(JSP),有喜欢如何在jsp页面中获取maven项目的版本号?

<b>version:</b> 2.3.3... 

文本是否有可能,我每次做一个新的版本的时候,我只改变pom.xml中<version/>,并在JSP版本号可以通过自动填写这个maven $ {project.version}?

我试过maven配置文件,但它似乎没有工作。

有什么想法?

谢谢。

+0

顺便说一句,我用资源过滤和maven配置文件的其他配置文件,例如那些春天xmls,他们正在工作。但对于jsp,没有工作。 – Kent 2009-10-06 11:37:40

回答

12

这也许愚蠢的,但我会使用一个.properties文件就像this example而不是直接过滤JSP。

+1

那么,最后我做到了这一点。我在现有的属性文件中添加了一个条目,并让mvn填充该版本。并添加了一个自动启动servlet。当应用程序上下文启动时,servlet读取prop文件并设置一个系统属性,例如“myVersion”,以便jsp可以按名称检索。 使用maven资源插件copy-resources的目标也是如此。不过我也觉得过滤jsp资源不那么干净。我们使用apache-tiles进行布局,也就是说,在某些jsps中,tile已经有$ {...}。如果添加了一些$ {},可能会引起混淆。 谢谢你们的评论。 肯特 – Kent 2009-10-06 13:25:34

28

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

例如,添加以下内容到POM能使滤波的src/main /资源:

<resources> 
    <resource> 
    <directory>src/main/resources</directory> 
    <filtering>true</filtering> 
    </resource> 
</resources> 

更新:战争的包装,你可能需要配置的战争插件来完成其过滤。有关更多详细信息和示例,请参阅war-plugin的documentationFiltering部分。

本质的过程是一样的,但下面的战争插件定义,所以你有这样的事情:

<plugins> 
    <plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-war-plugin</artifactId> 
    <version>2.0</version> 
    <configuration> 
     <webResources> 
     <resource> 
      <directory>src/main/resources</directory> 
      <filtering>true</filtering> 
     </resource> 
     </webResources> 
    </configuration> 
    </plugin> 
</plugins> 
+0

很高兴帮助。这适用于筛选时设置的任何属性 – 2009-10-06 11:27:52

+0

抱歉,刚才我正在检查错误的文件,并认为它正在工作。 我加入: \t \t \t \t 的src /主/ web应用 \t \t \t \t \t \t \t 和在src /主/资源,有一个test.jsp的,它与$ {} project.version。 运行mvn clean package后,版本号未分配给$ {...}。 我做错了什么? – Kent 2009-10-06 11:34:09

+0

在您的评论中,您正在过滤src/main/webapp,但JSP在src/main/resources中,您确定配置匹配吗? – 2009-10-06 12:09:21

2

这篇文章已经创建了一段时间,但我希望这会有所帮助。它会得到的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"); 

%><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr"> 
<head> 
    <title>Application <%= name %> v<%= version %></title> 

不幸的是,有一些缺点:

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

问候。

0

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

它指出这一点:
非战争项目

您也可以使用这个插件与非战的项目,例如验证的JSP。它们将被编译,但不包含在最终的工件中,并且不会生成或修改web.xml文件。

如果您只想验证并编译您的JSP而不实际在您的war项目中包含生成的代码,那么也可以使用将includeInProject参数设置为false。

2

我想做同样的事情,但我不满意任何现有的解决方案,包括使用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配置做同样的事情。

0

你可以在你的JSP文件(在我的例子template.jsp)

<head> 
<meta name="Historia Social Unica version:${version}" /> 

然后在你的项目中的pom.xml使这个你必须激活过滤:

<resources> <resource> <includes> <include>template.jsp</include> </includes> <directory>src/main/webapp/jsp/template</directory> <targetPath>jsp/template/</targetPath> <filtering>true</filtering> </resource> </resources> </build>

并且您可以在替换变量版本的情况下获得您的JSP。

1

家长pom。XML:

<properties> 
     <!-- in my case injected by jenkins build job --> 
     <build.version>dev</build.version> 
     <build.branch>local</build.branch> 
     <build.revision /> 
</properties> 

资源过滤

<resources> 
    <resource> 
     <directory>src/main/resources</directory> 
     <includes> 
      <include>conf/version.properties</include> 
     </includes> 
     <filtering>true</filtering> 
    </resource> 
</resources> 

豆和财产配置的占位符(占位符由这里POM属性值替换)在webContext.xml:

<context:property-placeholder location="classpath:conf/version.properties"/> 

    <bean id="buildVersion" class="de.your.package.cfg.BuildVersion"> 
     <property name="buildBranch" value="${build_branch}"/> 
     <property name="buildVersion" value="${build_version}"/> 
     <property name="buildRevision" value="${build_revision}"/> 
    </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("<a href=\"http://your_server_url"+branch+"/"+version+"\" >" + branch + " build " + version + "</a>"); 
    } else { 
     out.println(branch + " build " + version + " rev. " + revision); 
    } 
%> 
1

使用maven-replacer-plugin

包括像这样在你的pom.xml的插件:

<plugin> 
     <groupId>com.google.code.maven-replacer-plugin</groupId> 
     <artifactId>replacer</artifactId> 
     <version>(version)</version> 
     <executions> 
      <execution> 
       <phase>prepare-package</phase> 
       <goals> 
        <goal>replace</goal> 
       </goals>      
      </execution> 
     </executions> 
     <configuration> 
      <ignoreMissingFile>true</ignoreMissingFile> 
      <file>target/someapp/jsp/helloWorld.jsp</file> 
      <outputFile> 
       target/someapp/jsp/helloWorld-updated.jsp 
      </outputFile> 
      <regex>false</regex> 
      <token>$BUILD_NUMBER$</token> 
      <value>${buildNumber}</value> 
     </configuration> 
    </plugin> 

现在任何地方指定的文件在令牌$BUILD_NUMBER$的令牌将被替换。

1

我会交给这个.jsp的

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

的值看起来像1.0.0-SNAPSHOT的实例。

如果您是刚开始空,则可能需要在类路径

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-war-plugin</artifactId> 
    <version>2.5</version> 
    <configuration> 
     <archive> 
      <manifest> 
       <addClasspath>true</addClasspath> 
      </manifest> 
     </archive> 
    </configuration> 
</plugin> 

添加到war的舱单的类加载器把它捡起来。

相关问题