在使用java -jar命令执行包时,使用Options封装一些参数,
如下方法中使用:
public static void run(String[] args) {
if (args.length > 0) {
Properties properties = new Properties();
Options options = new Options();
options.addOption("M", true, "main app classname");
options.addOption("P", true, "properties filename");
OptionBuilder.withArgName("property=value");
OptionBuilder.hasArgs(2);
OptionBuilder.withValueSeparator();
OptionBuilder.withDescription("use value for given property");
options.addOption(OptionBuilder.create("D"));
CommandLineParser parser = new PosixParser();
CommandLine cmd = null;
try{
cmd = parser.parse(options, args);
}catch (ParseException e){
e.printStackTrace();
}
if (cmd.hasOption('M')) {
String fullClassName = cmd.getOptionValue('M');
if (fullClassName != null && !fullClassName.isEmpty()) {
if (cmd.hasOption('P')) {
File file = new File(cmd.getOptionValue('P'));
try{
FileInputStream fStream = new FileInputStream(file);
try {
properties.load(fStream);
} catch (IOException e) {
e.printStackTrace();
}
}catch (FileNotFoundException e1){
e1.printStackTrace();
}
}
properties.putAll(cmd.getOptionProperties("D"));
properties.setProperty("conf.app.startup.timestamp", String.valueOf((new Date()).getTime())); //当前时间戳加入到配置中
logger.info("properties:"+properties.toString());
}
}
}
}