1. 程式人生 > 其它 >Maven安裝和基本配置

Maven安裝和基本配置

1.1 下載安裝Maven

官網 https://maven.apache.org/

1.2 配置環境變數

配置如下

  • MAVEN_HOME maven的目錄
  • path:%MAVEN_HOME%\bin

安裝成功

1.3 阿里雲映象

國內使用阿里雲的映象

<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

1.5.2 建立Maven專案

1.5.3 普通專案轉換成WEB專案

將專案中的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的核心配置檔案

匯入相關依賴

<?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>