系统要求

Maven 3.9+ requires JDK 8 or above to execute.

安装

下载

maven 3.9.8 下载tar.gz: apache-maven-3.9.8-bin.tar.gz

安装

  1. 解压apache-maven-3.9.8-bin.tar.gz到指定目录**/usr/local/maven**
1# -z 表示gzip压缩的tar包。
2# -x 表示解压。
3# -v 表示显示过程中的信息。
4# -f 表示后面跟压缩文件名。
5# apache-maven-3.9.8-bin.tar.gz 是压缩文件的名字。
6# -C /usr/local/maven 表示将解压到指定的目录。 注意目录需要存在
7# --strip-components 1 表示脱去一层目录。
8tar -xzvf apache-maven-3.9.8-bin.tar.gz -C /usr/local/maven --strip-components 1
  1. 配置环境变量

笔记是macos操作系统shell类型是zsh, 如果是linux bash,需要配置vi ~/.bashrc, 如果是window则在环境变量添加

vi ~/.zshrc

追加

1export M2_HOME=/usr/local/maven
2export PATH=$PATH:$M2_HOME/bin
  1. 修改conf/settings.xml文件
 1# 1. 修改仓库存储位置, 默认是${user.home}/.m2/repository, 笔记习惯性修改到扩展硬盘上
 2...
 3  <localRepository>/path/to/local/repo</localRepository>
 4...
 5
 6# 2. 在<servers></servers>节点中增加账号
 7
 8....
 9
10注意 以下配置的id, 在项目中会使用
11<servers>
12    ...
13    <server>
14      <id>local-releases</id>
15      <username>deploy</username>
16      <password>123456</password>
17    </server>
18    <server> 
19      <id>local-snapshots</id>
20      <username>deploy</username>
21      <password>123456</password>
22    </server>
23    <server>
24      <id>local-repo</id>
25      <username>deploy</username>
26      <password>123456</password>
27    </server>
28    ...
29</servers>
30...

source ~/.zshrc 激活

  1. 查看maven版本
1mvn -version
2
3Apache Maven 3.9.8 (36645f6c9b5079805ea5009217e36f2cffd34256)
4Maven home: /Volumes/data/software/maven
5Java version: 17.0.11, vendor: Oracle Corporation, runtime: /Volumes/data/software/java/graalvm-jdk-17.0.11+7.1/Contents/Home
6Default locale: zh_CN_#Hans, platform encoding: UTF-8
7OS name: "mac os x", version: "12.5", arch: "x86_64", family: "mac"

创建项目

  • groupId: com.clibing
  • artifactId: demo
  1. 创建项目
1mvn archetype:generate -DgroupId=com.clibing -DartifactId=demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
2cd demo
3mvn install
  1. 配置私服

编辑 pom.xml

1cd demo
2vi pom.xml
 1<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 3  <modelVersion>4.0.0</modelVersion>
 4  <groupId>com.clibing</groupId>
 5  <artifactId>demo</artifactId>
 6  <packaging>jar</packaging>
 7  <version>1.0-SNAPSHOT</version>
 8  <name>demo</name>
 9  <url>http://maven.apache.org</url>
10  <dependencies>
11    <dependency>
12      <groupId>junit</groupId>
13      <artifactId>junit</artifactId>
14      <version>3.8.1</version>
15      <scope>test</scope>
16    </dependency>
17  </dependencies>
18
19  <distributionManagement> 
20      <repository>
21          <id>local-releases</id>
22          <url>http://localhost:8081/repository/release/</url>
23      </repository>
24      <snapshotRepository>
25          <id>local-snapshots</id>
26          <url>http://localhost:8081/repository/snapshots/</url>
27      </snapshotRepository>
28  </distributionManagement>
29
30  <repositories>
31        <repository>
32            <id>local-repo</id>
33            <url>http://localhost:8081/repository/maven-public/</url>
34            <releases>
35                <enabled>true</enabled>
36            </releases>
37            <snapshots>
38                <enabled>true</enabled>
39            </snapshots>
40        </repository>
41    </repositories>
42
43</project>

发布

1mvn clean install -Dmaven.test.skip=true deploy

输出日志

 1[INFO] Scanning for projects...
 2[INFO] 
 3[INFO] --------------------------< com.clibing:demo >--------------------------
 4[INFO] Building demo 1.0-SNAPSHOT
 5[INFO]   from pom.xml
 6[INFO] --------------------------------[ jar ]---------------------------------
 7[INFO] 
 8[INFO] --- clean:3.2.0:clean (default-clean) @ demo ---
 9[INFO] Deleting /Volumes/data/workspace/temp/demo/target
