2009-10-06 82 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的艙單的類加載器把它撿起來。

相關問題