How can I in a jsp page get maven project version number?

后端 未结 11 2199
天命终不由人
天命终不由人 2020-12-14 16:07

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

相关标签:
11条回答
  • 2020-12-14 16:45

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

    It states this:
    Non-WAR Projects

    You can also use this plugin with non-war projects, for instance to validate JSPs. They will be compiled, but not included in your final artifact, and no web.xml file will be generated or modified.

    If you want to just validate and compile your JSPs without actually including the generated code in your war project, you can also use set the includeInProject parameter to false.

    0 讨论(0)
  • 2020-12-14 16:46

    It make a while this post have been created, but I hope it would help. It will get properties generated from 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>
    

    Unfortunately, there is some drawbacks :

    • you had to explicitely write groupId and artifactId in your code
    • if you deploy your web-app directly from target/ to your server, it won't find the file because this one is in maven-archiver directory, not in META-INF, before packaging.

    Regards.

    0 讨论(0)
  • 2020-12-14 16:46

    You can use this in your JSP file (template.jsp in my example)

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

    Then in your pom.xml of your project you have to activate filtering:

    <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>
    

    And you obtain your JSP with the variable version replaced.

    0 讨论(0)
  • 2020-12-14 16:47

    Use the maven-replacer-plugin

    Include the plugin in your pom.xml like this:

        <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>
    

    Now anywhere in the specified file that has the token$BUILD_NUMBER$ the token will get replaced.

    0 讨论(0)
  • 2020-12-14 16:51

    Parent pom.xml:

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

    Resource filtering (placeholders are replaced by pom-property values here)

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

    Bean and property placeholder config in 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>
    

    Your bean looks like this then

    @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;
        }
    
    }
    

    And here comes your JSP snippet:

    <%@ 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);
        }
    %>
    
    0 讨论(0)
  • 2020-12-14 16:55

    It's maybe stupid but I'd use a .properties file like in this example instead of filtering directly the JSP.

    0 讨论(0)
提交回复
热议问题