文章目录
下载 && 解压
在官网:http://maven.apache.org/download.cgi
下载 https://mirrors.bfsu.edu.cn/apache/maven/maven-3/3.8.1/binaries/apache-maven-3.8.1-bin.zip
解压到指定目录(我是~/javaweb/
) unzip apache-maven-3.8.1-bin.zip -d ~/javaweb/
查看该目录下的内容:
文本格式:
leung@wuyujin ~ % ls -alG ~/javaweb
total 0
drwxr-xr-x 4 leung staff 128 6 2 22:20 .
drwxr-xr-x+ 38 leung staff 1216 6 2 22:03 ..
drwxr-xr-x 9 leung staff 288 11 7 2019 apache-maven-3.8.1
drwxr-xr-x 2 leung staff 64 6 2 22:20 mvnrepo
leung@wuyujin ~ % ls -alG ~/javaweb/apache-maven-3.8.1
total 64
drwxr-xr-x 9 leung staff 288 11 7 2019 .
drwxr-xr-x 4 leung staff 128 6 2 22:20 ..
-rw-r--r-- 1 leung staff 17504 11 7 2019 LICENSE
-rw-r--r-- 1 leung staff 5141 11 7 2019 NOTICE
-rw-r--r-- 1 leung staff 2612 11 7 2019 README.txt
drwxr-xr-x 8 leung staff 256 11 7 2019 bin
drwxr-xr-x 4 leung staff 128 11 7 2019 boot
drwxr-xr-x 6 leung staff 192 6 2 22:11 conf
drwxr-xr-x 65 leung staff 2080 11 7 2019 lib
leung@wuyujin ~ %
这熟悉的目录结构。
配置文件修改
环境变量配置
只是为了在任意目录都可以直接使用mvn
命令(且不需要输入命令的全路径)。
本步骤可省略
配置项:
export MVN_HOME=/Users/leung/javaweb/apache-maven-3.8.1
export PATH=$MVN_HOME/bin:$PATH
将其写入当前用户、当前shell的配置文件中,我的是~/.zshrc
写入后保存,重载配置文件,使之生效:source ~/.zshrc
查看配置是否生效:
leung@wuyujin ~ % echo $MVN_HOME
/Users/leung/javaweb/apache-maven-3.8.1
leung@wuyujin ~ % echo $PATH
/Users/leung/javaweb/apache-maven-3.8.1/bin:/System/Volumes/Data/Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home/:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin
leung@wuyujin ~ % where mvn
/Users/leung/javaweb/apache-maven-3.8.1/bin/mvn
leung@wuyujin ~ % mvn --version
Apache Maven 3.8.1 (05c21c65bdfed0f71a2f2ada8b84da59348c4c5d)
Maven home: /Users/leung/javaweb/apache-maven-3.8.1
Java version: 1.8.0_291, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "mac os x", version: "10.16", arch: "x86_64", family: "mac"
leung@wuyujin ~ %
使用测试
其实可以在命令行直接使用maven命令新建不同类型的java项目。
新建一个web项目mvn archetype:generate -DgroupId=com.wuyujin -DartifactId=webdemo -DarchetypeArtifactId=maven-archetype-webapp
leung@wuyujin coderepo % mvn archetype:generate -DgroupId=com.wuyujin -DartifactId=webdemo -DarchetypeArtifactId=maven-archetype-webapp
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:3.2.0:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.2.0:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO]
[INFO] --- maven-archetype-plugin:3.2.0:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
[WARNING] No archetype found in remote catalog. Defaulting to internal catalog
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-webapp/1.0/maven-archetype-webapp-1.0.pom
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-webapp/1.0/maven-archetype-webapp-1.0.pom (533 B at 416 B/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-bundles/2/maven-archetype-bundles-2.pom
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-bundles/2/maven-archetype-bundles-2.pom (1.5 kB at 1.6 kB/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetype/maven-archetype-parent/1/maven-archetype-parent-1.pom
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetype/maven-archetype-parent/1/maven-archetype-parent-1.pom (1.3 kB at 1.4 kB/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/maven-parent/4/maven-parent-4.pom
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/maven-parent/4/maven-parent-4.pom (10.0 kB at 10 kB/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/apache/3/apache-3.pom
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/apache/3/apache-3.pom (3.4 kB at 4.1 kB/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-webapp/1.0/maven-archetype-webapp-1.0.jar
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-webapp/1.0/maven-archetype-webapp-1.0.jar (3.9 kB at 3.3 kB/s)
[INFO] Using property: groupId = com.wuyujin
[INFO] Using property: artifactId = webdemo
Define value for property 'version' 1.0-SNAPSHOT: :
[INFO] Using property: package = com.wuyujin
Confirm properties configuration:
groupId: com.wuyujin
artifactId: webdemo
version: 1.0-SNAPSHOT
package: com.wuyujin
Y: : y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-webapp:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: /Users/leung/javaweb/coderepo
[INFO] Parameter: package, Value: com.wuyujin
[INFO] Parameter: groupId, Value: com.wuyujin
[INFO] Parameter: artifactId, Value: webdemo
[INFO] Parameter: packageName, Value: com.wuyujin
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: /Users/leung/javaweb/coderepo/webdemo
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 16.377 s
[INFO] Finished at: 2021-06-02T22:49:59+08:00
[INFO] ------------------------------------------------------------------------
leung@wuyujin coderepo %
接下来在命令行完成install和package, run jar.
cd webdemo
mvn install
mvn package
package 动作会生成一个 war
该 wat 放置到 tomcat/jetty 等servlet容器中就可以运行/访问。
当执行过mvn archetype:generate -DgroupId=com.wuyujin -DartifactId=webdemo -DarchetypeArtifactId=maven-archetype-webapp
之后,
会生成webdemo
,其中有pom.xml
以及src/
目录及src/
目录下的默认源码。
mvn install 会生成 target/
目录及其下内容。