• API查看Web App发布版本+编译时间+环境变量


    项目环境: JDK7+Maven3.04

    项目架构:SpringMVC

    • 方法一:API访问Maven生成的MANIFEST.MF

    1. 在pom.xml中添加jar包支持

    <dependency>
      <groupId>com.jcabi</groupId>
      <artifactId>jcabi-manifests</artifactId>
      <version>1.1</version>
    </dependency>

    2. pom.xml配置

    配置archive添加

    built-at: 编译时间

    env: 环境

    project-version: 代码分支号

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-war-plugin</artifactId>
      <version>2.4</version>
      <executions>
        <execution>
          <id>artifact-package</id>
          <phase>package</phase>
          <goals>
            <goal>war</goal>
          </goals>
          <configuration>
            <archiveClasses>true</archiveClasses>
            <warSourceExcludes>WEB-INF/lib/servlet-*</warSourceExcludes>
            <archive>
              <manifestEntries>
                <built-at>${maven.build.timestamp}</built-at>
                <env>${env}</env>
                <project-version>${project.version}</project-version>
              </manifestEntries>
            </archive>
          </configuration>
        </execution>
        <execution>
          <id>local-package</id>
          <phase>package</phase>
          <goals>
            <goal>exploded</goal>
          </goals>
          <configuration>
            <primaryArtifact>false</primaryArtifact>
            <webappDirectory>${project.build.directory}/hiv-local</webappDirectory>
            <archiveClasses>true</archiveClasses>
            <warSourceExcludes>WEB-INF/lib/servlet-*</warSourceExcludes>
            <archive>
              <manifestEntries>
                <built-at>${maven.build.timestamp}</built-at>
                <env>${env}</env>
                <project-version>${project.version}</project-version>
              </manifestEntries>
            </archive>
          </configuration>
        </execution>
      </executions>
    </plugin>

    3. 编译后结果如下

    META-INF/MANIFEST.MF

    Manifest-Version: 1.0
    Built-By: dell
    Build-Jdk: 1.7.0_75
    project-version: develop-SNAPSHOT
    Created-By: Apache Maven 3.0.4
    built-at: 2015-07-02 13:35:38
    env: qa
    Archiver-Version: Plexus Archiver

     4. 配置API访问编译结果并输出

    @Controller
    public class VersionController {
      @Resource
      private ServletContext context;
    
      @ResponseBody
      @RequestMapping(value = "/version", method = RequestMethod.GET)
      @ApiOperation(value = "查看编译版本", response = Map.class, httpMethod = "GET", notes = "查看编译版本")
      public Map<String, String> version(HttpServletRequest request) throws IOException {
        Map<String, String> map = new HashMap<String, String>();
        MfMap mfMap = Manifests.DEFAULT.append(new ServletMfs(context));
        map.put("project-version", mfMap.get("project-version"));
        map.put("env", mfMap.get("env"));
        map.put("built-at", mfMap.get("built-at"));
        return map;
      }
    }

    5. 打成war包启动结果如下:

    {"project-version":"develop-SNAPSHOT","built-at":"2015-07-02 13:35:38","env":"qa"}

     参考资料: http://manifests.jcabi.com/

    • 方法二:使用maven生成变量

    1. 在pom.xml文件中配置

    <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <maven.build.timestamp.format>yyyy-MM-dd HH:mm:ss</maven.build.timestamp.format>
      <timestamp>${maven.build.timestamp}</timestamp>
    </properties>

    2. 在properties文件中配置

    project.version=${project.version}
    project.env=${env}
    project.built.time=${timestamp}

    3. 配置API访问编译结果并输出

    @Controller
    public class VersionController {
      @Value("${project.version}")
      private String version;
      @Value("${project.env}")
      private String env;
      @Value("${project.built.time}")
      private String builtAt;
    
      @ResponseBody
      @RequestMapping(value = "version", method = GET)
      @ApiOperation(value = "查看版本信息")
      public Map<String, String> version() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("project-version", version);
        map.put("env", env);
        map.put("built-at", builtAt);
        return map;
      }
    
    }

    参考资料: http://stackoverflow.com/questions/13228472/how-to-acces-maven-build-timestamp-for-resource-filtering

  • 相关阅读:
    《NoSQL精粹》读书笔记
    react+flux编程实践(一) 基础篇
    MongoDB索引(一) --- 入门篇:学习使用MongoDB数据库索引
    (译+注解)node.js的C++扩展入门
    深入解析Javascript异步编程
    (译)package.json详解
    Protobuf学习
    Redis学习
    MySQL学习-常用命令整理
    TCP/IP-TCP
  • 原文地址:https://www.cnblogs.com/hiver/p/4616087.html
Copyright © 2020-2023  润新知