package com.hzcominfo.application.etl.settings.web.controller.highconfig;

import com.hzcominfo.application.common.base.cmpt.web.controller.index.BaseController;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties; @Controller
@RequestMapping("/mavenTest/")
public class MavenTest extends BaseController { /**
* 是否是windows系统
*
* @return
*/
private static boolean isWindows() {
String osName = System.getProperty("os.name");
if (osName != null && osName.toLowerCase().indexOf("win") >= 0) {
return true;
}
return false;
} /**
* 查看jar包中maven版本号
*
* @return
* @throws IOException
* @throws XmlPullParserException
*/
@ResponseBody
@RequestMapping("test")
public String getJarPath() throws IOException, XmlPullParserException {
//查看jar包里面pom.properties版本号
String jarPath = MavenTest.class.getProtectionDomain().getCodeSource().getLocation().getFile();
jarPath = java.net.URLDecoder.decode(jarPath, "UTF-8");
try {
URL url = new URL("jar:file:" + jarPath + "!/META-INF/maven/com.hzcominfo.application.etl-settings/application-etl-settings/pom.properties");
InputStream inputStream = url.openStream();
Properties properties = new Properties();
properties.load(inputStream);
String version = properties.getProperty("version");
return version;
} catch (Exception e) {
e.printStackTrace();
//开发过程中查看pom.xml版本号
MavenXpp3Reader reader = new MavenXpp3Reader();
String basePath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
if (isWindows() && basePath.startsWith("/")) {
basePath = basePath.substring(1);
}
if (basePath.indexOf("/target/") != -1) {
basePath = basePath.substring(0, basePath.indexOf("/target/"));
}
Model model = reader.read(new FileReader(new File(basePath + "\\pom.xml")));
String version = model.getVersion();
return version;
}
}
}
package com.hzcominfo.application.common.base.cmpt.web.controller.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import com.alibaba.fastjson.JSONObject;
import com.hzcominfo.application.common.base.cmpt.web.controller.index.BaseController;
import com.hzcominfo.application.common.util.DatetimeUtil;
import com.hzcominfo.application.common.util.StringUtil; /**
* 获取项目maven版本号
*
* @Author:linzp
*/
@Controller
@RequestMapping("/mavenVersion/")
public class MavenVersion extends BaseController { @Value("${other.info:Copyright info}")
private String otherInfo; /**
* 查看jar包中maven版本号
*
* @return
*/
@ResponseBody
@RequestMapping("getVersion")
public JSONObject getJarPath(String groupId, String artifactId,String jarName, String fileName) {
JSONObject jSONObject = new JSONObject();
Map<String, Object> versionInfo = new HashMap<String, Object>();
//查看jar包里面pom.properties版本号
try {
// String jarPath = MavenVersion.class.getProtectionDomain().getCodeSource().getLocation().getFile();
String pomVersion = "";
try{
String jarPath = System.getProperty("user.dir") + File.separator + jarName;
jarPath = java.net.URLDecoder.decode(jarPath, "UTF-8");
logger.info("version info : " + jarPath);
URL url = new URL("jar:file:" + jarPath + "!/META-INF/maven/" + groupId + "/" + artifactId + "/pom.properties");
Properties properties = new Properties();
properties.load(url.openStream());
pomVersion = properties.getProperty("version");
}catch(Exception e){
logger.error("read jar or pom error:",e);
}
String version = "";
if(StringUtil.isEmpty(fileName))fileName="version.txt";
// if(StringUtil.isNotEmpty(fileName)){
File otherInfoFile = new File(System.getProperty("user.dir") + File.separator + fileName);
Long filelength = otherInfoFile.length();
byte[] filecontent = new byte[filelength.intValue()];
try {
FileInputStream in = new FileInputStream(otherInfoFile);
in.read(filecontent);
in.close();
version += new String(filecontent, "utf-8");
} catch (FileNotFoundException e) {
logger.error("version file not found :" , e);
} catch (IOException e) {
logger.error("version file read error :" , e);
}
// }
versionInfo.put("versionInfo", version);
versionInfo.put("pomVersion", pomVersion);
versionInfo.put("jvmName", System.getProperty("java.vm.name"));
versionInfo.put("jvmVendor", System.getProperty("java.vm.vendor"));
versionInfo.put("javaVersion", System.getProperty("java.version"));
versionInfo.put("otherInfo", otherInfo);
versionInfo.put("serviceTime", DatetimeUtil.getDate());
jSONObject.put("result", "0");
jSONObject.put("versionInfo", versionInfo);
} catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage());
jSONObject.put("result", "1");
}
return jSONObject;
}
public static void aaa(){
// System.out.println(MavenVersion.class.getProtectionDomain().getCodeSource().getLocation().getFile());
// System.out.println(System.getProperty("java.vm.name"));
System.out.println(DatetimeUtil.getDate());
}
public static void main(String[] args) {
// System.out.println(MavenVersion.class.getProtectionDomain().getCodeSource().getLocation().getFile());
MavenVersion.aaa();
}
}
versionDataFlag = true;

    function getVersionInfo(){
if(versionDataFlag){
versionDataFlag = false;
//获取版本号
$.ajax({
type: "post",
datatype: "json",
async: true,
url: rootPath + "/mavenVersion/getVersion",
data: {
groupId: "com.hzcominfo.application.etl-settings",
artifactId: "application-etl-settings",
jarName: "etl.jar",
fileName: "version.txt"
},
success: function (data) {
versionDataFlag = true;
var resultObject = JSON.parse(data);
if (resultObject.result == "0") {
$("#versionInfo").text(resultObject.versionInfo.versionInfo);
$("#jvmName").text(resultObject.versionInfo.jvmName);
$("#jvmVendor").text(resultObject.versionInfo.jvmVendor);
$("#javaVersion").text(resultObject.versionInfo.javaVersion);
$("#serviceTime").text(resultObject.versionInfo.serviceTime);
//$("#projectVersionA").show();
} else {
//$("#projectVersionA").remove();
$("#versionInfo").text("");
$("#jvmName").text("");
$("#jvmVendor").text("");
$("#javaVersion").text("");
$("#serviceTime").text("");
}
pageii = layer.open({
type: 1,
title: '关于',
shadeClose: true,
shade: 0.8,
area: ['400px', '250px'],
content: $("#aboutVersion").html()
});
},
error: function(data){
versionDataFlag = true;
$("#versionInfo").text("");
$("#jvmName").text("");
$("#jvmVendor").text("");
$("#javaVersion").text("");
$("#serviceTime").text("");
pageii = layer.open({
type: 1,
title: '关于',
shadeClose: true,
shade: 0.8,
area: ['400px', '250px'],
content: $("#aboutVersion").html()
});
}
});
}
}
function closeAbout(){
layer.close(pageii);
}

