-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SSM detailed version support (#1847)
- Loading branch information
1 parent
94174e4
commit 48ccb0c
Showing
5 changed files
with
268 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
smart-common/src/main/java/org/smartdata/versioninfo/VersionInfoRead.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.smartdata.versioninfo; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Properties; | ||
|
||
public class VersionInfoRead { | ||
|
||
Properties prop = new Properties(); | ||
|
||
protected VersionInfoRead(String component) { | ||
InputStream in = null; | ||
String s = component + "-versionInfo.properties"; | ||
try { | ||
in = Thread.currentThread().getContextClassLoader().getResourceAsStream(s); | ||
prop.load(in); | ||
} catch (Exception e) { | ||
System.out.println(e); | ||
} finally { | ||
try { | ||
in.close(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
|
||
private static VersionInfoRead info = new VersionInfoRead("common"); | ||
|
||
protected String getVersion() { | ||
return info.prop.getProperty("version", "Unknown"); | ||
} | ||
|
||
protected String getRevision() { | ||
return info.prop.getProperty("revision", "Unknown"); | ||
} | ||
|
||
protected String getTime() { | ||
return info.prop.getProperty("date", "Unknown"); | ||
} | ||
|
||
protected String getUser() { | ||
return info.prop.getProperty("user", "Unknown"); | ||
} | ||
|
||
protected String getUrl() { | ||
return info.prop.getProperty("url", "Unknown"); | ||
} | ||
|
||
protected String getBranch() { | ||
return info.prop.getProperty("branch", "Unknown"); | ||
} | ||
|
||
public void printInfo() { | ||
System.out.println("SSM " + getVersion()); | ||
System.out.println("Subversion " + getUrl()); | ||
System.out.println("Last commit " + getRevision() + " on branch " + getBranch()); | ||
System.out.println("Compiled by " + getUser() + " on " + getTime()); | ||
} | ||
|
||
public static void main(String[] args) { | ||
VersionInfoRead t = new VersionInfoRead("common"); | ||
t.printInfo(); | ||
} | ||
} |
157 changes: 157 additions & 0 deletions
157
smart-common/src/main/java/org/smartdata/versioninfo/VersionInfoWrite.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.smartdata.versioninfo; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.io.OutputStream; | ||
import java.text.DateFormat; | ||
import java.text.SimpleDateFormat; | ||
import java.util.ArrayList; | ||
import java.util.Date; | ||
import java.util.List; | ||
import java.util.Properties; | ||
import java.util.TimeZone; | ||
|
||
public class VersionInfoWrite { | ||
File directory = new File(""); | ||
String pom = directory.getAbsolutePath() + "/" + "pom.xml"; | ||
|
||
public void execute() { | ||
Properties prop = new Properties(); | ||
OutputStream output = null; | ||
String s = this.getClass().getResource("/").getPath() + "common-versionInfo.properties"; | ||
try { | ||
output = new FileOutputStream(s); | ||
prop.setProperty("version", getVersionInfo(pom)); | ||
prop.setProperty("revision", getCommit()); | ||
prop.setProperty("user", getUser()); | ||
prop.setProperty("date", getBuildTime()); | ||
prop.setProperty("url", getUri()); | ||
prop.setProperty("branch", getBranch()); | ||
prop.store(output, new Date().toString()); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} finally { | ||
if (output != null) { | ||
try { | ||
output.close(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private String getBuildTime() { | ||
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'"); | ||
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); | ||
return dateFormat.format(new Date()); | ||
} | ||
|
||
private String getVersionInfo(String pom) throws IOException { | ||
File file = new File(pom); | ||
FileReader fileReader = new FileReader(file); | ||
String st; | ||
BufferedReader br = new BufferedReader(fileReader); | ||
while ((st = br.readLine()) != null) { | ||
if (st.contains("<version>")) { | ||
return st.trim().substring("<version>".length(), | ||
st.trim().length() - "</version>".length()); | ||
} | ||
} | ||
return "Not found"; | ||
} | ||
|
||
private String getUri() { | ||
List<String> list = execCmd("git remote -v"); | ||
String uri = "Unknown"; | ||
for (String s : list) { | ||
if (s.startsWith("origin") && s.endsWith("(fetch)")) { | ||
uri = s.substring("origin".length()); | ||
uri = uri.substring(0, uri.length() - "(fetch)".length()); | ||
break; | ||
} | ||
} | ||
return uri.trim(); | ||
} | ||
|
||
private String getCommit() { | ||
List<String> list = execCmd("git log -n 1"); | ||
String commit = "Unknown"; | ||
for (String s : list) { | ||
if (s.startsWith("commit")) { | ||
commit = s.substring("commit".length()); | ||
break; | ||
} | ||
} | ||
return commit.trim(); | ||
} | ||
|
||
private String getUser() { | ||
List<String> list = execCmd("whoami"); | ||
String user = "Unknown"; | ||
for (String s : list) { | ||
user = s.trim(); | ||
break; | ||
} | ||
return user; | ||
} | ||
|
||
private String getBranch() { | ||
List<String> list = execCmd("git branch"); | ||
String branch = "Unknown"; | ||
for (String s : list) { | ||
if (s.startsWith("*")) { | ||
branch = s.substring("*".length()).trim(); | ||
break; | ||
} | ||
} | ||
return branch; | ||
} | ||
|
||
public List<String> execCmd(String cmd) { | ||
String command = "/bin/sh -c " + cmd; | ||
List<String> list = new ArrayList<String>(); | ||
try { | ||
Runtime rt = Runtime.getRuntime(); | ||
Process proc = rt.exec(cmd, null, null); | ||
InputStream stderr = proc.getInputStream(); | ||
InputStreamReader isr = new InputStreamReader(stderr, "UTF-8"); | ||
BufferedReader br = new BufferedReader(isr); | ||
String line; | ||
while ((line = br.readLine()) != null) { | ||
list.add(line); | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return list; | ||
} | ||
|
||
public static void main(String[] args) { | ||
VersionInfoWrite w = new VersionInfoWrite(); | ||
w.execute(); | ||
} | ||
} |