1. 程式人生 > >IDEA中如何配置Maven環境?

IDEA中如何配置Maven環境?

1、約定

  • 分類存放
    • X:/applications 目錄存放 開發工具,比如 IDEA 、Eclipse 、Typora
    • X:/Environments 目錄存放開發環境,比如 JDK、MinGW-w64、cmder
    • X:/IdeaProjects 目錄存放 IDEA 工程
    • X:/workspaces 目錄存放 Eclipse 的工作空間
    • X:/Repository 目錄用作 apache-maven 的倉庫
    • X:/Repository/Synchronization 目錄用作 apache-maven 從 中央倉庫同步時使用的本地倉庫位置 這個目錄,也是在 apache-maven 的 conf/settings.xml 中所配置的 localRepository 對應的目錄
    • X:/Repository/ThirdParty 目錄 用於存放不存在於 maven 中央倉庫的 jar 檔案 ( 比如 ojdbc.jar )

注意,這裡的 X 盤可以是硬碟上的 C盤、D盤等。

  • 編碼
    • Eclipse 所有的文字檔案採用 UTF-8 編碼 ( Text file encoding )
    • 換行方式採用 Unix 方式 ( New text file line delimiter )

2、Maven

2.1、下載

apache-maven 官網 下載 最新版 maven ,在官網的下載頁面中:

  • Binary zip archive ( apache-maven-3.6.0-bin
    .zip ) 表示已經編譯好的,可以直接使用的壓縮包
  • Source zip archive ( apache-maven-3.6.0-src.zip ) 表示 原始碼包

2.2、解壓

根據 約定,應該將 apache-maven-3.6.0-bin.zip 解壓到 X:/applications 目錄下。

2.3、配置

根據約定,在 X:/applications/apache-maven-3.6.0 目錄中找到 conf 目錄,使用 EditPlus 、 EmEditor、Notepad++ 等工具開啟 其中的 settings.xml 檔案。

2.3.1、替換

將 conf/settings.xml 檔案中的所有內容,替換為以下內容:

<?xml version="1.0" encoding="UTF-8"?>

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    
  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
   -->
  <localRepository>/path/to/local/repo</localRepository>
  
  <pluginGroups></pluginGroups>

  <proxies></proxies>

  <servers></servers>

  <mirrors>
   
  </mirrors>
 
  <profiles></profiles>

</settings>

2.3.2、修改本地倉庫

建議根據約定,將 conf/settings.xml 檔案中的本地倉庫位置修改為如下形式:

<localRepository>D:/Repository/Synchronization</localRepository>

2.3.3、修改 mirror

   <mirrors>
	<!-- 由阿里雲提供的Maven中央倉庫映象 -->
	<mirror>
		<id>nexus-aliyun</id>
		<name>Nexus aliyun</name>
		<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
		<mirrorOf>*</mirrorOf>
	</mirror> 

	<!-- 由 Apache 提供的倉庫 -->
	<mirror>
		<id>apache-repo</id>  
		<name>apache Repository</name>  
		<url>https://repository.apache.org/content/groups/public/</url> 
		<mirrorOf>apache-repo</mirrorOf> <!-- 注意,這裡必須寫 apache-repo -->
	</mirror> 
  </mirrors>

2.3.4、最後的 settings.xml

<?xml version="1.0" encoding="UTF-8"?>


<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    
  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
   -->
  <localRepository>D:/Repository/Synchronization</localRepository>
  
  <pluginGroups></pluginGroups>

  <proxies></proxies>

  <servers></servers>

  <mirrors>
	<!-- 由阿里雲提供的Maven中央倉庫映象 -->
	<mirror>
		<id>nexus-aliyun</id>
		<name>Nexus aliyun</name>
		<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
		<mirrorOf>*</mirrorOf>
	</mirror> 

	<!-- 由 Apache 提供的倉庫 -->
	<mirror>
		<id>apache-repo</id>  
		<name>apache Repository</name>  
		<url>https://repository.apache.org/content/groups/public/</url> 
		<mirrorOf>apache-repo</mirrorOf> <!-- 注意,這裡必須寫 apache-repo -->
	</mirror> 
  </mirrors>
 
  <profiles></profiles>

</settings>

3、IDEA

3.1、配置 maven

3.1.1、 開啟設定介面

在 以下介面中點選右下角 "Configure" 右側的 三角形,隨後點選 "Settings" 選項即可開啟設定介面。

3.1.2、 設定軟體主題和字型

在 "Settings for New Projects" 視窗中選擇 "Appearance & Behavior" 下面的 "Appearance"。

在右側的 "Theme" 選項中可以通過 下拉列表來切換 主題。

勾選 "Use custom font " 後,可以設定 "字型" 和 字型尺寸

3.1.3、在 IDEA 中配置 maven

在 "Settings for New Projects" 視窗中選擇 "Build , Execution , Deployment" 下方的 "Build Tools" 下方的 "Maven",如下圖所示:

