使用 IntelliJ IDEA 2016和Maven創建Java Web項目的詳細步驟及相關問題解決辦法

工具使用


Maven簡介

相對于傳統的項目,Maven 下管理和構建的項目真的非常好用和簡單,所以這里也強調下,盡量使用此類工具進行項目構建, 它可以管理項目的整個生命周期。

可以通過其命令做所有相關的工作,其常用命令如下:

- mvn compile
- mvn test
- mvn clean
- mvn package
- mvn install            //把新創建的jar包安裝到倉庫中
- mvn archetype:generate //創建新項目
中央工廠URL:http://search.maven.org/    http://repo.maven.apache.org/maven2

本文主要演示在IntelliJ IDEA IDE環境下如何配置和使用。

配置Maven

首先到Maven官網下載 apache-maven-3.3.9-bin.tar.gz 然后將其解壓到相關目錄;

解壓后的文件目錄:

1483370808401.png

找到/conf/setting.xml文件,用文本編輯器打開,添加如下信息:

  <localRepository>此處填的是 Repository 所在的目錄,(不填也可以)</localRepository>

由于Maven遠程倉庫在國外,所以在下載相關jar包時非常慢,將其配置為Alibaba的倉庫,需添加下面代碼;

    <mirrors>
      <!-- mirror
       | Specifies a repository mirror site to use instead of a given repository. The repository that
       | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
       | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
       |
      <mirror>
        <id>mirrorId</id>
        <mirrorOf>repositoryId</mirrorOf>
        <name>Human Readable Name for this Mirror.</name>
        <url>http://my.repository.com/repo/path</url>
      </mirror>
       -->
    
      <mirror>
        <id>alimaven</id>
        <name>aliyun maven</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        <mirrorOf>central</mirrorOf>
      </mirror>
    
      <mirror> 
        <id>uk</id>  
        <mirrorOf>central</mirrorOf>  
        <name>Human Readable Name for this Mirror.</name>  
        <url>http://uk.maven.org/maven2/</url> 
      </mirror>
    
       <mirror>
        <id>CN</id>
        <name>OSChina Central</name>
        <url>http://maven.oschina.net/content/groups/public/</url>
        <mirrorOf>central</mirrorOf>
      </mirror>
    
      <mirror>
        <id>nexus</id>
        <name>internal nexus repository</name>
        <!-- <url>http://192.168.1.100:8081/nexus/content/groups/public/</url>-->
        <url>http://repo.maven.apache.org/maven2</url>
        <mirrorOf>central</mirrorOf>
      </mirror>
    
    </mirrors>

配置 IDEA默認設置

此處解決了『maven骨架生成項目速度慢的令人發指,都在Generating project in Batch mode等待,Idea狀態顯示欄還在不行runing,并沒有卡死。查看debug信息發現,是maven獲取archetype-catalog.xml導致。』的問題;

1483371214740.png
1483371272405.png

新建項目

注意:要選maven-archetype-webapp,而不是cocoon-22-archetype-webapp

1483371552572.png

GroupID是項目組織唯一的標識符,實際對應JAVA的包的結構,是main目錄里java的目錄結構。

ArtifactID就是項目的唯一的標識符,實際對應項目的名稱,就是項目根目錄的名稱。一般GroupID就是填com.leafive.test這樣子。

1483371908932.png

next

1483372123744.png

next

1483372172924.png

配置成功會提示如下信息

1483372220170.png
1483372242091.png

解決 IDEA 2016 用Maven構建Web應用時,所遇到的依賴包下載慢及相應問題


解決辦法:

  • 加上-DarchetypeCatalog=internal 運行參數,archetype-catalog.xml本地獲取
215636_8E5T_225373.png
  • 找到Maven解壓所在目錄的\conf\setting.xml,將下面的代碼復制到對應的標簽內;
    <mirrors>
      <!-- mirror
       | Specifies a repository mirror site to use instead of a given repository. The repository that
       | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
       | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
       |
      <mirror>
        <id>mirrorId</id>
        <mirrorOf>repositoryId</mirrorOf>
        <name>Human Readable Name for this Mirror.</name>
        <url>http://my.repository.com/repo/path</url>
      </mirror>
       -->
    
      <mirror>
        <id>alimaven</id>
        <name>aliyun maven</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        <mirrorOf>central</mirrorOf>
      </mirror>
    
      <mirror> 
        <id>uk</id>  
        <mirrorOf>central</mirrorOf>  
        <name>Human Readable Name for this Mirror.</name>  
        <url>http://uk.maven.org/maven2/</url> 
      </mirror>
    
       <mirror>
        <id>CN</id>
        <name>OSChina Central</name>
        <url>http://maven.oschina.net/content/groups/public/</url>
        <mirrorOf>central</mirrorOf>
      </mirror>
    
      <mirror>
        <id>nexus</id>
        <name>internal nexus repository</name>
        <!-- <url>http://192.168.1.100:8081/nexus/content/groups/public/</url>-->
        <url>http://repo.maven.apache.org/maven2</url>
        <mirrorOf>central</mirrorOf>
      </mirror>
    
    </mirrors>

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容