《Maven 安装与使用》
1. 安装 Maven
wget http://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.6.2/binaries/apache-maven-3.6.2-bin.zip
# 解压到 /opt
unzip apache-maven-3.6.2-bin.zip -d /opt
# 配置环境变量
export PATH=$PATH:/opt/apache-maven-3.6.2
2. 引入第三方依赖
在 pom.xml 的 <dependencies> 中添加依赖,至少包含 groupId、artifactId、version 和 scope 四个标签:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.andy.app</groupId>
<artifactId>app</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>app</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20190722</version>
</dependency>
</dependencies>
</project>
scope 可选值:test、compile、runtime、provided、import 等。
参考:https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html
3. 编译项目
mvn -e compile -X
4. 运行主类
# 通过 exec 插件
mvn exec:java -Dexec.mainClass="com.andy.app.App"
# 或进入 target/classes 目录直接运行
java com.andy.app.App
# 或使用带依赖的 fat jar
java -cp target/app-1.0-SNAPSHOT-jar-with-dependencies.jar com.andy.app.App
5. 查找依赖
- 根据 groupId / artifactId 查询:https://search.maven.org/
- org.json 库 API:https://stleary.github.io/JSON-java/
阅读 —
·
全站 —