mvn 打包jar檔案
阿新 • • 發佈:2018-11-30
一、使用maven-assembly-plugin外掛打包
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.5.5</version> <configuration> <archive> <manifest> <mainClass>com.cn.demo.App</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin>
命令:
mvn package assembly:single
二、commandline
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(); } } }
執行:
java -jar Demo-1.0-SNAPSHOT-jar-with-dependencies.jar -n 192.168.0.1:9876