文章目录

  • 一、Maven安装与配置
  • (1)安装Maven
  • (2)配置环境变量
  • (3)配置仓库
  • (4)配置镜像
  • (5)配置JDK
  • 二、IDEA配置Maven
  • 三、新建项目
  • 3.1 按模板创建一个web项目的maven项目
  • 3.2 不按模板创建一个web项目的maven项目
  • 3.3运行项目 —Idea中部署自己安装的Tomcat并进行
  • 四、Idea中配置Tomcat以及运行maven项目(另一篇,可以不看)
  • 五、IDEA修改Maven默认配置不生效
  • 六、IDEA热部署,html,css,javaScript自动编译
  • 七、文章来源


一、Maven安装与配置

(1)安装Maven

官方下载,下载页面:http://maven.apache.org/download.cgi(这边选择apache-maven-3.8.6-bin.zip,点击下载

idea maven默认安装目录 idea maven安装及配置教程_intellij-idea

2.下载完成后,解压到某一路径下。本文以D:\IDEA\maven为例,实际配置环境变量时以自己安装的路径为准。路径切记不要有中文,特殊字符

idea maven默认安装目录 idea maven安装及配置教程_intellij-idea_02

(2)配置环境变量

maven 的使用是在jdk的基础上,所以电脑必须有jdk 1.右击“我的电脑”–>“属性”–>“高级系统设置”–>“高级”–>“环境变量”,在打开的窗口中选择 “系统变量” 下的 “新建” 按钮创建环境变量。

变量名:MAVEN_HOME

变量值:D:\Maven\apache-maven-3.8.6(文件路径)

idea maven默认安装目录 idea maven安装及配置教程_idea maven默认安装目录_03


2.在path环境变量中添加:%MAVEN_HOME%\bin

idea maven默认安装目录 idea maven安装及配置教程_intellij-idea_04


双击之后点击新建

idea maven默认安装目录 idea maven安装及配置教程_intellij-idea_05


3.测试:按住win+R 输入cmd,进入黑窗口控制台。输入命令: mvn -v

如果出现以下maven的版本信息,则说明maven的安装与环境变量的配置均正确;

idea maven默认安装目录 idea maven安装及配置教程_sed_06

(3)配置仓库

1.在D:\IDEA\maven路径下新建maven-repository文件夹,用作maven的本地库。

idea maven默认安装目录 idea maven安装及配置教程_maven_07


2.在路径D:\IDEA\maven\apache-maven-3.8.6\conf下找到settings.xml文件

idea maven默认安装目录 idea maven安装及配置教程_intellij-idea_08


打开方式选择记事本

找到节点localRepository,在注释外添加

<localRepository>D:\IDEA\maven\maven-repository</localRepository>

idea maven默认安装目录 idea maven安装及配置教程_sed_09


在四五十行

localRepository节点用于配置本地仓库,本地仓库其实起到了一个缓存的作用,它的默认地址是 C:\Users\用户名.m2。
当我们从maven中获取jar包的时候,maven首先会在本地仓库中查找,如果本地仓库有则返回;如果没有则从远程仓库中获取包,并在本地库中保存。
此外,我们在maven项目中运行mvn install,项目将会自动打包并安装到本地仓库中。

(4)配置镜像

1、在settings.xml配置文件中找到mirrors节点
2、添加如下配置(注意要添加在和两个标签之间,其它配置同理)

<!-- 阿里云仓库 -->
<mirror>
	<id>alimaven</id>
	<mirrorOf>central</mirrorOf>
	<name>aliyun maven</name>
	<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
</mirror>

idea maven默认安装目录 idea maven安装及配置教程_maven_10

(5)配置JDK

1、在settings.xml配置文件中找到profiles节点
2、添加如下配置

<!-- java版本 --> 
<profile>
	  <id>jdk-1.8</id>
	  <activation>
		<activeByDefault>true</activeByDefault>
		<jdk>1.8</jdk>
	  </activation>

	  <properties>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
		<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
	  </properties>
</profile>

idea maven默认安装目录 idea maven安装及配置教程_idea maven默认安装目录_11


配置完成,win+R运行cmd,输入mvn help:system测试,配置成功则本地仓库(E:\Tools\Maven\maven-repository)中会出现一些文件