10[INFO] 
11[INFO] --- resources:3.3.1:resources (default-resources) @ demo ---
12[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
13[INFO] skip non existing resourceDirectory /Volumes/data/workspace/temp/demo/src/main/resources
14[INFO] 
15[INFO] --- compiler:3.13.0:compile (default-compile) @ demo ---
16[INFO] Recompiling the module because of changed source code.
17[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
18[INFO] Compiling 1 source file with javac [debug target 1.8] to target/classes
19[WARNING] 未与 -source 8 一起设置引导类路径
20[INFO] 
21[INFO] --- resources:3.3.1:testResources (default-testResources) @ demo ---
22[INFO] Not copying test resources
23[INFO] 
24[INFO] --- compiler:3.13.0:testCompile (default-testCompile) @ demo ---
25[INFO] Not compiling test sources
26[INFO] 
27[INFO] --- surefire:3.2.5:test (default-test) @ demo ---
28[INFO] Tests are skipped.
29[INFO] 
30[INFO] --- jar:3.4.1:jar (default-jar) @ demo ---
31[INFO] Building jar: /Volumes/data/workspace/temp/demo/target/demo-1.0-SNAPSHOT.jar
32[INFO] 
33[INFO] --- install:3.1.2:install (default-install) @ demo ---
34[INFO] Installing /Volumes/data/workspace/temp/demo/pom.xml to /Volumes/data/storage/m2_repository/com/clibing/demo/1.0-SNAPSHOT/demo-1.0-SNAPSHOT.pom
35[INFO] Installing /Volumes/data/workspace/temp/demo/target/demo-1.0-SNAPSHOT.jar to /Volumes/data/storage/m2_repository/com/clibing/demo/1.0-SNAPSHOT/demo-1.0-SNAPSHOT.jar
36[INFO] 
37[INFO] --- resources:3.3.1:resources (default-resources) @ demo ---
38[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
39[INFO] skip non existing resourceDirectory /Volumes/data/workspace/temp/demo/src/main/resources
40[INFO] 
41[INFO] --- compiler:3.13.0:compile (default-compile) @ demo ---
42[INFO] Nothing to compile - all classes are up to date.
43[WARNING] Overwriting artifact's file from /Volumes/data/workspace/temp/demo/target/demo-1.0-SNAPSHOT.jar to /Volumes/data/workspace/temp/demo/target/classes
44[INFO] 
45[INFO] --- resources:3.3.1:testResources (default-testResources) @ demo ---
46[INFO] Not copying test resources
47[INFO] 
48[INFO] --- compiler:3.13.0:testCompile (default-testCompile) @ demo ---
49[INFO] Not compiling test sources
50[INFO] 
51[INFO] --- surefire:3.2.5:test (default-test) @ demo ---
52[INFO] Tests are skipped.
53[INFO] 
54[INFO] --- jar:3.4.1:jar (default-jar) @ demo ---
55[INFO] 
56[INFO] --- install:3.1.2:install (default-install) @ demo ---
57[INFO] Installing /Volumes/data/workspace/temp/demo/pom.xml to /Volumes/data/storage/m2_repository/com/clibing/demo/1.0-SNAPSHOT/demo-1.0-SNAPSHOT.pom
58[INFO] Installing /Volumes/data/workspace/temp/demo/target/demo-1.0-SNAPSHOT.jar to /Volumes/data/storage/m2_repository/com/clibing/demo/1.0-SNAPSHOT/demo-1.0-SNAPSHOT.jar
59[INFO] 
60[INFO] --- deploy:3.1.2:deploy (default-deploy) @ demo ---
61Downloading from local-snapshots: http://localhost:8081/repository/snapshots/com/clibing/demo/1.0-SNAPSHOT/maven-metadata.xml
62Uploading to local-snapshots: http://localhost:8081/repository/snapshots/com/clibing/demo/1.0-SNAPSHOT/demo-1.0-20240712.070318-1.pom
63Uploaded to local-snapshots: http://localhost:8081/repository/snapshots/com/clibing/demo/1.0-SNAPSHOT/demo-1.0-20240712.070318-1.pom (1.3 kB at 905 B/s)
64Uploading to local-snapshots: http://localhost:8081/repository/snapshots/com/clibing/demo/1.0-SNAPSHOT/demo-1.0-20240712.070318-1.jar
65Uploaded to local-snapshots: http://localhost:8081/repository/snapshots/com/clibing/demo/1.0-SNAPSHOT/demo-1.0-20240712.070318-1.jar (2.2 kB at 2.5 kB/s)
66Downloading from local-snapshots: http://localhost:8081/repository/snapshots/com/clibing/demo/maven-metadata.xml
67Uploading to local-snapshots: http://localhost:8081/repository/snapshots/com/clibing/demo/1.0-SNAPSHOT/maven-metadata.xml
68Uploaded to local-snapshots: http://localhost:8081/repository/snapshots/com/clibing/demo/1.0-SNAPSHOT/maven-metadata.xml (759 B at 1.7 kB/s)
69Uploading to local-snapshots: http://localhost:8081/repository/snapshots/com/clibing/demo/maven-metadata.xml
70Uploaded to local-snapshots: http://localhost:8081/repository/snapshots/com/clibing/demo/maven-metadata.xml (273 B at 610 B/s)
71[INFO] ------------------------------------------------------------------------
72[INFO] BUILD SUCCESS
73[INFO] ------------------------------------------------------------------------
74[INFO] Total time:  9.106 s
75[INFO] Finished at: 2024-07-12T15:03:27+08:00
76[INFO] ------------------------------------------------------------------------

私服预览

清理本机快照数据

自己用golang写了一个清理本地过时的maven snapshots jar包,只保留最后一个, 支持预览、确认删除

传送门

1# 查看将要删除的jar列表
2knife system maven /Volumes/data/storage/m2_repository 
3
4# 确认删除
5knife system maven -c /Volumes/data/storage/m2_repository