Maven的安装与使用

安装

1、下载,官网下载。

2、解压,存放路径中不可包含空格和中文。如:"E:\dev\workspace\maven\apache-maven-3.6.0"

3、配置本地仓库,进入 "conf/settings.xml" 中,在 settings 节下开启如下配置,该路径就是指向本地仓库的路径:

<localRepository>E:\dev\workspace\maven\repository</localRepository>

Maven 在查找 jar 时遵循什么样的顺序呢?

  1. 优先在本地仓库中查找。
  2. 如果本地仓库中找不到,则从私服查找,找到后下载到本地仓库。
  3. 如果私服中找不到,则从中央仓库查找,找到后下载带私服,最后下载到本地仓库。

为方便使用,这里提供了已包含常用 jar 包的本地仓库,点击下载。

三套生命周期

Maven 对项目构建过程分为三套相互独立的生命周期,请注意这里说的是“三套”,而且“相互独立”,这三套生命周期分别是:

  1. Clean Lifecycle:在进行真正的构建之前进行一些清理工作。
  2. Default Lifecycle:构建的核心部分,如编辑、测试、打包、部署等等。
  3. Site Lifecycle:生成项目报告、站点、发布站点。

每一个阶段都有一个对应的命令,且有相应的插件来支持命令的执行。

注:属于同一个命令周期内的命令,当执行后面的命令时,前面的命令会自动执行。

