使用 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解压所在目录的confsetting.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>



文/风腾狼(简书作者)
原文链接:http://www.jianshu.com/p/b0935094b828
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
原文地址:https://www.cnblogs.com/huaxingtianxia/p/6245813.html