首次执行 mvn help:system 命令,Maven相关工具自动帮我们到Maven中央仓库下载缺省的或者Maven中央仓库更新的各种配置文件和类库(jar包)到Maven本地仓库中。
下载完各种文件后, mvn help:system 命令会打印出所有的Java系统属性和环境变量,这些信息对我们日常的编程工作很有帮助。

附录
完整的settings.xml配置文件,可以直接复制使用,只需要修改相应的路径即可

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

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->

<!--
 | This is the configuration file for Maven. It can be specified at two levels:
 |
 |  1. User Level. This settings.xml file provides configuration for a single user,
 |                 and is normally provided in ${user.home}/.m2/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -s /path/to/user/settings.xml
 |
 |  2. Global Level. This settings.xml file provides configuration for all Maven
 |                 users on a machine (assuming they're all using the same Maven
 |                 installation). It's normally provided in
 |                 ${maven.conf}/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -gs /path/to/global/settings.xml
 |
 | The sections in this sample file are intended to give you a running start at
 | getting the most out of your Maven installation. Where appropriate, the default
 | values (values used when the setting is not specified) are provided.
 |
 |-->
<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.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>
  -->
<localRepository>D:\IDEA\maven\maven-repository</localRepository>
  <!-- interactiveMode
   | This will determine whether maven prompts you when it needs input. If set to false,
   | maven will use a sensible default value, perhaps based on some other setting, for
   | the parameter in question.
   |
   | Default: true
  <interactiveMode>true</interactiveMode>
  -->

  <!-- offline
   | Determines whether maven should attempt to connect to the network when executing a build.
   | This will have an effect on artifact downloads, artifact deployment, and others.
   |
   | Default: false
  <offline>false</offline>
  -->

  <!-- pluginGroups
   | This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.
   | when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers
   | "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.
   |-->
  <pluginGroups>
    <!-- pluginGroup
     | Specifies a further group identifier to use for plugin lookup.
    <pluginGroup>com.your.plugins</pluginGroup>
    -->
  </pluginGroups>

  <!-- proxies
   | This is a list of proxies which can be used on this machine to connect to the network.
   | Unless otherwise specified (by system property or command-line switch), the first proxy
   | specification in this list marked as active will be used.
   |-->
  <proxies>
    <!-- proxy
     | Specification for one proxy, to be used in connecting to the network.
     |
    <proxy>
      <id>optional</id>
      <active>true</active>
      <protocol>http</protocol>
      <username>proxyuser</username>
      <password>proxypass</password>
      <host>proxy.host.net</host>
      <port>80</port>
      <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
    </proxy>
    -->
  </proxies>

  <!-- servers
   | This is a list of authentication profiles, keyed by the server-id used within the system.
   | Authentication profiles can be used whenever maven must make a connection to a remote server.
   |-->
  <servers>
    <!-- server
     | Specifies the authentication information to use when connecting to a particular server, identified by
     | a unique name within the system (referred to by the 'id' attribute below).
     |
     | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
     |       used together.
     |
    <server>
      <id>deploymentRepo</id>
      <username>repouser</username>
      <password>repopwd</password>
    </server>
    -->

    <!-- Another sample, using keys to authenticate.
    <server>
      <id>siteServer</id>
      <privateKey>/path/to/private/key</privateKey>
      <passphrase>optional; leave empty if not used.</passphrase>
    </server>
    -->
  </servers>

  <!-- mirrors
   | This is a list of mirrors to be used in downloading artifacts from remote repositories.
   |
   | It works like this: a POM may declare a repository to use in resolving certain artifacts.
   | However, this repository may have problems with heavy traffic at times, so people have mirrored
   | it to several places.
   |
   | That repository definition will have a unique id, so we can create a mirror reference for that
   | repository, to be used as an alternate download site. The mirror site will be the preferred
   | server for that repository.
   |-->
  <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>maven-default-http-blocker</id>
      <mirrorOf>external:http:*</mirrorOf>
      <name>Pseudo repository to mirror external repositories initially using HTTP.</name>
      <url>http://0.0.0.0/</url>
      <blocked>true</blocked>
    </mirror>
  -->
  <mirror>
     <id>nexus-aliyun</id>
     <mirrorOf>central</mirrorOf>
     <name>Nexus aliyun</name>
     <url>http://maven.aliyun.com/nexus/content/groups/public</url>
  </mirror>

  </mirrors>

  <!-- profiles
   | This is a list of profiles which can be activated in a variety of ways, and which can modify
   | the build process. Profiles provided in the settings.xml are intended to provide local machine-
   | specific paths and repository locations which allow the build to work in the local environment.
   |
   | For example, if you have an integration testing plugin - like cactus - that needs to know where
   | your Tomcat instance is installed, you can provide a variable here such that the variable is
   | dereferenced during the build process to configure the cactus plugin.
   |
   | As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles
   | section of this document (settings.xml) - will be discussed later. Another way essentially
   | relies on the detection of a system property, either matching a particular value for the property,
   | or merely testing its existence. Profiles can also be activated by JDK version prefix, where a
   | value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'.
   | Finally, the list of active profiles can be specified directly from the command line.
   |
   | NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact
   |       repositories, plugin repositories, and free-form properties to be used as configuration
   |       variables for plugins in the POM.
   |
   |-->
  <profiles>
    <!-- profile
     | Specifies a set of introductions to the build process, to be activated using one or more of the
     | mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>
     | or the command line, profiles have to have an ID that is unique.
     |
     | An encouraged best practice for profile identification is to use a consistent naming convention
     | for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
     | This will make it more intuitive to understand what the set of introduced profiles is attempting
     | to accomplish, particularly when you only have a list of profile id's for debug.
     |
     | This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.
    <profile>
      <id>jdk-1.4</id>

      <activation>
        <jdk>1.4</jdk>
      </activation>

      <repositories>
        <repository>
          <id>jdk14</id>
          <name>Repository for JDK 1.4 builds</name>
          <url>http://www.myhost.com/maven/jdk14</url>
          <layout>default</layout>
          <snapshotPolicy>always</snapshotPolicy>
        </repository>
      </repositories>
    </profile>
    -->
    <profile>
	  <id>jdk-1.8</id>
	  <activation>
		<activeByDefault>true</activeByDefault>
		<jdk>1.8</jdk>
	  </activation>
	  <properties>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
		<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
	  </properties>
   </profile>
    <!--
     | Here is another profile, activated by the system property 'target-env' with a value of 'dev',
     | which provides a specific path to the Tomcat instance. To use this, your plugin configuration
     | might hypothetically look like:
     |
     | ...
     | <plugin>
     |   <groupId>org.myco.myplugins</groupId>
     |   <artifactId>myplugin</artifactId>
     |
     |   <configuration>
     |     <tomcatLocation>${tomcatPath}</tomcatLocation>
     |   </configuration>
     | </plugin>
     | ...
     |
     | NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to
     |       anything, you could just leave off the <value/> inside the activation-property.
     |
    <profile>
      <id>env-dev</id>

      <activation>
        <property>
          <name>target-env</name>
          <value>dev</value>
        </property>
      </activation>

      <properties>
        <tomcatPath>/path/to/tomcat/instance</tomcatPath>
      </properties>
    </profile>
    -->
  </profiles>

  <!-- activeProfiles
   | List of profiles that are active for all builds.
   |
  <activeProfiles>
    <activeProfile>alwaysActiveProfile</activeProfile>
    <activeProfile>anotherAlwaysActiveProfile</activeProfile>
  </activeProfiles>
  -->
