项目环境: 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; } }