码迷,mamicode.com
首页 > Windows程序 > 详细

API访问Maven生成的MANIFEST.MF

时间:2015-07-02 15:23:11      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:

项目环境: JDK7+Maven3.04

项目架构:SpringMVC

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>
        </archive>
        <webResources>
          <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
          </resource>
          <resource>
            <directory>src/main/resources/${env}</directory>
            <filtering>true</filtering>
          </resource>
        </webResources>
        <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}/education-local</webappDirectory>
        <archiveClasses>true</archiveClasses>
        <warSourceExcludes>WEB-INF/lib/servlet-*</warSourceExcludes>
        <archive>
        </archive>
        <webResources>
          <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
          </resource>
          <resource>
            <directory>src/main/resources/${env}</directory>
            <filtering>true</filtering>
          </resource>
        </webResources>
        <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/

API访问Maven生成的MANIFEST.MF

标签:

原文地址:http://www.cnblogs.com/hiver/p/4616087.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!