Maven简单使用

Maven简单使用

我的理解,Maven之于Java相当于npm之于Vue,一个包管理,Maven环境准备

  • 生成和运行默认的Java项目:
$ mvn archetype:generate "-DgroupId=cn.kfchen" "-DartifactId=second-demo" "-DarchetypeArtifactId=maven-archetype-quickstart" "-DinteractiveMode=false"

依次执行:

$ mvn compile # 编译.
$ mvn package # 打包.
$ java -cp .\target\second-demo-1.0-SNAPSHOT.jar cn.kfchen.App # 执行二进制文件.

关于项目的生命周期:

  • clean: 清除之前编译打包的 target 目录
  • validate: 可行性测试
  • compile: 编译
  • test: 执行test实例
  • package: 打包
  • verify: 验证
  • install: 将编译好的jar包复制到 本地repo库
  • site: 生成一个默认网站
  • deploy: 将jar发布到私有仓库
    Idea 中的快捷操作

    Idea 中的快捷操作

具体实例:

$ second-demo> mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< cn.kfchen:second-demo >------------------------
[INFO] Building second-demo 1.0-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
Downloading from aliyun-maven: https://maven.aliyun.com/repository/public/junit/junit/3.8.1/junit-3.8.1.pom
Downloaded from aliyun-maven: https://maven.aliyun.com/repository/public/junit/junit/3.8.1/junit-3.8.1.pom (998 B at 2.7 kB/s)
[INFO]
[INFO] --- resources:3.4.0:resources (default-resources) @ second-demo ---
[INFO] skip non existing resourceDirectory G:\Java_ws\second-demo\src\main\resources
[INFO]
[INFO] --- compiler:3.15.0:compile (default-compile) @ second-demo ---
[INFO] Recompiling the module because of changed source code.
[INFO] Compiling 1 source file with javac [debug release 8] to target\classes
[WARNING] 源值 8 已过时,将在未来发行版中删除
[WARNING] 目标值 8 已过时,将在未来发行版中删除
[WARNING] 要隐藏有关已过时选项的警告, 请使用 -Xlint:-options。
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.408 s
[INFO] Finished at: 2026-05-10T20:43:26+08:00
[INFO] ------------------------------------------------------------------------
(base) PS G:\Java_ws\second-demo> mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< cn.kfchen:second-demo >------------------------
[INFO] Building second-demo 1.0-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
Downloading from aliyun-maven: https://maven.aliyun.com/repository/public/junit/junit/3.8.1/junit-3.8.1.jar
Downloaded from aliyun-maven: https://maven.aliyun.com/repository/public/junit/junit/3.8.1/junit-3.8.1.jar (121 kB at 322 kB/s)
[INFO]
[INFO] --- resources:3.4.0:resources (default-resources) @ second-demo ---
[INFO] skip non existing resourceDirectory G:\Java_ws\second-demo\src\main\resources
[INFO]
[INFO] --- compiler:3.15.0:compile (default-compile) @ second-demo ---
[INFO] Nothing to compile - all classes are up to date.
[INFO]
[INFO] --- resources:3.4.0:testResources (default-testResources) @ second-demo ---
[INFO] skip non existing resourceDirectory G:\Java_ws\second-demo\src\test\resources
[INFO]
[INFO] --- compiler:3.15.0:testCompile (default-testCompile) @ second-demo ---
[INFO] Recompiling the module because of changed dependency.
[INFO] Compiling 1 source file with javac [debug release 8] to target\test-classes
[WARNING] 源值 8 已过时,将在未来发行版中删除
[WARNING] 目标值 8 已过时,将在未来发行版中删除
[WARNING] 要隐藏有关已过时选项的警告, 请使用 -Xlint:-options。
[INFO]
[INFO] --- surefire:3.5.4:test (default-test) @ second-demo ---
[INFO] Using auto detected provider org.apache.maven.surefire.junit.JUnit3Provider
Downloading from aliyun-maven: https://maven.aliyun.com/repository/public/org/apache/maven/surefire/surefire-junit3/3.5.4/surefire-junit3-3.5.4.pom
Downloaded from aliyun-maven: https://maven.aliyun.com/repository/public/org/apache/maven/surefire/surefire-junit3/3.5.4/surefire-junit3-3.5.4.pom (3.2 kB at 6.3 kB/s)
Downloading from aliyun-maven: https://maven.aliyun.com/repository/public/org/apache/maven/surefire/common-junit3/3.5.4/common-junit3-3.5.4.pom
Downloaded from aliyun-maven: https://maven.aliyun.com/repository/public/org/apache/maven/surefire/common-junit3/3.5.4/common-junit3-3.5.4.pom (2.7 kB at 6.7 kB/s)
Downloading from aliyun-maven: https://maven.aliyun.com/repository/public/org/apache/maven/surefire/surefire-junit3/3.5.4/surefire-junit3-3.5.4.jar
Downloaded from aliyun-maven: https://maven.aliyun.com/repository/public/org/apache/maven/surefire/surefire-junit3/3.5.4/surefire-junit3-3.5.4.jar (24 kB at 43 kB/s)
Downloading from aliyun-maven: https://maven.aliyun.com/repository/public/org/apache/maven/surefire/common-junit3/3.5.4/common-junit3-3.5.4.jar
Downloaded from aliyun-maven: https://maven.aliyun.com/repository/public/org/apache/maven/surefire/common-junit3/3.5.4/common-junit3-3.5.4.jar (12 kB at 14 kB/s)
[INFO]
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running cn.kfchen.AppTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.020 s -- in cn.kfchen.AppTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] --- jar:3.5.0:jar (default-jar) @ second-demo ---
[INFO] Building jar: G:\Java_ws\second-demo\target\second-demo-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  4.636 s
[INFO] Finished at: 2026-05-10T20:43:48+08:00
[INFO] ------------------------------------------------------------------------

$ second-demo> tree
.
├─src
│  ├─main
│  │  └─java
│  │      └─cn
│  │          └─kfchen
│  └─test
│      └─java
│          └─cn
│              └─kfchen
└─target
    ├─classes
    │  └─cn
    │      └─kfchen
    ├─generated-sources
    │  └─annotations
    ├─generated-test-sources
    │  └─test-annotations
    ├─maven-archiver
    ├─maven-status
    │  └─maven-compiler-plugin
    │      ├─compile
    │      │  └─default-compile
    │      └─testCompile
    │          └─default-testCompile
    ├─surefire-reports
    └─test-classes
        └─cn
            └─kfchen
$ second-demo> java -cp .\target\second-demo-1.0-SNAPSHOT.jar cn.kfchen.App
Hello World!

使用社交账号登录

  • Loading...
  • Loading...
  • Loading...
  • Loading...
  • Loading...