java代码获取项目版本号实例的更多相关文章

  1. 用JAVA代码获取Weblogic配置的JNDI 数据源连接

    第一步:生成与JDK版本对应的weblogicjar,利用cmd 进入到weblogic_home 路径下进入到server/lib目录,然后运行  JDK  1.6 命令 "java -j ...

  2. Java中获取项目根路径和类加载路径的7种方法

    引言 在web项目开发过程中,可能会经常遇到要获取项目根路径的情况,那接下来我就总结一下,java中获取项目根路径的7种方法,主要是通过thisClass和System,线程和request等方法. ...

  3. java代码获取客户端的真实ip

    java代码获取客户端的真实ip protected String getIpAddr(HttpServletRequest request) { String ip = request.getHea ...

  4. 【java】获取项目资源路径

    目资源路径分两种,一种是普通Java项目的资源路径,另一种是JavaEE项目的资源路径. 获取Java项目的包(源码下的包 或者 jar包)的资源路径 // 方法1:通过this.getClass() ...

  5. 通过Java代码获取系统信息

    在开发中,我们需要获取JVM中的信息,以及操作系统信息,内存信息,CPU信息,磁盘信息,网络信息等,通过Java的API不能获取内存等信息,需要sigar的第三方依赖包. ①:加入依赖 <dep ...

  6. Java代码获取NTP服务器时间

    apache的commons-net包下面有ntp相关的实现类,主要类有: 1  org.apache.commons.net.ntp.NTPUDPClient ? 1  org.apache.com ...

  7. Java代码获取spring 容器的bean几种方式

    一.目的 写了一个项目,多个module,然后想在A模块中实现固定的config注入,当B模块引用A时候,能够直接填写相对应的配置信息就行了.但是遇到一个问题,B引用A时候,A的配置信息总是填充不了, ...

  8. java 代码获取视频时长

    package test; import it.sauronsoftware.jave.Encoder; import it.sauronsoftware.jave.MultimediaInfo; i ...

  9. Java反射特性--获取其他类实例并调用其方法

    1. 代码结构 .├── com│   └── test│   └── MyTest.java└── MainCall.java 2. 代码内容 MyTest.java: package com.te ...

随机推荐

  1. Django框架深入了解_01(Django请求生命周期、开发模式、cbv源码分析、restful规范、跨域、drf的安装及源码初识)

    一.Django请求生命周期: 前端发出请求到后端,通过Django处理.响应返回给前端相关结果的过程 先进入实现了wsgi协议的web服务器--->进入django中间件--->路由f分 ...

  2. Node.js实现PC端类微信聊天软件(四)

    Github StackChat 学习回顾 React和Electron结合 TypeError: fs.existsSync is not a function 在React组件里引入electro ...

  3. [数据结构 - 第6章] 树之链式二叉树(C语言实现)

    一.什么是二叉树? 1.1 定义 二叉树,是度为二的树,二叉树的每一个节点最多只有二个子节点,且两个子节点有序. 1.2 二叉树的重要特性 (1)二叉树的第 i 层上节点数最多为 2n-1: (2)高 ...

  4. 怎么在eclipse中安装properties插件

    原文地址:https://jingyan.baidu.com/article/380abd0a6abe731d90192ce4.html 首先,在eclipse中点击“help”-"Inst ...

  5. 解析Java反射java.lang.IllegalArgumentException: wrong number of arguments

    项目中遇到的问题 import org.springframework.util.ReflectionUtils; import java.lang.reflect.Method; public cl ...

  6. 对javascript中call()方法的理解

    call ( thisObj [, arg1 [, arg2 [,  [, argN] ] ] ]) call()方法:官方介绍是,调用一个对象的一个方法,以另一个对象替换当前对象. call()方法 ...

  7. log4j重复打印的解决方法

    log4j的logger(旧版本称logger)是层次结构的,子logger会继承父logger的属性,appender也是可继承的属性,这常常 容易导致配置错误而引起的log4j输出重复的log信息 ...

  8. day23——继承

    day23 初识继承 字面意思:儿子可以完全使用父亲的所有内容 专业角度:如果B类继承A类, B类就称为子类.派生类 A类就称为父类.基类.超类 面向对象三大特性:继承.封装.多态 继承:单继承.多继 ...

  9. Go基础编程实践(四)—— 数组和map

    数组去重 package main import "fmt" func main(){ intSlice := []int{1,5,5,5,5,7,8,6,6, 6} fmt.Pr ...

  10. 国际化地区语言码对照表(i18n)

    af 公用荷兰语 af-ZA 公用荷兰语 - 南非 sq 阿尔巴尼亚 sq-AL 阿尔巴尼亚 -阿尔巴尼亚 ar 阿拉伯语 ar-DZ 阿拉伯语 -阿尔及利亚 ar-BH 阿拉伯语 -巴林 ar-EG ...