</settings>

二、IDEA配置Maven

直接 快捷键 Ctrl+Alt+S 直接进入设置

1、打开IDEA–>File–>Settings

idea maven默认安装目录 idea maven安装及配置教程_idea maven默认安装目录_12


2、在搜索框中输入maven,查找与Maven相关的配置,然后点击Maven

idea maven默认安装目录 idea maven安装及配置教程_idea maven默认安装目录_13


3、修改相应的配置(使用本地Maven)

idea maven默认安装目录 idea maven安装及配置教程_intellij-idea_14


maven home path: 填写自己下载好的maven的位置(我的是放到了D盘)

user setting file :是用来配置去哪下载依赖的jar包的settings.xml文件

Local repository: 是下载的jar包存放的本地仓库路径

三、新建项目

3.1 按模板创建一个web项目的maven项目

idea maven默认安装目录 idea maven安装及配置教程_java_15


idea maven默认安装目录 idea maven安装及配置教程_java_16


idea maven默认安装目录 idea maven安装及配置教程_maven_17


上述选的是开发web项目的模板,开发不同的项目可以选的模板百度可以搜一下

点击create就创建了一个web项目的maven模板,当然形成maven骨架需要等待一定的是时间

等待一定时间完成后此时的目录结构:

idea maven默认安装目录 idea maven安装及配置教程_sed_18


