Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在jsp页面中获取maven项目版本号?_Java_Maven 2 - Fatal编程技术网

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

Java 如何在jsp页面中获取maven项目版本号?,java,maven-2,Java,Maven 2,我正在开发一个java web应用程序,由maven2管理。我们不时地做一些更改,并希望发布新的版本,当然要有新的版本号。在主页(jsp)中,有如下文本 <b>version:</b> 2.3.3... 版本:2.3.3。。。 是否可能,每次我发布一个新版本时,我只更改pom.xml中的,jsp中的版本号可以由这个maven${project.version}自动填充 我尝试了maven profile,但它似乎不起作用 有什么想法吗 谢谢。当JSP被复制到目标位置

我正在开发一个java web应用程序,由maven2管理。我们不时地做一些更改,并希望发布新的版本,当然要有新的版本号。在主页(jsp)中,有如下文本

<b>version:</b> 2.3.3... 
版本:2.3.3。。。
是否可能,每次我发布一个新版本时,我只更改pom.xml中的
,jsp中的版本号可以由这个maven${project.version}自动填充

我尝试了maven profile,但它似乎不起作用

有什么想法吗

谢谢。

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

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

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

这可能很愚蠢,但我会使用类似于的
.properties
文件,而不是直接过滤JSP。

我使用这个插件

您可以在Java中执行类似的操作

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

将这个值传递给JSP是很简单的。

这篇文章已经创建了一段时间,但我希望它能有所帮助。它将获得从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>

应用程序v
不幸的是,存在一些缺点:

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

它表明:
非战争项目

您还可以将此插件用于非war项目,例如验证JSP。它们将被编译,但不包括在最终工件中,并且不会生成或修改web.xml文件


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

我也想做同样的事情,但我对现有的任何解决方案都不满意,包括使用Maven过滤方法,这是可以的,但是我试图在构建过程中避免修改现有的代码文件,所以我排除了这种方法,尽管这是一种合理的方法

我将Maven项目版本放入JSP文件的方法与从中获取的方法类似,只是我没有创建version.java文件,而是让Maven将版本写入属性文件,如“version.properties”,如下所示:

<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>
    <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>
version.properties:

app.version = 0.1
让Maven将其放在类路径上,例如,在src/main/resources中,如下所示:

<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>
    <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>
最后,在/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>

服务状态
服务API
服务已启动并正在运行!(v${appVersion})

这当然会表现为:

服务已启动并正在运行!(v0.1)


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

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


然后,在项目的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>

template.jsp
src/main/webapp/jsp/template
jsp/模板/
真的
您可以获得替换了变量版本的JSP。

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>
下面是您的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);
    }
%>

使用
maven replacer插件

将插件包括在pom.xml中,如下所示:


com.google.code.maven-replacer-plugin
替代者
(版本)
准备包装
代替
真的
target/someapp/jsp/helloWorld.jsp
target/someapp/jsp/helloWorld-updated.jsp
假的
$BUILD_编号$
${buildNumber}

现在,在指定文件中具有标记的任何位置,
$BUILD\u NUMBER$
标记都将被替换。

我会将

String version = getClass().getPackage().getImplementationVersion();
例如,这看起来像
1.0.0-SNAPSHOT

如果您刚刚得到空值,则可能需要使用将类路径添加到
war
的清单中

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

org.apache.maven.plugins
maven战争插件
2.5
真的

有很多方法可以传递值(如这些注释中所讨论的)。另一种方法(有其自身的优点和缺点)是将参数添加到POM文件的清单中:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <archive>
                    <manifestEntries>
                        <Build-Version>${project.version}</Build-Version>
                        <Build-Date>${buildDateTime}</Build-Date>
                        <Build-Number>${buildNumber}</Build-Number>
                        <Build-Revision>${buildRevision}</Build-Revision>
                    </manifestEntries>
                </archive>
            </configuration>
        </plugin>

org.apache.maven.plugins
maven战争插件
2.6
${project.version}
${buildDateTime}
${buildNumber}
${buildRevision}
然后打开并阅读清单,在配置期间设置一个单例bean,或者使用以下命令直接将它们导入JSP:

<%
    String buildVersion;
    String buildDate;
    String buildRevision;
    String buildNumber;
    Attributes attributes;
    String version = "";
    InputStream in = null;

    // Get manifest attributes
    try {
        Manifest manifest;

        in = pageContext.getServletContext().getResourceAsStream("/META-INF/MANIFEST.MF");
        manifest = new Manifest(in);
        attributes = manifest.getMainAttributes();
    } catch (Exception ex) {
        attributes = new Attributes();
        attributes.put(new Attributes.Name("Build-Version"), "None (Inplace Deployment)");
    } finally {
        if (in != null) {
            in.close();
        }
    }
    buildVersion = attributes.getValue("Build-Version");
    buildDate = attributes.getValue("Build-Date");
    buildRevision = attributes.getValue("Build-Revision");
    buildNumber = attributes.getValue("Build-Number");
%>


一个优点是,该信息也以易于查找的文档形式出现在清单中。一个缺点是需要打开并读取清单文件。

很乐意提供帮助。这适用于在filter timesorry设置的任何属性,刚才我检查了错误的文件,并认为它可以工作。我添加了:src/main/webapp true和unde
<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>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <archive>
                    <manifestEntries>
                        <Build-Version>${project.version}</Build-Version>
                        <Build-Date>${buildDateTime}</Build-Date>
                        <Build-Number>${buildNumber}</Build-Number>
                        <Build-Revision>${buildRevision}</Build-Revision>
                    </manifestEntries>
                </archive>
            </configuration>
        </plugin>
<%
    String buildVersion;
    String buildDate;
    String buildRevision;
    String buildNumber;
    Attributes attributes;
    String version = "";
    InputStream in = null;

    // Get manifest attributes
    try {
        Manifest manifest;

        in = pageContext.getServletContext().getResourceAsStream("/META-INF/MANIFEST.MF");
        manifest = new Manifest(in);
        attributes = manifest.getMainAttributes();
    } catch (Exception ex) {
        attributes = new Attributes();
        attributes.put(new Attributes.Name("Build-Version"), "None (Inplace Deployment)");
    } finally {
        if (in != null) {
            in.close();
        }
    }
    buildVersion = attributes.getValue("Build-Version");
    buildDate = attributes.getValue("Build-Date");
    buildRevision = attributes.getValue("Build-Revision");
    buildNumber = attributes.getValue("Build-Number");
%>