-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMain.java
executable file
·135 lines (122 loc) · 4.82 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package launcher;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import launcher.settings.ISettingsService;
import launcher.settings.SettingsItem;
import launcher.settings.exceptions.ESettingsException;
import launcher.settings.properties.PropertiesLoader;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectHelper;
/**
* this is project for automatization of console work ( ant compile, run ... )
*/
public class Main {
private static final String antPath="/usr/app/ant/bin/ant";
private static final String javaHome="/usr/app/java";
public static void main(String[] args) throws ESettingsException {
System.out.println("0 - path to property file");
System.out.println("1..n - execute command (or path to build.xml )");
System.out.println("begin");
// args=new String[]{"/home/vcherkashin/workspaces/launcher/properties/launcher.properties","tcopy"};
// args=new String[]{"/home/vcherkashin/workspaces/launcher/launcher.properties","/usr/app/ant/bin/ant"};
ISettingsService service=new PropertiesLoader(args[0]);
List<SettingsItem> list=service.getAllValues();
for(int counter=1;counter<args.length;counter++){
try{
System.out.println("Execute:"+args[counter]);
SettingsItem item=SettingsItem.getSettingsItemByName(list, args[counter]);
if(item!=null){
int returnValue=runCommand(detectAntScript(item.getValue()));
if(returnValue>0){
System.out.println("Command execute with Error: "+item);
break;
}
}else{
System.out.println("command does not found in property file: "+args[counter]);
runCommand(detectAntScript(args[counter]));
}
}catch(ESettingsException ex){
System.out.println(ex.getMessage());
break;
}
}
System.out.println("-end-");
}
/** return string command */
private static String[] detectAntScript(String command) throws ESettingsException{
if(command!=null){
if(command.endsWith("build.xml")){
try{
runAntScript(command);return null;
// return new String[]{antPath,"-buildfile",command};
}catch(Exception ex){
// System.out.println("Run Exception:"+ex.getMessage());
throw new ESettingsException("Run Exception:"+ex.getMessage());
}
}else{
return detectMultiCommand(command);
}
}else{
return new String[]{};
}
}
private static void runAntScript(String fileBuild) throws Exception{
File buildFile = new File(fileBuild);
System.out.println(" >>>> "+buildFile.getAbsolutePath().trim());
Project p = new Project();
p.setUserProperty("ant.file", buildFile.getAbsolutePath());
p.setUserProperty("build.compiler", "extJavac");
p.setProperty("java.home", javaHome);
p.setUserProperty("java.home", javaHome);
p.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
p.addReference("ant.projectHelper", helper);
helper.parse(p, buildFile);
p.executeTarget(p.getDefaultTarget());
}
/** run command */
private static int runCommand(String[] command){
if(command==null)return 0;
try {
printArray(" >>>> ", command);
Process p = Runtime.getRuntime().exec(command);
p.waitFor();
if(p.exitValue()>0){
printInputStream(p.getErrorStream());
}else{
printInputStream(p.getInputStream());
}
return p.exitValue();
}
catch (Exception err) {
printArray(null, command);
err.printStackTrace();
return 2;
}
}
private static void printInputStream(InputStream is) throws IOException{
System.out.print(" ... ");
while(is.available()>0){
System.out.print( (char)is.read());
}
System.out.println();
}
private static void printArray(String preambula, String[] values){
StringBuffer buffer=new StringBuffer();
if(preambula!=null)buffer.append(preambula);
for(int counter=0;counter<values.length;counter++){
buffer.append(values[counter]);
buffer.append(" ");
}
System.out.println(buffer.toString());
}
private static String[] detectMultiCommand(String command) {
return command.trim().split(" ");
}
}