Apache Commons Geometry 常用 CLI 命令与工具详解
面向 Apache Commons Geometry 1.0 的常用 CLI 命令与工具详解,覆盖 Maven 构建与依赖命令及 JDK 配套工具(javac/java/jar/javap),每条命令附真实回显。
Apache Commons Geometry 常用 CLI 命令与工具详解
目标:梳理 Apache Commons Geometry 1.0 开发/运维中最常用的命令行工具与命令——Maven 构建与依赖管理、JDK 配套工具(
java/javac/jar/javap)以及工程验证方法,每条命令均附实测回显。以下输出均在 Ubuntu 22.04(JDK 17.0.20.1 + Maven 3.6.3)实测通过。
一、环境信息
本节先固定本次实测所用软硬件基线,便于读者逐项对照复现。
- 操作系统:Ubuntu 22.04.5 LTS(x86_64)
- JDK:OpenJDK 17.0.20.1(
java -version实测) - 构建工具:Apache Maven 3.6.3(
mvn -version实测) - 目标库:Apache Commons Geometry 1.0(
org.apache.commons:commons-geometry-euclidean:1.0) - 示例工程目录:
/opt/geo-demo(含GeometryCheck.java、demo/Geometry2DDemo.java、demo/Geometry3DDemo.java、demo/PrecisionDemo.java) - 运行期堆内存:示例统一
-Xmx256m
二、工具概览
Apache Commons Geometry 是纯 Java 库,没有自带 CLI 程序(不像 MySQL、Redis 有可执行文件)。它的“命令”就是配套的构建与 JDK 工具链,按用途分为三组:
| 工具 | 版本(实测) | 用途 | 关键命令 |
|---|---|---|---|
| Maven | 3.6.3 | 依赖解析与构建 | mvn compile、mvn package、mvn dependency:tree |
| java | 17.0.20.1 | 运行 Geometry 程序 | java -Xmx256m -cp commons-geometry-euclidean-1.0.jar:commons-geometry-core-1.0.jar demo.Main |
| javac | 17.0.20.1 | 手动编译源码 | javac -encoding UTF-8 -cp commons-geometry-euclidean-1.0.jar Demo.java |
| jar | 17.0.20.1 | 查看/解压 jar 包 | jar tf、jar xf |
| javap | 17.0.20.1 | 反查类结构与 API | javap -cp commons-geometry-euclidean-1.0.jar org.apache.commons.geometry.euclidean.twod.Line |
💡 技术拓展:几何计算库不止 Apache Commons Geometry,其命令行配套也不同——
库 运行形态 命令行配套 Commons Geometry Java 库(无 CLI) Maven + JDK 工具链 JTS Topology Suite Java 库(无 CLI) Maven + JDK 工具链 GeoTools Java 库(无 CLI) Maven + JDK 工具链 Shapely Python 库 python/pip,可配合 Jupyter本文场景为 Java 服务内几何计算,选用 Commons Geometry,命令行配套即 Maven + JDK 工具链。
三、前置条件
执行本文命令前需满足:
- JDK 17 与 Maven 已安装(安装步骤见《Ubuntu 22.04 安装 Apache Commons Geometry 指南》)。
- 示例工程已创建:
/opt/geo-demo,pom.xml已引入commons-geometry-euclidean:1.0。 - 已生成依赖 classpath 文件:
cd /opt/geo-demo
mvn -q -B dependency:build-classpath -Dmdep.outputFile=cp.txt
wc -c cp.txt
输出:
595 cp.txt
cp.txt 保存了从本地 Maven 仓库解析出的完整依赖路径(含 commons-geometry-euclidean-1.0.jar、commons-geometry-core-1.0.jar 与 4 个 commons-numbers-* jar),供 java 直接运行使用。
四、Maven 构建与依赖命令
4.1 版本与环境:mvn -version
确认 Maven 与 JDK 版本,排查构建环境问题:
mvn -version
输出:
Apache Maven 3.6.3
Maven home: /usr/share/maven
Java version: 17.0.20.1, vendor: Ubuntu, runtime: /usr/lib/jvm/java-17-openjdk-amd64
Default locale: en_US, platform encoding: ANSI_X3.4-1968
OS name: "linux", version: "6.8.0-138-generic", arch: "amd64", family: "unix"
解读:Java version: 17.0.20.1 说明 Maven 已使用 JDK 17,可直接编译 Java 17 代码。
4.2 全量命令速查:mvn --help
查看 Maven 全部命令行选项:
mvn --help 2>&1 | head -20
输出节选(实测首尾):
usage: mvn [options] [<goal(s)>] [<phase(s)>]
Options:
-am,--also-make If project list is specified, also
build projects required by the
list
-amd,--also-make-dependents If project list is specified, also
build projects that depend on
projects on the list
-B,--batch-mode Run in non-interactive (batch)
mode (disables output color)
-b,--builder <arg> The id of the build strategy to
use
-C,--strict-checksums Fail the build if checksums don't
match
-c,--lax-checksums Warn if checksums don't match
-cpu,--check-plugin-updates Ineffective, only kept for
backward compatibility
-D,--define <arg> Define a system property
-e,--errors Produce execution error messages
-emp,--encrypt-master-password <arg> Encrypt master security password
-ep,--encrypt-password <arg> Encrypt server password
-f,--file <arg> Force the use of an alternate POM
file (or directory with pom.xml)
-fae,--fail-at-end Only fail the build afterwards;
allow all non-impacted builds to
continue
-ff,--fail-fast Stop at first failure in
reactorized builds
常用选项解读:-B 批处理模式(CI 推荐,禁用彩色输出)、-D 定义系统属性(如 -DskipTests)、-f 指定 pom 文件、-q 安静模式(只输出错误)。
4.3 编译:mvn compile
编译工程主源码到 target/classes:
cd /opt/geo-demo
mvn -B compile
输出节选(实测首尾):
[INFO] -------------------------< demo:geometry-demo >-------------------------
[INFO] Building geometry-demo 1.0.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ geometry-demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 6 source files to /opt/geo-demo/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] Total time: 1.283 s
验证编译产物:
ls target/classes/demo/
输出:
Geometry2DDemo.class
Geometry3DDemo.class
Precision2Demo.class
Precision3Demo.class
PrecisionDemo.class
GeometryCheck.class
4.4 打包:mvn package
执行完整生命周期(compile → test → package),产出可分发 jar:
mvn -B package
输出节选(实测首尾):
[INFO] Building jar: /opt/geo-demo/target/geometry-demo-1.0.0.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] Total time: 2.412 s
验证产物:
ls -l target/geometry-demo-1.0.0.jar
输出:
-rw-r--r-- 1 root root 11609 Oct 13 18:08 target/geometry-demo-1.0.0.jar
⚠️ 注意:该 jar 不包含依赖(非 fat jar)。分发到其他机器时需连同
cp.txt中的 6 个依赖 jar 一起部署,或用java -cp指定依赖。
4.5 依赖树:mvn dependency:tree
查看依赖传递关系,确认坐标与版本:
mvn -B dependency:tree
输出节选(实测首尾):
[INFO] demo:geometry-demo:jar:1.0.0
[INFO] \- org.apache.commons:commons-geometry-euclidean:jar:1.0:compile
[INFO] +- org.apache.commons:commons-geometry-core:jar:1.0:compile
[INFO] +- org.apache.commons:commons-numbers-core:jar:1.0:compile
[INFO] +- org.apache.commons:commons-numbers-arrays:jar:1.0:compile
[INFO] +- org.apache.commons:commons-numbers-angle:jar:1.0:compile
[INFO] \- org.apache.commons:commons-numbers-quaternion:jar:1.0:compile
[INFO] BUILD SUCCESS
解读:commons-geometry-euclidean:1.0 传递依赖 core 与 4 个 commons-numbers-* 组件,共 5 个传递依赖。若出现版本冲突(不同版本号),可加 -Dverbose 查看冲突详情。
4.6 依赖 classpath:mvn dependency:build-classpath
将依赖路径导出为文件,供 java -cp 直接运行:
mvn -q -B dependency:build-classpath -Dmdep.outputFile=cp.txt
head -c 180 cp.txt
输出节选(实测首部):
/root/.m2/repository/org/apache/commons/commons-geometry-euclidean/1.0/commons-geometry-euclidean-1.0.jar:/root/.m2/repository/org/apache/commons/commons-geometry-core/1.0/commons-geometry-core-1.0.jar:/root/.m2/repository/org/apache/commons/commons-numbers-core/1.0/commons-numbers-core-1.0.jar:...
4.7 运行:mvn exec:java
通过 exec-maven-plugin 直接运行工程主类(mainClass 在 pom.xml 的插件配置中声明为 demo.Geometry2DDemo):
mvn -q exec:java
输出节选(实测首尾):
[v-add] a.add(b) = (4.0, 6.0)
[v-sub] a.subtract(b) = (2.0, 2.0)
[v-dot] a.dot(b) = 11.0
...
[region-x] [0,4]x[0,3] AND [2,6]x[0,3] intersection area = 6.0
[region-u] union area = 18.0
[INFO] BUILD SUCCESS
[INFO] Total time: 0.385 s
⚠️ 注意:实测中
-Dexec.mainClass无法覆盖pom.xml里配置的mainClass(插件配置优先级更高)。想运行其他主类时,用 4.6 的cp.txt+java直接运行,见 5.2。
4.8 清理:mvn clean
删除 target 目录,回到干净状态:
mvn -q clean
ls target 2>/dev/null || echo "target removed"
输出:
target removed
五、JDK 配套工具命令
5.1 运行:java
运行已编译的 Geometry 程序。基础用法:
java --help 2>&1 | head -6
输出节选(实测首部):
Usage: java [options] <mainclass> [args...]
(to execute a class)
or java [options] -jar <jarfile> [args...]
(to execute a jar file)
or java [options] -m <module>[/<mainclass>] [args...]
java [options] --module <module>[/<mainclass>] [args...]
(to execute the main class in a module)
or java [options] <sourcefile> [args]
(to execute a single source-file program)
运行带堆限制的 2D 示例(-Xmx256m 限制堆 256 MB,配合 cp.txt 指定依赖):
cd /opt/geo-demo
java -Xmx256m -cp "target/classes:$(cat cp.txt)" demo.Geometry2DDemo
输出节选(实测首尾):
[v-add] a.add(b) = (4.0, 6.0)
[v-sub] a.subtract(b) = (2.0, 2.0)
[v-dot] a.dot(b) = 11.0
[v-norm] a.norm() = 5.0
[v-unit] a.normalize() = (0.6000000000000001, 0.8)
[pt-dist] a.distance(b) = 2.8284271247461903
[line] fromPoints(0,0)-(4,0) = Line[origin= (0.0, 0.0), direction= (1.0, 0.0)]
[proj] line.project(2,3) = (2.0, 0.0)
[line-dis]line.distance(2,3) = 3.0
[x-line] l1.intersection(l2) = (2.0, 2.0)
[parallel]l1.isParallel(l2) = false
[seg] start/end = (0.0, 0.0) / (3.0, 4.0)
[seg-len] seg.getSize() = 5.0
[seg-mid] seg.getCentroid() = (1.5, 2.0)
[seg-has] seg.contains(1.5,2) = true
[area-convex] getSize() = 12.0
[area-centroid] = (2.0, 1.5)
[area-tree] toTree.getSize= 12.0
[area-verts] getVertices() = [(0.0, 0.0), (4.0, 0.0), (4.0, 3.0), (0.0, 3.0)]
[region-x] [0,4]x[0,3] AND [2,6]x[0,3] intersection area = 6.0
[region-u] union area = 18.0
-Xms/-Xmx 帮助原文:
java --help-extra 2>&1 | grep -A1 "Xmx\|Xms"
输出节选(实测首部):
-Xms<size> set initial Java heap size
-Xmx<size> set maximum Java heap size
-Xnoclassgc disable class garbage collection
运行 3D 示例:
java -Xmx256m -cp "target/classes:$(cat cp.txt)" demo.Geometry3DDemo
输出节选(实测首尾):
[v3-add] u.add(v) = (1.0, 1.0, 0.0)
[v3-dot] u.dot(v) = 0.0
[v3-cross] u.cross(v) = (0.0, 0.0, 1.0)
[v3-norm] u.norm() = 1.0
[v3-dist] u.distance(Vector3D.of(4,0,0)) = 3.0
[plane] fromPoints = Plane[origin= (0.0, 0.0, 0.0), normal= (0.0, 0.0, 1.0)]
[proj] plane.project(pt) = (2.0, 3.0, 0.0)
[offset] plane.offset(pt) = 5.0
[line3d] fromPoints = Line3D[origin= (2.0, 2.0, 0.0), direction= (0.0, 0.0, -1.0)]
[x] plane.intersection(line) = (2.0, 2.0, 0.0)
[x-plane] wall.intersection(plane) = Line3D[origin= (0.0, 0.0, 0.0), direction= (0.0, -1.0, 0.0)]
[p-parallel] roof.isParallel(high) = true
[p-dist] roof.offset(high) = 4.0
5.2 javac:手动编译
不依赖 Maven 时,用 javac 直接编译源码(含中文源码必须 -encoding UTF-8):
cd /opt/geo-demo
javac -encoding UTF-8 -cp "target/classes:$(cat cp.txt)" -d out src/main/java/demo/Geometry3DDemo.java
javac -version
输出:
javac 17.0.20.1
5.3 jar:查看与解压 jar 包
列出 commons-geometry-euclidean jar 内的 2D 直线相关类:
jar tf /opt/geo-jars/commons-geometry-euclidean-1.0.jar | grep "euclidean/twod/[A-Z].*\.class" | grep -v '\$' | head -12
输出:
org/apache/commons/geometry/euclidean/twod/AffineTransformMatrix2D.class
org/apache/commons/geometry/euclidean/twod/BoundaryList2D.class
org/apache/commons/geometry/euclidean/twod/BoundarySource2D.class
org/apache/commons/geometry/euclidean/twod/BoundarySourceBoundsBuilder2D.class
org/apache/commons/geometry/euclidean/twod/BoundarySourceLinecaster2D.class
org/apache/commons/geometry/euclidean/twod/Bounds2D.class
org/apache/commons/geometry/euclidean/twod/ConvexArea.class
org/apache/commons/geometry/euclidean/twod/EmbeddedTreeLineSubset.class
org/apache/commons/geometry/euclidean/twod/Line.class
org/apache/commons/geometry/euclidean/twod/LineConvexSubset.class
org/apache/commons/geometry/euclidean/twod/LineSpanningSubset.class
org/apache/commons/geometry/euclidean/twod/LineSubset.class
jar 工具版本与帮助:
jar --version
jar --help 2>&1 | head -8
输出节选(实测首部):
jar 17.0.20.1
Usage: jar [OPTION...] [ [--release VERSION] [-C dir] files] ...
jar creates an archive for classes and resources, and can manipulate or
restore individual classes or resources from an archive.
Examples:
# Create an archive called classes.jar with two class files:
jar --create --file classes.jar Foo.class Bar.class
5.4 javap:反查类结构
查看示例类 Geometry2DDemo 的公开结构:
javap -cp "target/classes:$(cat cp.txt)" demo.Geometry2DDemo
输出:
Compiled from "Geometry2DDemo.java"
public class demo.Geometry2DDemo {
public demo.Geometry2DDemo();
public static void main(java.lang.String[]);
}
反查库内 Line 类的继承与公开方法(判断可用 API):
javap -cp "$(cat cp.txt)" org.apache.commons.geometry.euclidean.twod.Line 2>&1 | head -14
输出节选(实测首部):
Compiled from "Line.java"
public final class org.apache.commons.geometry.euclidean.twod.Line extends org.apache.commons.geometry.core.partitioning.AbstractHyperplane<org.apache.commons.geometry.euclidean.twod.Vector2D> implements org.apache.commons.geometry.core.partitioning.EmbeddingHyperplane<org.apache.commons.geometry.euclidean.twod.Vector2D, org.apache.commons.geometry.euclidean.oned.Vector1D> {
static final java.lang.String TO_STRING_FORMAT;
org.apache.commons.geometry.euclidean.twod.Line(org.apache.commons.geometry.euclidean.twod.Vector2D$Unit, double, org.apache.commons.numbers.core.Precision$DoubleEquivalence);
public double getAngle();
public org.apache.commons.geometry.euclidean.twod.Vector2D$Unit getDirection();
public org.apache.commons.geometry.euclidean.twod.Vector2D getOffsetDirection();
public org.apache.commons.geometry.euclidean.twod.Vector2D getOrigin();
public double getOriginOffset();
public org.apache.commons.geometry.euclidean.twod.Line reverse();
...
}
解读:Line 继承 AbstractHyperplane、实现 EmbeddingHyperplane,构造器接收 Precision.DoubleEquivalence 作为精度参数(见《Apache Commons Geometry 配置参数详解》)。
六、注意事项
- 无系统级服务:Geometry 是进程内库,无守护进程、无端口、无日志文件;“运行”即 Java 进程执行。
-Xmx限制:示例统一-Xmx256m;生产按数据规模调整,区间 256-512 MB 足够。- classpath 完整性:
java -cp必须同时包含工程 class 目录与cp.txt中的全部依赖 jar,缺commons-numbers-core会报NoClassDefFoundError: Precision。 mvn exec:java的主类:以pom.xml插件配置为准,命令行-Dexec.mainClass实测不覆盖(见 4.7)。- 打包分发:
mvn package产物不含依赖,跨机器部署需一并携带依赖 jar。
七、常见问题(FAQ)
Q1:java 运行报 Could not find or load main class?
classpath 未包含类所在目录。确认用 -cp "target/classes:$(cat cp.txt)"(工程类 + 依赖),并核对类名全限定(如 demo.Geometry2DDemo)。
Q2:mvn exec:java 运行的不是我想要的主类?
pom.xml 的 exec-maven-plugin 配置优先于命令行 -Dexec.mainClass(实测)。改 pom 中 <mainClass> 或改用 java -cp 直跑。
Q3:怎么确认某个类在哪个 jar?
jar tf /opt/geo-jars/commons-geometry-euclidean-1.0.jar | grep "twod/Line.class"
输出:
org/apache/commons/geometry/euclidean/twod/Line.class
Q4:jar tf 输出很长,怎么只看类?
用 grep 过滤:jar tf commons-geometry-euclidean-1.0.jar | grep '\.class$' | grep -v '\$',\$ 过滤匿名/内部类。
Q5:怎么验证依赖 jar 完整?
jar tf /root/.m2/repository/org/apache/commons/commons-numbers-core/1.0/commons-numbers-core-1.0.jar | grep "Precision.class"
输出:
org/apache/commons/numbers/core/Precision.class
八、参考链接
- Apache Commons Geometry 官方站点:https://commons.apache.org/proper/commons-geometry/
- Maven 生命周期与插件文档:https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html
- JDK 工具参考(java/javac/jar/javap):https://docs.oracle.com/en/java/javase/17/tools/
- exec-maven-plugin 文档:https://www.mojohaus.org/exec-maven-plugin/