一、使用maven-assembly-plugin插件打包
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<archive>
<manifest>
<!-- com.cn.demo.TestClient1 -->
<mainClass>com.cn.demo.App</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
命令:
mvn package assembly:single
二、commandline
应用程序入口App()
package com.cn.demo;
import org.apache.commons.cli.*;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )throws ParseException
{
Options options = new Options();
/**
* 1、初始化命令
* opt : n 在命令行输入: java -jar demo.jar -n 参数1
* longOpt : namesrvAddr 是 n 的全写方式 ,与 opt 一致
*/
Option opt = new Option("n", "namesrvAddr", true,"Name server address list, eg: 192.168.0.1:9876;192.168.0.2:9876");
opt.setRequired(true);//true 在命令行的时候必须要添加参数, false 没有参数也可以
options.addOption(opt);
CommandLineParser parser = new DefaultParser();
CommandLine commandLine = parser.parse(options, args);
String optNmae = "n";
if (commandLine.hasOption(optNmae)) {
for (String s : commandLine.getOptionValues(optNmae)) {
System.out.print(s+" ");
}
//调业务代码
new UserService().test1();
}
}
}
三、新增 MANIFEST.MF
运行:
java -jar Demo-1.0-SNAPSHOT-jar-with-dependencies.jar -n 192.168.0.1:9876