Maven安装和基本配置

1.1 下载安装Maven

官网 https://maven.apache.org/
Maven安装和基本配置

1.2 配置环境变量

配置如下

  • MAVEN_HOME maven的目录
  • path:%MAVEN_HOME%\bin
    Maven安装和基本配置

安装成功
Maven安装和基本配置

1.3 阿里云镜像

Maven安装和基本配置

国内使用阿里云的镜像

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

1.4 本地仓库

在本地的仓库,远程仓库;
建立一个本地仓库:localRepository

<localRepository>E:\Develop\Repository</localRepository>

1.5 创建Maven项目

1.5.1 全局设置项目Maven

Maven安装和基本配置

1.5.2 创建Maven项目

Maven安装和基本配置

Maven安装和基本配置

1.5.3 普通项目转换成WEB项目

Maven安装和基本配置

将项目中的web.xml(2.5)替换成web.xml(4.0)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
</web-app>

1.5.4 pom文件

pom.xml 是Maven的核心配置文件

Maven安装和基本配置

导入相关依赖

<?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>org.example</groupId>  
  <artifactId>javaServlet</artifactId>  
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <!--引入依赖-->
  <dependencies>
    <!--servlet依赖-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.1</version>
    </dependency>
    <!--基本测试-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>
  </dependencies>
</project>

maven约定大于配置,可能遇到写的配置文件,无法被导出或者生效的问题

解决方案

<build>
  <resources>
    <resource>
      <directory>src/main/resources</directory>
      <includes>
        <include>**/*.properties</include>
        <include>**/*.xml</include>
      </includes>
      <filtering>true</filtering>
    </resource>
    <resource>
      <directory>src/main/java</directory>
      <includes>
        <include>**/*.properties</include>
        <include>**/*.xml</include>
      </includes>
      <filtering>true</filtering>
    </resource>
  </resources>
</build>
上一篇:maven的部署以及maven配置详解


下一篇:Ubuntu系列(二):给Ubuntu18.4换软件源和pip源