JCommander is a very small Java framework that makes it trivial to parse command line parameters.
译文:JCommander是一个非常小的Java框架,它使得解析命令行参数变得微不足道。
相关资料
- 文档: https://jcommander.org/
- github: http://github.com/cbeust/jcommander
依赖
<!-- https://mvnrepository.com/artifact/com.beust/jcommander -->
<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
<version>1.82</version>
</dependency>
完整 pom.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>cmd-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.beust/jcommander -->
<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
<version>1.82</version>
</dependency>
</dependencies>
<build>
<finalName>app</finalName>
<plugins>
<!-- 跳过测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<!-- jar -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<!-- 生成的jar中不要包含pom.xml和pom.properties这两个文件 -->
<addMavenDescriptor>false</addMavenDescriptor>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<!--项目的启动类-->
<mainClass>com.example.Demo</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<!-- 依赖 dependencies -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-lib</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<includeScope>runtime</includeScope>
<useBaseVersion>true</useBaseVersion>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
定义参数
package com.example;
import com.beust.jcommander.Parameter;
public class CommandOptions {
@Parameter(names = {"--name", "-n"},
description = "用户姓名")
private String name;
public String getName() {
return name;
}
}
入口文章来源:https://www.toymoban.com/news/detail-506807.html
package com.example;
import com.beust.jcommander.JCommander;
public class Demo {
public static void main(String[] args) {
CommandOptions commandOptions = new CommandOptions();
JCommander commander = JCommander.newBuilder()
.addObject(commandOptions)
.build();
// 解析参数
commander.parse(args);
System.out.println(commandOptions.getName());
}
}
# 打包
$ mvn clean package
# 执行
$ java -jar target/app.jar --name Tom
Tom
$ java -jar target/app.jar -n Tom
Tom
参考文章来源地址https://www.toymoban.com/news/detail-506807.html
- 使用 JCommander 解析命令行参数
- 使用 Maven 生成 Jar 文件的三种方式
到了这里,关于Java:使用JCommander解析命令行参数的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!