常用命令

  • complie:编译命令,作用是将 'src/main/java' 下的 java 源文件编译为 class 文件并输出到 target 下的 classes 目录下。

    ZHANGZHONGEN+zze@ZHANGZHONGEN MINGW64 /f/idea/0219/helloworld
    $ mvn compile
    [INFO] Scanning for projects...
    [INFO]
    [INFO] -------------------------< com.zze:helloworld >-------------------------
    [INFO] Building helloworld Maven Webapp 1.0-SNAPSHOT
    [INFO] --------------------------------[ war ]---------------------------------
    [INFO]
    [INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ helloworld ---
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] skip non existing resourceDirectory F:\idea\0219\helloworld\src\main\resources
    [INFO]
    [INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ helloworld ---
    [INFO] Nothing to compile - all classes are up to date
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  0.763 s
    [INFO] Finished at: 2019-02-21T15:22:51+08:00
    [INFO] ------------------------------------------------------------------------

    例:

  • clean:清除命令,执行 clean 会删除 target 目录及其目录下所有内容。

    ZHANGZHONGEN+zze@ZHANGZHONGEN MINGW64 /f/idea/0219/helloworld
    $ mvn clean
    [INFO] Scanning for projects...
    [INFO]
    [INFO] -------------------------< com.zze:helloworld >-------------------------
    [INFO] Building helloworld Maven Webapp 1.0-SNAPSHOT
    [INFO] --------------------------------[ war ]---------------------------------
    [INFO]
    [INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ helloworld ---
    [INFO] Deleting F:\idea\0219\helloworld\target
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  0.359 s
    [INFO] Finished at: 2019-02-21T15:27:50+08:00
    [INFO] ------------------------------------------------------------------------

    例:

  • test:测试命令,会执行 'src/main/java' 下的单元测试类。

    ZHANGZHONGEN+zze@ZHANGZHONGEN MINGW64 /f/idea/0219/helloworld
    $ mvn test
    [INFO] Scanning for projects...
    [INFO]
    [INFO] -------------------------< com.zze:helloworld >-------------------------
    [INFO] Building helloworld Maven Webapp 1.0-SNAPSHOT
    [INFO] --------------------------------[ war ]---------------------------------
    [INFO]
    [INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ helloworld ---
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] skip non existing resourceDirectory F:\idea\0219\helloworld\src\main\resources
    [INFO]
    [INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ helloworld ---
    [INFO] Nothing to compile - all classes are up to date
    [INFO]
    [INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ helloworld ---
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] skip non existing resourceDirectory F:\idea\0219\helloworld\src\test\resources
    [INFO]
    [INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ helloworld ---
    [INFO] Changes detected - recompiling the module!
    [INFO] Compiling 1 source file to F:\idea\0219\helloworld\target\test-classes
    [INFO]
    [INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ helloworld ---
    [INFO]
    [INFO] -------------------------------------------------------
    [INFO]  T E S T S
    [INFO] -------------------------------------------------------
    [INFO] Running com.zze.test1.DemoTest
    2
    [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.019 s - in com.zze.test1.DemoTest
    [INFO]
    [INFO] Results:
    [INFO]
    [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
    [INFO]
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  2.414 s
    [INFO] Finished at: 2019-02-21T15:36:20+08:00
    [INFO] ------------------------------------------------------------------------

    例:

  • package:打包命令,执行 package 命令对于 java 工程会打成 jar 包,对于 web 工程会打成 war 包。

    ZHANGZHONGEN+zze@ZHANGZHONGEN MINGW64 /f/idea/0219/helloworld
    $ mvn package
    [INFO] Scanning for projects...
    [INFO]
    [INFO] -------------------------< com.zze:helloworld >-------------------------
    [INFO] Building helloworld Maven Webapp 1.0-SNAPSHOT
    [INFO] --------------------------------[ war ]---------------------------------
    [INFO]
    [INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ helloworld ---
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] skip non existing resourceDirectory F:\idea\0219\helloworld\src\main\resources
    [INFO]
    [INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ helloworld ---
    [INFO] Nothing to compile - all classes are up to date
    [INFO]
    [INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ helloworld ---
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] skip non existing resourceDirectory F:\idea\0219\helloworld\src\test\resources
    [INFO]
    [INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ helloworld ---
    [INFO] Changes detected - recompiling the module!
    [INFO] Compiling 1 source file to F:\idea\0219\helloworld\target\test-classes
    [INFO]
    [INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ helloworld ---
    [INFO]
    [INFO] -------------------------------------------------------
    [INFO]  T E S T S
    [INFO] -------------------------------------------------------
    [INFO] Running com.zze.test1.DemoTest
    2
    [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.018 s - in com.zze.test1.DemoTest
    [INFO]
    [INFO] Results:
    [INFO]
    [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
    [INFO]
    [INFO]
    [INFO] --- maven-war-plugin:3.2.2:war (default-war) @ helloworld ---
    [INFO] Packaging webapp
    [INFO] Assembling webapp [helloworld] in [F:\idea\0219\helloworld\target\helloworld]
    [INFO] Processing war project
    [INFO] Copying webapp resources [F:\idea\0219\helloworld\src\main\webapp]
    [INFO] Webapp assembled in [44 msecs]
    [INFO] Building war: F:\idea\0219\helloworld\target\helloworld.war
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  2.752 s
    [INFO] Finished at: 2019-02-21T15:40:07+08:00
    [INFO] ------------------------------------------------------------------------

    例:

  • install:安装命令,执行 install 会将项目打成 jar 或 war 包发布到本地仓库。

    ZHANGZHONGEN+zze@ZHANGZHONGEN MINGW64 /f/idea/0219/helloworld
    $ mvn install
    [INFO] Scanning for projects...
    [INFO]
    [INFO] -------------------------< com.zze:helloworld >-------------------------
    [INFO] Building helloworld Maven Webapp 1.0-SNAPSHOT
    [INFO] --------------------------------[ war ]---------------------------------
    [INFO]
    [INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ helloworld ---
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] skip non existing resourceDirectory F:\idea\0219\helloworld\src\main\resources
    [INFO]
    [INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ helloworld ---
    [INFO] Nothing to compile - all classes are up to date
    [INFO]
    [INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ helloworld ---
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] skip non existing resourceDirectory F:\idea\0219\helloworld\src\test\resources
    [INFO]
    [INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ helloworld ---
    [INFO] Nothing to compile - all classes are up to date
    [INFO]
    [INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ helloworld ---
    [INFO]
    [INFO] -------------------------------------------------------
    [INFO]  T E S T S
    [INFO] -------------------------------------------------------
    [INFO] Running com.zze.test1.DemoTest
    2
    [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.021 s - in com.zze.test1.DemoTest
    [INFO]
    [INFO] Results:
    [INFO]
    [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
    [INFO]
    [INFO]
    [INFO] --- maven-war-plugin:3.2.2:war (default-war) @ helloworld ---
    [INFO] Packaging webapp
    [INFO] Assembling webapp [helloworld] in [F:\idea\0219\helloworld\target\helloworld]
    [INFO] Processing war project
    [INFO] Copying webapp resources [F:\idea\0219\helloworld\src\main\webapp]
    [INFO] Webapp assembled in [43 msecs]
    [INFO] Building war: F:\idea\0219\helloworld\target\helloworld.war
    [INFO]
    [INFO] --- maven-install-plugin:2.5.2:install (default-install) @ helloworld ---
    [INFO] Installing F:\idea\0219\helloworld\target\helloworld.war to E:\dev\workspace\maven\repository\com\zze\helloworld\1.0-SNAPSHOT\helloworld-1.0-SNAPSHOT.war
    [INFO] Installing F:\idea\0219\helloworld\pom.xml to E:\dev\workspace\maven\repository\com\zze\helloworld\1.0-SNAPSHOT\helloworld-1.0-SNAPSHOT.pom
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  2.544 s
    [INFO] Finished at: 2019-02-21T15:44:21+08:00
    [INFO] ------------------------------------------------------------------------

    例:

依赖管理

依赖的范围

  • compile:编译范围,默认值

    compile 是默认的范围;如果没有提供一个范围,那该依赖的范围就是编译范围。编译范围依赖在所有的 classpath 中可用,同时它们也会被打包。

  • provided:已提供范围

    provided 依赖只有在当 JDK 或者一个容器已提供该依赖之后才使用。例如, 如果你开发了一个 web 应用,你可能在编译 classpath 中需要可用的 Servlet API 来编译一个 Servlet,但是你不会想要在打包好的 war 中包含这个 Servlet API;这个 Servlet API JAR 由你的应用服务器或者 Servlet 容器提供。已提供范围的依赖在编译 classpath (不是运行时)可用。它们不是传递性的,也不会被打包。

  • runtime:运行时范围

    runtime 依赖在运行和测试系统的时候需要,但在编译的时候不需要。比如,你可能在编译的时候只需要 JDBC API JAR,而只有在运行的时候才需要 JDBC 驱动实现。

  • test:测试范围

    test 范围依赖在一般的编译和运行时都不需要,它们只有在测试编译和测试运行阶段可用。

  • system:系统范围

    system 范围依赖与 provided 类似,但是你必须显式的提供一个对于本地系统中JAR 文件的路径。这么做是为了允许基于本地对象编译,而这些对象是系统类库的一部分。这样的构件应该是一直可用的,Maven 也不会在仓库中去寻找它。如果你将一个依赖范围设置成系统范围,你必须同时提供一个 systemPath 元素。注意该范围是不推荐使用的(你应该一直尽量去从公共或定制的 Maven 仓库中引用依赖)。

依赖的传递

参考工程的继承与聚合,它其实就是使用依赖的传递来实现的。

排除依赖

创建工程,引入 'struts2-core' 依赖:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.zze</groupId><artifactId>A</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-core</artifactId><version>2.3.37</version></dependency></dependencies></project>

pom.xml

假如我们不想使用传递进来的 'javassist',那么我们可以通过配置将其排除:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.zze</groupId><artifactId>A</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-core</artifactId><version>2.3.37</version><exclusions><exclusion><groupId>javassist</groupId><artifactId>javassist</artifactId></exclusion></exclusions></dependency></dependencies></project>

pom.xml

路径近者优先

创建工程,引入'struts2-spring-plugin' 依赖:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.zze</groupId><artifactId>A</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-spring-plugin</artifactId><version>2.3.37</version></dependency></dependencies></project>

pom.xml

接着引入 'spring-beans' 的依赖:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.zze</groupId><artifactId>A</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-spring-plugin</artifactId><version>2.3.37</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>4.2.4.RELEASE</version></dependency></dependencies></project>

pom.xml

此时会发现 'spring-beans' 的版本为下面直接声明的版本,因为它是直接引入,相对传进进来路径更近。

第一声明者优先

创建工程,引入 'struts2-spring-plugin' 依赖:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.zze</groupId><artifactId>A</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-spring-plugin</artifactId><version>2.3.37</version></dependency></dependencies></project>

pom.xml

接着引入 'spring-context' 依赖:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.zze</groupId><artifactId>A</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-spring-plugin</artifactId><version>2.3.37</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.2.4.RELEASE</version></dependency></dependencies></project>

pom.xml

此时会发现 'spring-beans' 的版本依旧是 'struts2-spring-plugin' 传递进来的,因为 'struts2-spring-plugin' 是先声明的。

交换 'struts2-spring-plugin' 和 'spring-context' 依赖的声明顺序:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.zze</groupId><artifactId>A</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.2.4.RELEASE</version></dependency><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-spring-plugin</artifactId><version>2.3.37</version></dependency></dependencies></project>

pom.xml

此时 'spring-beans' 的版本就改为 'spring-context' 传递进来的版本了,因为 'spring-context' 是先声明的。

版本锁定

版本锁定一般在父子工程间使用,创建父工程 A,锁定 'spring-beans' 的版本:

<?xml version="1.0" encoding="UTF-8"?><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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.zze</groupId><artifactId>A</artifactId><version>1.0-SNAPSHOT</version><packaging>pom</packaging><modules><module>../B</module></modules><dependencyManagement><!--dependencyManagement 下的 dependencies 节只是用来预先锁定指定依赖的版本,并不会真的引入依赖--><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>4.2.4.RELEASE</version></dependency></dependencies></dependencyManagement></project>

pom.xml [A]

创建子工程 B ,继承父工程 A,引入 'spring-beans' 依赖:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd"><parent><artifactId>A</artifactId><groupId>com.zze</groupId><version>1.0-SNAPSHOT</version><relativePath>../A/pom.xml</relativePath></parent><modelVersion>4.0.0</modelVersion><artifactId>B</artifactId><dependencies><!--因为在父工程中已经锁定了 spring-beans 的版本,所以在子工程中不用指定版本--><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId></dependency></dependencies>
</project>

pom.xml [B]

因为在父工程中已经锁定了 'spring-beans' 的版本,所以在子工程中不指定版本会默认引用父工程锁定的版本。

版本常量使用

创建工程,创建版本常量,在依赖中引用版本常量:

<?xml version="1.0" encoding="UTF-8"?><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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.zze</groupId><artifactId>A</artifactId><version>1.0-SNAPSHOT</version><packaging>pom</packaging><properties><!--创建版本常量--><spring.version>4.2.4.RELEASE</spring.version></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><!--引用版本常量--><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency></dependencies></dependencyManagement></project>

pom.xml

工程的继承与聚合

继承

创建一个父工程 A,再创建一个子工程 B 继承 A:

<?xml version="1.0" encoding="UTF-8"?><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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.zze</groupId><artifactId>A</artifactId><version>1.0-SNAPSHOT</version><packaging>pom</packaging><modules><module>../B</module></modules><dependencies><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-core</artifactId><version>2.3.37</version></dependency></dependencies>
</project>

pom.xml [A]

 <?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd"><parent><artifactId>A</artifactId><groupId>com.zze</groupId><version>1.0-SNAPSHOT</version><relativePath>../A/pom.xml</relativePath></parent><modelVersion>4.0.0</modelVersion><packaging>jar</packaging><artifactId>B</artifactId></project>

pom.xml [B]

A 的 依赖会传递给B,此时就可以直接在子工程 B 中使用父工程 A 依赖了,这就是工程的继承。

聚合

创建工程 A,再创建工程 B 依赖工程 A:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.zze</groupId><artifactId>A</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-core</artifactId><version>2.3.37</version></dependency></dependencies>
</project>

pom.xml [A]

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.zze</groupId><artifactId>B</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>com.zze</groupId><artifactId>A</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies>
</project>

pom.xml [B]

此时 A 的依赖会传递给 B ,在工程 B 就可以直接使用工程 A 引入的依赖了,这就是工程的聚合。

IDEA中使用Maven

配置

1、快捷键 CTRL+ALT+S 打开 IDEA 设置,配置 Maven 地址:

2、在 Importing 页中勾选如图项:

3、配置 Runner 页中属性 '-DarchetypeCatalog=internal',防止未联网情况不能创建 Maven 工程。

创建工程

创建java工程

1、新建项目,选中 Maven,直接 Next:

2、输入坐标,再次 Next:

3、直接 Finish:

4、创建完成,编写代码测试:

创建web工程

1、新建项目,选中 Maven,如图选择 web 工程骨架,Next:

2、选择自己配置的 Maven 目录,Next:

3、直接 Finish:

4、输出如图则创建成功:

web项目的运行

准备

下面以运行一个 HelloWorld 程序为例:

1、新建 web 项目,在 'src/main' 下 java 创建文件夹,并标记其为 Sources Root 文件夹,执行完这个操作后 java 文件夹就相当于普通工程的 classpath 根目录了。

2、在 pom 文件中引入 Servlet 开发依赖 jar:

<dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version><scope>provided</scope>
</dependency>
<dependency><groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId><version>2.0</version><scope>provided</scope>
</dependency>

注意:因为 maven  web 工程后续运行时使用 maven 提供的 tomcat 环境,这里要设置引入 jar 的 scope 为 provided,否则会因为 jar 包重复出异常。

3、在 'src\main\java' 下创建 Servlet 如下:

package com.zze.servlet;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;public class HelloServlet extends HttpServlet {protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.getWriter().write("Hello world!!!");}
}

com.zze.servlet.HelloServlet

<servlet><servlet-name>helloServlet</servlet-name><servlet-class>com.zze.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping><servlet-name>helloServlet</servlet-name><url-pattern>/hello</url-pattern>
</servlet-mapping>

WEB-INF/web.xml

运行方式一:命令运行

1、打开 MavenProjects 窗口:

2、点击如图按钮:

3、输入 'tomcat:run' 指令,Execute:

4、此时项目就以被部署到 tomcat 并运行:

5、访问 'localhost:8080/helloworld/hello' 测试:

运行方式二:配置运行

1、进入 Edit Configurations:

2、选择 Maven:

3、输入如下,Apply:

4、此时就可直接点击该图标启动项目了:

5、还可在 Maven Projects 窗口中双击该配置启动:

运行方式三:本地Tomcat运行

1、进入 Edit Configurations:

2、选择 Tomcat 下的 Local 项:

3、选择 Deployment 栏,点击 + 号:

3、选择 Artifact:

4、选择 war exploded 结尾项:

5、OK,接下来就可以像运行普通 web 工程一样启动 maven 项目:

补充

配置国内仓库源

在 "conf/settings.xml" 文件中的 mirrors 节下选下面一个节点添加即可:

<mirror><id>alimaven</id><name>aliyun maven</name><url>http://maven.aliyun.com/nexus/content/groups/public/</url><mirrorOf>central</mirrorOf>
</mirror>

阿里云

<mirror><id>jboss-public-repository-group</id><mirrorOf>central</mirrorOf><name>JBoss Public Repository Group</name><url>http://repository.jboss.org/nexus/content/groups/public</url>
</mirror>

jboss

配置Tomcat插件

<!--使用 tomcat7:run-->
<plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><version>2.2</version><configuration><port>8080</port><path>/</path></configuration>
</plugin>

聚合工程整合SSH示例

点击下载

IDEA创建Maven工程常用骨架

下面是我习惯使用的骨架:

创建普通 java 工程:不使用骨架;

创建父工程:maven-archetype-site-simple;

创建 web 工程:maven-archetype-webapp;

转载于:https://www.cnblogs.com/zze46/p/10399663.html

IDEA中使用Maven相关推荐

  1. Maven安装与配置(最实用!!!)eclipse中配置maven

    Maven安装与配置 一.需要准备的东西 JDK Eclipse(本章主要是在eclipse中进行配置maven) Maven程序包 二.下载与安装 1. 前往maven下载最新版的Maven程序: ...

  2. Linux(CentOS6.5)中安装maven

    Linux(CentOS6.5)中安装maven 1.上传相关包(*.tar.gz等) 使用相关软件上传或用Xshell连接后下载命令:yum install lrzsz 2.安装maven 1> ...

  3. 在eclipse中搭建maven工程(第二种方法)

    第一种方法见前面的博客 用Maven创建web项目(详细步骤) maven-3.3.9 下载之后就是配置环境变量,可以去百度一哈,用MAVEN_HOME配置,我图方便,直接在用户的path配置的. 接 ...

  4. Maven学习总结(七)——eclipse中使用Maven创建Web项目

    2019独角兽企业重金招聘Python工程师标准>>> Maven学习总结(七)--eclipse中使用Maven创建Web项目 一.创建Web项目 1.1 选择建立Maven Pr ...

  5. 解决IDEA中进行maven install报:系统资源不足的问题

    一.背景 最近在idea中使用maven对公司的项目进行install的时候老是出现系统资源不足的问题导致install失败,在网上搜索也没找到很好的答案,自己不断摸索,最终在idea的配置里面找到了 ...

  6. 解决idea中执行maven命令失败的问题

    解决idea中执行maven命令失败的问题 参考文章: (1)解决idea中执行maven命令失败的问题 (2)https://www.cnblogs.com/qyf404/p/4839479.htm ...

  7. IDEA中创建maven项目后解决main文件夹下目录不全的问题

    IDEA中创建maven项目后解决main文件夹下目录不全的问题 参考文章: (1)IDEA中创建maven项目后解决main文件夹下目录不全的问题 (2)https://www.cnblogs.co ...

  8. 解决IDEA中,maven依赖不自动补全的问题

    解决IDEA中,maven依赖不自动补全的问题 参考文章: (1)解决IDEA中,maven依赖不自动补全的问题 (2)https://www.cnblogs.com/flypig666/p/1179 ...

  9. idea在Terminal中使用maven指令

    如果无法直接使用mvn指令,那么这里需要配置你idea中的maven的环境变量, 先说maven在idea中的位置,在你idea安装目录下的\plugins\maven 接下来配置环境变量:在你的用户 ...

  10. eclipse中配置Maven仓库

    一.解压下载好的Maven核心程序到非中文的文件夹中 二.配置Maven环境变量 1.配置maven之前必须有jdk的环境: 2.配置M2_HOME 新建M2_HOME变量: 配置path的值 3.配 ...

最新文章

  1. 使用批处理实现mysql数据库备份与上传
  2. OSChina 周四乱弹 —— 春天在哪里,春天在哪里?
  3. python break -else 语句
  4. Lintcode: Unique Paths
  5. 将一个BYTE数组转换成16进制字符串和10进制字符串格式
  6. LeetCode 599. 两个列表的最小索引总和(哈希map)
  7. Shiro【授权过滤器、与ehcache整合、验证码、记住我】
  8. arcgis构建金字塔失败什么原因_天猫入驻为什么失败?知舟集团给出失败原因和解决办法...
  9. Qt之利用事件过滤器在QLabel上画框
  10. NEC公司日前正式回绝了AMD的传唤请求
  11. JDK java version 1.8.0_181环境搭建
  12. python如何清屏_python如何清屏
  13. kali linux2019镜像下载,Kali 2019下载_KaLi Linux镜像文件iso下载 2019.1a x86/x64_当载软件站...
  14. [附源码]Python计算机毕业设计SSM基于Yigo平台库房管理系统(程序+LW)
  15. 2021年中式面点师(高级)报名考试及中式面点师(高级)模拟试题
  16. Swift --- 扩展(Extention)
  17. 基于android的pc系统,Phoneix OS 系统一款基于安卓打造的个人电脑系统
  18. node安装详细步骤
  19. 集成学习 Adaboost(python实现)
  20. 微信小游戏设置游戏路径以及成员添加

热门文章

  1. ssh,scp带密码操作
  2. 设计模式之- 外观模式(Facade Pattern)
  3. 关于C语言中的'\?'和%%
  4. jdk7与jdk8环境共存与切换
  5. 中高级JavaScript易错面试题
  6. Linux网络流量实时监控ifstat iftop命令详解
  7. 刚刚出炉的Asp.net网站部署视频教程
  8. mysql my.cnf在哪里_my.cnf配置文件在哪
  9. C++ 运算符优先级
  10. matlab imwrite将图像保存到其他目录