需要在main文件夹下补全两个文件夹java,resorces,怎么添加下面介绍

下是maven项目的标准目录:
src/main/java src/main/resources src/test/java src/test/resources 下面来构建目录,在main上右击New->Directory 新建java文件夹和resources文件夹

在src目录上右击New->Directory 新建test文件夹
在test目录上右击New->Directory 新建java和resources文件夹

接下来就是把新建立的文件夹进行关联了,看清楚红色的关联对象,关联错了就得重新关联,这一定不能出错,关联方法如下图。

idea maven默认安装目录 idea maven安装及配置教程_sed_19


src/main/java 关联为 Sources Root

(右击java》选择mark Dirctory as》选择Sources Root)

src/main/resources 关联为Resources Root

(右击resources》选择mark Dirctory as》选择Resources Root)

src/test/java 关联为Test Sources Root

(右击test文件夹下test》选择mark Dirctory as》选择Test Sources Root)

src/test/resources 关联为 Test Resources Root

(右击test文件夹下resources》选择mark Dirctory as》选择Test Resources Root)

idea maven默认安装目录 idea maven安装及配置教程_intellij-idea_20


创建后目录结构:

idea maven默认安装目录 idea maven安装及配置教程_idea maven默认安装目录_21


这样就创建成一个maven项目

也可以右键项目,然后选择Open Module Settings打开项目配置页面更改,关联方法如下图。

idea maven默认安装目录 idea maven安装及配置教程_intellij-idea_22


原文链接


3.2 不按模板创建一个web项目的maven项目

idea maven默认安装目录 idea maven安装及配置教程_intellij-idea_23


点击create,如下

idea maven默认安装目录 idea maven安装及配置教程_sed_24


从左边可以看出,web项目差了一些相应的项目文件夹,接下来手动补全这些文件夹先右击test文件夹创建一个resources普通文件夹,然后如下

idea maven默认安装目录 idea maven安装及配置教程_sed_25


idea maven默认安装目录 idea maven安装及配置教程_sed_26


接下来可以在main下创建一个空目录webapp,作为web目录

idea maven默认安装目录 idea maven安装及配置教程_java_27


点击IDEA中的file在选项中打开project structure,给maven添加web framework

idea maven默认安装目录 idea maven安装及配置教程_sed_28


idea maven默认安装目录 idea maven安装及配置教程_idea maven默认安装目录_29


idea maven默认安装目录 idea maven安装及配置教程_sed_30


idea maven默认安装目录 idea maven安装及配置教程_intellij-idea_31


点击ok就,这一项web.xml创建好了,接下来指定webapp的位置

idea maven默认安装目录 idea maven安装及配置教程_idea maven默认安装目录_32


idea maven默认安装目录 idea maven安装及配置教程_intellij-idea_33


注意:先不要点击ok,接着去点击左边的Artifacts选项,如下图所示去为项目创建一个war方式的启动项

idea maven默认安装目录 idea maven安装及配置教程_idea maven默认安装目录_34


idea maven默认安装目录 idea maven安装及配置教程_idea maven默认安装目录_35


点击弹跳框的ok,接着点击下面的apply,ok就行了,这样做防止运行项目时可能在tomcat配置没有war方式启动项,完成了上述后接下来可以看到所有的文件夹都补全了,如下图:

idea maven默认安装目录 idea maven安装及配置教程_idea maven默认安装目录_36


这样上述步骤不用模板创建web项目的maven就完成了,当然自己还需要webapp下创建一个index.jsp文件接下来就是操作简单的运行

idea maven默认安装目录 idea maven安装及配置教程_sed_37

3.3运行项目 —Idea中部署自己安装的Tomcat并进行

Idea中有自带的Tomcat,看个人需要,想配置Tomcat的就配置,不想配置Tomcat就使用开发工具自带的Tomcat,下面的步骤:配置自己电脑上安装的tomcat

(1).直接进入Idea,点击Run——Edit Configurations…

idea maven默认安装目录 idea maven安装及配置教程_maven_38


(2)、点击左侧“+”号,找到Tomcat Server——Local(若是没有找到Tomcat Server 可以点击最后一行 34 items more)

idea maven默认安装目录 idea maven安装及配置教程_sed_39


idea maven默认安装目录 idea maven安装及配置教程_maven_40


idea maven默认安装目录 idea maven安装及配置教程_intellij-idea_41


