常见命令

清理 mvn clean

编译 mvn compile

打包 mvn package

运行 run

eg: java -jar target/gs-maven-0.1.0.jar

本地安装 mvn install

install to local Repository

Scope

compile

  • 编译时需要

    • 默认运行时也需要

test

  • 编译测试时需要

    • 默认运行测试时也需要

runtime

  • 编译时不需要,运行时需要

    • 例如:jdbc 驱动,mysql

provided

  • 编译时需要,运行时不需要,

    • 注:

      • 不是用不到,而是由别人提供

        • 这里的别人是,服务器等环境提供

LifeCycle 生命周期

  • 参考

  • 生命周期由 phase 组成
  • 一个 phase,又会触发一个或多个 goal
  • 类比记忆法

    • lifecycle –> package 包
    • phase –> 类
    • goal –> 方法
  • 名称

    • lifecycle

      • 单词,default
    • phase

      • 单词,eg: process-classes, generate-sources, compile, clean,
    • goal

      • abc:xyz, eg: compiler:compile, compiler:testCompile
  • 实际使用的命令

    • mvn compile

      • 解说,走 lifecycle,从开始 validate 到 (goal) compile 为止
      • 注:

        • 即,这是的命令是 phase
        • 其实,命令也可以是 goal

Jar 坐标

groupId

artifactId

version

  • 注意

    • 以-SNAPSHOT 结尾的版本号,被视为开发版本

      • 每次 maven 都会重新下载
      • 只能内部使用,不允许公开发布

插件

发布 artifact

配置

依赖

  • scope

    • 有效范围
1
2
3
4
5
6
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.12</version>
  <scope>test</scope>
</dependency>

继承与子模块 <parent>

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<project>
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>com.mycompany.app</groupId>
    <artifactId>my-app</artifactId>
    <version>1</version>
  </parent>

  <artifactId>my-module</artifactId>
</project>
  • <parent> 作用

    • 指定当前模块属于哪个项目
    • 当前模块在 <parent> 中没有具体指定的内容,会继承父项目的

      • 例如:如果在<parent>中省略 <version>, version 将会和父项目一致

父项目位置

上级目录 或 maven 本地 Repository

不用在<parent>中特别声明

其它情况,未安装到本地 Repository,同级目录或随便放置的目录

使用 <relativePath> 指定位置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<project>
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>com.mycompany.app</groupId>
    <artifactId>my-app</artifactId>
    <version>1</version>
    <relativePath>../parent/pom.xml</relativePath>
  </parent>

  <artifactId>my-module</artifactId>
</project>

项目聚合 Project Aggregation

https://maven.apache.org/guides/introduction/introduction-to-the-pom.html#project-aggregation

  • 作用

    • 编译父项目,子模块一起编译
  • 实现:

    1. 需要在父项目中指定子模块的名称和位置,设置 <modules>
    2. 设置父项目<packaging>pom</packaging>

       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      
      <project>
        <modelVersion>4.0.0</modelVersion>
      
        <groupId>com.mycompany.app</groupId>
        <artifactId>my-app</artifactId>
        <version>1</version>
        <packaging>pom</packaging>
      
        <modules>
          <module>../my-module</module>
        </modules>
      </project>
      • 子模块和父项目 pom.xml 同级

        • 说明名称即可
      • 子模块和父项目 pom.xml 在不同位置

        • 声明具体位置

Maven 变量

https://maven.apache.org/guides/introduction/introduction-to-the-pom.html#project-interpolation-and-variables

Reference http://maven.apache.org/ref/3.6.3/maven-model-builder/#Model_Interpolation

  • 使用格式

    1
    2
    3
    
    <version>${project.version}</version>
    
           ${project.version}
  • 使用前提

    • 在 Maven 的子模块继承结构中
  • 使用变量

    1
    2
    3
    
    ${project.groupId},
    ${project.version},
    ${project.build.sourceDirectory}

Maven plugins

插件用来提供额外的一个完整的功能,如: clean compiler 等 https://maven.apache.org/plugins/index.html

  • 分类

    • build

      • <build> 标签下
    • reporting

      • <reporting> 标签下

<dependencyManagement> Vs. <dependencies>

https://www.cnblogs.com/zhangmingcheng/p/10984036.html

  • <dependencyManagement> 只是对在别处声明的依赖进行集中管理

    • 注意

      • 这里不会引入新的依赖
      • 这些依赖来源

        • <parent> 父项目
        • <dependencies> 中引入
  • 优先级

    • 如果在 <dependencies> 已经规定了 <version>

      • 以 <dependencies> 规定为准
  • <version> 必不可缼

    • 如果 在<dependencies> 中没有,会到 <dependencyManagement> 中查询

      • 如果在 <dependencyManagement> 中也没有

        • 报错

创建项目

参考:

命令行创建

  • 命令

    1
    2
    3
    4
    5
    
    mvn archetype:generate \
            -DgroupId={project-packaging} \
            -DartifactId={project-name} \
            -DarchetypeArtifactId={maven-template} \
            -DinteractiveMode=false
    • 验证,win10 下 powershell 有效
  • 修改 pom.xml

    1
    2
    3
    4
    
    <properties>
      <maven.compiler.source>1.8</maven.compiler.source>
      <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    • 添加上述代码

例子:

1
2
3
4
5
mvn archetype:generate\
    -DgroupId=cn.sz\
    -DartifactId=demo01 \
    -DarchetypeArtifactId=maven-archetype-quickstart\
    -DinteractiveMode=false

命令行运行

使用 exec:java

  • 参考

  • 配置 pom.xml

    • 使用 plugin

       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      
      <build>
        <plugins>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <executions>
              <execution>
                <goals>
                  <goal>java</goal>
                </goals>
              </execution>
            </executions>
            <configuration>
              <mainClass>cn.sz.cmd.LsInfo</mainClass>
            </configuration>
          </plugin>
        </plugins>
      </build>
  • 执行命令

    1
    2
    
    mvn exec:java '-Dexec.mainClass="cn.sz.cmd.LsInfo"'
    mvn exec:java '-Dexec.mainClass=cn.sz.cmd.LsInfo'
    • powershell, cmd 验证有效

      • 上述两条都可以

代理 proxy

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?xml version="1.0" encoding="utf-8"?>
<proxies>
  <proxy>
    <!--id 代理的名称(随便设)-->
    <id>clash-http</id>
    <!--true 表示生效-->
    <active>false</active>
    <!--协议-->
    <protocol>http</protocol>
    <!--本机上网用户名及密码,如果没有,请注释或者是删除-->
    <!-- <username>*******.ex</username> -->
    <!-- <password>a*169646</password> -->
    <!--公司上网使用ip及端口,即代理,这里替换成相对应的ip和端口-->
    <host>127.0.0.1</host>
    <port>7890</port>
    <!--填写不用代理的地址,以竖线|分割多个地址,一般填写本地Maven仓库地址-->
    <!-- <nonProxyHosts>local.net|some.host.com</nonProxyHosts> -->
  </proxy>
  <proxy>
    <id>clash-https</id>
    <active>false</active>
    <protocol>https</protocol>
    <host>127.0.0.1</host>
    <port>7890</port>
  </proxy>
  <proxy>

    <id>clash-socket5</id>
    <active>true</active>
    <protocol>socks5</protocol>
    <host>127.0.0.1</host>
    <port>7891</port>
  </proxy>

</proxies>