在右側 "Maven home directory" 同一行最右側的 ... 按鈕上點選,並選擇到 apache-maven 的主目錄。

所謂的 apache-maven 主目錄,就是在解壓 maven 時,apache-maven 的解壓目錄。

在右側 "User settings file"同一行最右側,勾選 "Override" 複選框,再在複選框之前的 輸入框中點選 "資料夾" 圖示,選擇 apache-maven 的配置檔案( 根據約定,應該是 X:\applications\apache-maven-3.6.0\conf\settings.xml )

修改後的介面如下圖所示:

最後點選整個介面的右下角的 "Apply" 按鈕,再點選 "OK" 按鈕。

3.2、建立 maven-webapp

3.2.1、開啟 "New Project" 介面

在 歡迎介面 ( 下圖 ) 中點選 "Create New Project" 後即可彈出 "New Project" 介面。

"New Project" 介面:

3.2.2、選擇 maven 原型

3.2.2.1、選擇 maven

在 "New Project" 介面左側列表中選擇 maven :

3.2.2.2、指定 Project SDK

因為是首次使用 IDEA 開發 Java 工程(包括 maven 工程),因此這裡的 "Project SDK"是尚未配置的,因此需要配置 SDK。

在 正上方的 "Project SDK" 同一行右側點選New... 按鈕,選擇 JDK 的按照目錄即可。

選擇之後 "Project SDK" 就是相應的 JDK 。

3.2.2.3、在 archetype 列表中選擇 maven-webapp

在 "New Project" 介面中勾選 "Create from archetype" ,

然後選擇 "org.apache.maven.archetypes:maven-archetype-webapp" ,如下圖所示:

隨後點選 "Next" 按鈕,進入下一步 ( 如下圖所示 )。

在這裡,分別輸入 GroupId 、ArtifactId

  • GroupId : cn.edu.ecut
  • ArtifactId : hello

直接點選 "Next" 按鈕,進入下一步 ( 如下圖所示 )。

繼續點選 "Next",進入下一步( 如下圖所示 )

這裡,根據約定,應該將 "Project location" 更改為 X:\IdeaProjects\hello ,修改的介面後如下圖所示:

最後,點選 "Finish" 即可開始建立工程。

在工程構建過程中,IDEA軟體介面右下角可能提示:

點選 "Enable Auto-Import"即可。

隨後進入熱烈的專案構建過程中,最後在軟體介面下方看到 "BUILD SUCCESS" 即表示專案構建成功。

3.3、配置 maven-webapp

開啟 當前工程下的 pom.xml 檔案,其中:

  <groupId>cn.edu.ecut</groupId>
  <artifactId>hello</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

是當前工程最基本的資訊。<packaging> 用於指定專案的大包方式。

3.3.1、修改 properties 部分

替換 pom.xml 中 <properties> 中原來的內容為以下內容:

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <!-- 根據自己的 Project SDK 來確定版本 -->
    <maven.compiler.source>12</maven.compiler.source>
    <maven.compiler.target>12</maven.compiler.target>
    <maven.compiler.compilerVersion>12</maven.compiler.compilerVersion>
  </properties>

3.3.2、修改 dependencies 部分

替換 pom.xml 中 <dependencies> 中原來的內容為以下內容:

  <dependencies>

    <!-- 新增對 Servlet 的支援 -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.1</version>
      <scope>provided</scope>
    </dependency>

    <!-- MySQL 驅動 -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.45</version>
    </dependency>

  </dependencies>

3.3.3、修改 build 部分

替換 pom.xml 中 <build> 中原來的內容為以下內容:

  <build>
    <finalName>${project.artifactId}</finalName>
    <plugins>

      <!-- Maven Compiler Plugin -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
          <source>${maven.compiler.source}</source>
          <target>${maven.compiler.target}</target>
          <compilerVersion>${maven.compiler.compilerVersion}</compilerVersion>
          <encoding>${project.build.sourceEncoding}</encoding>
        </configuration>
      </plugin>

      <!-- Tomcat Maven Plugin -->
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <port>8080</port>
          <path>/</path>
          <uriEncoding>UTF-8</uriEncoding>
          <useBodyEncodingForURI>true</useBodyEncodingForURI>
        </configuration>
      </plugin>

    </plugins>

  </build>

3.3.4、追加 repositories 配置

在 pom.xml 檔案內部的 </project> 新增以下內容:

  <repositories>
    <repository>
      <id>nexus-aliyun</id>
      <name>Nexus aliyun</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
      <releases>
        <enabled>true</enabled>
      </releases>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
    </repository>
    <repository>
      <id>apache-repo</id>
      <name>apache Repository</name>
      <url>https://repository.apache.org/content/groups/public/</url>
      <releases>
        <enabled>true</enabled>
      </releases>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
    </repository>
  </repos