idea maven默认安装目录 idea maven安装及配置教程_idea maven默认安装目录_42


运行成功界面

idea maven默认安装目录 idea maven安装及配置教程_maven_43


以上就是在Idea中部署maven项目了 (用了自己电脑安装的tomcat)

另一种部署方法

先安装Maven Help插件,用maven自带的tomcat来启动项目
原文链接

四、Idea中配置Tomcat以及运行maven项目(另一篇,可以不看)

前言

提示:Tomcat下载好之后直接解压到你想要安装的地方,下载安装过程比较简单就不演示了(需要的可以百度),Tomcat可以配置环境变量也可以不配置环境变量,我没有配置,直接在开发工具中配置好就行了,下面啊以Idea为例

一、使用步骤

Idea中部署Tomcat并进行

Idea中有自带的Tomcat,看个人需要,想配置Tomcat的就配置,不想配置Tomcat就使用开发工具自带的Tomcat

(1)、直接进入Idea,点击Run——Edit Configurations…

idea maven默认安装目录 idea maven安装及配置教程_maven_44


(2)、点击左侧“+”号,找到Tomcat Server——Local(若是没有找到Tomcat Server 可以点击最后一行 34 items more)

idea maven默认安装目录 idea maven安装及配置教程_java_45


(3)、点击点击configure… ,进入下面第二张图,按图二添加配置你的Tomcat路径,配置完点击apply

idea maven默认安装目录 idea maven安装及配置教程_java_46


idea maven默认安装目录 idea maven安装及配置教程_java_47


(4)、点击Deployment—> + —> Artifact… ,进入下面第二张图,按图二添加项目

idea maven默认安装目录 idea maven安装及配置教程_java_48


idea maven默认安装目录 idea maven安装及配置教程_java_49


(5)、最后点击ok就结束了

idea maven默认安装目录 idea maven安装及配置教程_sed_50


(6)、运行

idea maven默认安装目录 idea maven安装及配置教程_idea maven默认安装目录_51


(7)、运行成功界面

idea maven默认安装目录 idea maven安装及配置教程_idea maven默认安装目录_52

五、IDEA修改Maven默认配置不生效

问题描述:IDEA版本2019.2,通过File->Other Setting->Setting for New Projects修改Maven的默认配置后,新建项目,File–>Settings查看Maven的配置还是默认配置(修改没有生效)

idea maven默认安装目录 idea maven安装及配置教程_idea maven默认安装目录_53


解决方法:

1、找到C:\Users\70423.IntelliJIdea2019.2\config\options路径下的project.default.xml配置文件

idea maven默认安装目录 idea maven安装及配置教程_sed_54


2、在配置文件中添加一个component节点

<component name="MavenImportPreferences">
	<option name="generalSettings">
		<MavenGeneralSettings>
			<option name="localRepository" value="E:\Tools\Maven\maven-repository" />
			<option name="mavenHome" value="E:\Tools\Maven\apache-maven-3.6.1" />
			<option name="userSettingsFile" value="E:\Tools\Maven\apache-maven-3.6.1\conf\settings.xml" />
		</MavenGeneralSettings>
	</option>
</component>

idea maven默认安装目录 idea maven安装及配置教程_idea maven默认安装目录_55


3、新建项目,通过File–>Settings查看Maven的配置,已修改成功。

idea maven默认安装目录 idea maven安装及配置教程_idea maven默认安装目录_56


注意:Maven的版本要老于IDEA的版本,否则导入Maven工程时会报错!

详情请参考:



原文链接:

六、IDEA热部署,html,css,javaScript自动编译

idea默认是不能进行自动编译的,但是它也同样提供了自动编译的功能,只是需要我们手动设置一下。

1、Setting设置 勾选Build project automatically

idea maven默认安装目录 idea maven安装及配置教程_idea maven默认安装目录_57


2、Registry

ctrl+alt+shift+’/’

弹出框选择如下,勾选即可。

idea maven默认安装目录 idea maven安装及配置教程_maven_58


compiler.automake.allow.when.app.running选项,此选项在IntelliJ IDEA 2021.2之后的版本迁移到高级设置中。如下图所示:

idea maven默认安装目录 idea maven安装及配置教程_java_59


3、关闭重启

将idea关闭,重新启动后,打开工程,如果项目里面有编译不通过的,idea会默认以红色波浪线自动提示。现在在修改js,html,css就不用重启工程啦!