1. Use common-cli to write command line tools
commons-cli
It is a package provided by the Apache open source organization for parsing command line parameters.
First refer to the common-cli dependency package:
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.2</version>
Command definition:
private static final Options OPTIONS = new Options(); public void defineCommand() { OPTIONS.addOption("i", true, "the input directory where the proto files are"); OPTIONS.addOption("o", true, "the output directory which is the output path"); OPTIONS.addOption("c",true,"whether we use config.json or not"); OPTIONS.addOption("groupId",true,"maven project related parameter"); OPTIONS.addOption("artifactId",true,"maven project related parameter"); OPTIONS.addOption("artifactVersion",true,"maven project related parameter"); OPTIONS.addOption("h", true ,"get command input help"); OPTIONS.addOption("help", true ,"get command input help"); }
Command parsing:
CommandLine cli = parser.parse(OPTIONS, args); if (cli.hasOption("c")) { //TODO: }
Excuting an order:
java -jar common-cli-demo-1.0-SNAPSHOT.jar -c c:\cli\demo1\config.json
config.json:
{ "groupId": "common.cli.demo", "artifactId": "cli-demo", "artifactVersion": "demo-snapshot", "outputDirectory": "c" }
The config.json file name can be arbitrary, if not specified, the default configuration will be read.
2. Use the mustache template
mustache reference address: Github address
Template file generator.mustache:
<?xml version="1.0" encoding="GBK"?> <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> <properties> <entry key="base_package">{{base_package}}</entry> <!--配置实体包路径--> <entry key="base_entity_package">{{base_entity_package}}</entry> <!--配置mybatis Mapper包路径--> <entry key="base_mapper_package">{{base_mapper_package}}</entry> </properties>
Template parameter configuration xmltemplate.json:
{
"base_package": "com.demo.package",
"base_entity_package": "com.demo.package.entity",
"base_mapper_package": "com.demo.package.mapper"
}
Excuting an order:
java -jar common-cli-demo-1.1-SNAPSHOT.jar -c c:\cli\demo2\template.json -o c:\cli\demo2\