Maven与IDEA的联系与详解

学习技术之前问自己,为什么要学习这个技术?

1.在javaweb开发中,需要使用大量的jar包,我们手动去导入(Tomcat能运行起来是因为它里面有个lib目录,而lib目录下有很多jar包)。

这么多jar包我们手动进行导入的话会很难。所以这也是我们学习新技术的原因所在。 

2.如何能够让一个东西自动帮我导入和配置这个jar包。

由此,Maven诞生了。

1.1 Maven(项目架构管理工具)

我们目前用来就是方便导入jar包的!

Maven的核心思想:约定大于配置(有规则必须这么去做)

有约束,不要去违反。

Maven会规定好你该如何去编写我们的java代码,必须要按照这个规范来;

1.2下载安装Maven

(1)找到官网如下所示: Maven – Welcome to Apache Maven。我们根据下载3.8.5,之后出现了错误在后面会提到,究其原因,是3.8.5不适配于IDEA2021,Maven3.8.5版本与IDEA2021暂不兼容。通过了解,我们要进行下载:Maven3.8.1版本

IDEA 2021 兼容maven 3.8.1及之前的所用版本;IDEA 2020 兼容Maven 3.6.3及之前所有版本;IDEA 2018 兼容Maven3.6.1及之前所有版本。我们可以根据自己适配的版本进行更正。我们在这里重新下载Maven3.8.1版本。

(2)点击download,即可下载最新版本的Maven,以下为下载最新版本Maven的方式

 其中后缀为zip的为Windows系统里面的,后缀为tar.gz的为Linux系统。Source为源码,而上面的Binary为生成的项目,我们一般下载生成的项目。

 我们进行下载这个:

 我们下载完成后进行解压,如下所示:

我们解压bin目录下的文件:

如果我们要下载以前的版本,我们进行如下所示:

我们点击Previous Releases下面的 archives

 我们进去如下所示:我们点击我们所要下载的版本3.8.1.

 我们点击binaries.

我们点击

apache-maven-3.8.1-bin.zip

 之后进行解压即可:

 我们这里进行建议如下:我们可以把电脑上的所有环境都放在一个文件夹下,方便管理。

bin目录为里面的执行文件。
boot为他的启动需要的一些选项。 

 我们重点来看一些conf里面的一些配置。

 我们找到conf文件夹里面的setting.xml,打开文件后我们来进行了解。

里面settings标签里面的xmlns为头文件,xmlns命名空间。

pluginGroups标签为插件组,可以往里面放置插件

 proxies标签为配置了一些它所连接的一些端口信息

 servers为它所提供的一些服务。

mirrors为镜像的配置

profile为配置多套环境。

这些内容除了头文件基本全部注解掉了,但是我们可以从注解掉的内容来看出所配置的默认值。(例如默认的镜像的地址,默认的端口等等)

 1.3配置环境变量

点击此电脑,点击属性,点击高级系统设置,点击环境变量。

在我们的系统环境变量中,配置如下配置:

M2_HOME    maven目录下的bin目录(在我们之后学习的自动依赖的系统比如Springboot和Springcloud里面引用了M2_HOME地址。不影响现在学习,但会影响后面的学习。

MAVEN_HOME  maven的目录

以下为我们在下载高版本的Maven3.8.5所配置的过程。

 

 在系统的path中配置 %MAVEN_HOME%\bin

由于之后会遇到版本不兼容的问题,我们直接配置Maven3.8.1版本的。

在系统的path中配置 %MAVEN_HOME%\bin

 

我们将环境变量配置完毕后,在命令提示符里面输出如下信息即安装完毕。

测试Maven是否安装成功,保证必须配置完毕。

测试所用的mvn为bin目录文件夹下面的文件:

 测试如下所示:

如果在配置完还没有成功,即我们在配置过程中Path变量里面是一长串,配置之前先加一个;再进行配置。 而且我们可以配置多个Maven环境,由于版本不一样。

1.4修改配置文件(我们使用阿里云镜像)

镜像:mirrors(默认为国外的)

作用:加速我们的下载。

国内建议使用阿里云的镜像

镜像如下所示:

 我们可以将镜像进行修改:

<mirror> 
    <id>nexus-aliyun</id> 
    <name>Nexus aliyun</name> 
    <url>http://maven.aliyun.com/nexus/content/groups/public/</url> 
    <mirrorOf>*,!jeecg,!jeecg-snapshots</mirrorOf> 
  </mirror> 

我们在镜像里面将该文件粘贴至如下所示:在mirrors里面进行粘贴修改。

同时镜像可以配置多个镜像,但是一般我们只使用阿里云镜像。(可以在所修改的镜像下面直接粘贴多个镜像。)

配置后的代码如下所示:

<?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 http://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>C:\Users\Administrator\Desktop\apache-maven-3.8.1\maven-repo</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.
   |-->
镜像:方便下载使用
Maven是国外的,国内有墙,我们访问外网会非常的慢;   解决这个问题我们可以通过翻墙,VPN;
  <mirrors>
 <mirror> 
    <id>nexus-aliyun</id> 
    <name>Nexus aliyun</name> 
    <url>http://maven.aliyun.com/nexus/content/groups/public/</url> 
    <mirrorOf>*,!jeecg,!jeecg-snapshots</mirrorOf> 
  </mirror> 
    <!-- 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>
     -->
   </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>
    -->

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

 1.5本地仓库

在本地的仓库,还有一个远程仓库

建立一个本地仓库: localRepository

我们可以在setting下面进行查找:

我们在Default里面的地址在本机上面进行查找, 

我们找到界面如下所示:即为本地的仓库

 

我们将该仓库进行修改。

我们在Maven文件夹下新建文件夹,命名为maven-repo,然后进行添加到下面即可。

 修改后我们将该地址添加到localRepository,如下所示:

<!-- 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>C:\Users\Administrator\Desktop\apache-maven-3.8.5\maven-repo</localRepository>

我们重新打开新建的maven-repo(里面存放有jar包),发现有如下文件所示:

如果没有如下文件,我们可以继续根据以下步骤进行完成,完成之后重新打开该文件即可发现。

 1.6在IDEA中使用Maven

1.启动IDEA

2创建一个Maven项目

 

 首先点击左边框里面的Maven,之后检查自己的java是否配置完成(如果没有配置完成则会暴红),SDK进行选择java的安装目录,其中Create from archetype,可以进行勾选,也可以不进行勾选。勾选的目的是为了使用Maven的一些模板。

我们选中如下所示的之后,点击Next,即可完成创建一个MavenWeb项目。创建普通项目可以不用进行勾选。

所选中的有提示如下:一个简单的java应用。(A sample Java web application)

 之后Groupld为组id,Artifactld为项目名 ,Version为版本的意思,下来点击Next即可,新版的直接如下所示:

点击Next之后,我们看到如下所示:

 我们选择3.8.1进行配置,否则在之后会因为不兼容而出现错误。

其中Maven home path为Maven的地址,User setting file为用户设置文件,Local repository为本地仓库。下面的Properties为一些自动生成的信息。

  里面的Bundled(Maven 3)为IDEA自带的。

  现在的IDEA基本已经配备完毕了,所以可以直接免去上面的过程直接配备就可以啦。

 IDEA本身也有maven,但是本身所带有的Maven可以配置的东西比较少。 

3.我们创建完成,点击Finish,发现出现如下错误,即可能问题为我们所用的maven的版本太高了,(出现该错误的原因为我们初始配置的为3.8.5,之后发现该错误后,我们才重新配置了3.8.1版本的Maven。我们重新按上述步骤进行下载,进行配置的修改即可。

出现如上所示的问题的原因为:tomcat与maven版本不兼容

Maven3.8.5版本与IDEA2021暂不兼容

IDEA 2021 兼容maven 3.8.1及之前的所用版本;IDEA 2020 兼容Maven 3.6.3及之前所有版本;IDEA 2018 兼容Maven3.6.1及之前所有版本

我们在配置过程中,可以利用命令提示符进行判断。出现如下问题所示:

我们将镜像进行修改之后,我们发现出现如下所示:

 出现如下所示即为成功: 我们重新从IDEA进入我们来看待:

出现以上及证明成功创建了一个MavenWeb项目。 

如果仍然没有成功,我们可以将hosts文件进行修改:

 修改如下所示:

 导入成功后,我们等待项目初始化完毕

 我们出现BUILD SUCCESS,出现这个就说明项目搭建成功了。

4.此时我们重新进入maven3.8.1里面的maven-repo文件,如下所示:

 发现里面多出来了好多文件。

里面有org-apache的一些认识文件:

这些是初始web项目自动导入的jar包。 

5.IDEA中的Maven设置:我们点击File,点击Setting.

 以上我们可以找到在IDEA里面配置Maven,需要注意的是:经常在IDEA这个集成开发环境中会出现一个问题,就是项目自动创建完成后,尤其在Springboot或者框架里面经常常见。它这个Maven home path 会选择默认的,比如前面出现的Maven 3。我们如果发现了这个问题,我们可以手动改为本地的所下载的Maven版本。

我们可以在IDEA项目创建成功后,看一眼Maven的配置。

自动导入源码:Automatically download:可以进行勾选Source,也可以不进行勾选。

 这里为选中配置文件:(可以不用管,如果出现问题,我们再进行查看)

 这是配置环境:

 这是本地仓库的体现:

 到这里,Maven在IDEA中的配置和使用就OK了。

这个版本的Maven的创建是采用了模板进行创建的。

1.7创建一个普通的Maven项目。

我们什么也不用进行勾选,直接点击Next. 

之后的步骤和上面创建过程相同。

一个干净的Maven项目如下:

 我们可知里面有两个目录,main和test,main下面有java(放置java源代码的),resources放置一些配置文件的。test下面的java一般用于测试使用的。测试也可以在public psvm进行测试,当然不建议这么使用。代码段为初始的Maven配置文件。

我们来查看初始的使用模板创建的Maven项目:

src文件夹下的main下面的webapp里面的WEB-INF,里面的web.xml,为web的配置。index.jsp为网页。这个只会在web应用下才会有的。我们在新创建的项目里面和这个进行对比之后我们会发现并不完善。我们进行完善项目。

 我们创建命名为java。

我们创建命名为resources。

 

我们创建之后如下所示:

1.8标记文件夹功能

如果我们所创建的java没有出现蓝色,即我们所使用的IDEA的版本过低。面对这种情况,我们可以通过以下方式进行解决。

其中Test Sources Root为测试源码目录,Resources Root为资源目录,Test Resources Root为测试资源目录,过低版本会比这个多一个目录,即为:Sources Root为源码目录,我们点击此即可。

 将java选为Sources Root为源码目录,将resources选为Resources Root为资源目录。

除了上面这种方式配置之外,我们也可以采用以下方式进行配置。

我们进行点击如下Project Structure(项目结构配置)所示:

 我们点击进去之后,点击Modules(模块),我们点击我们的项目,找到项目上面的java的时候,上面的Sources点亮了,我们将Sources点灭后,即会变成普通文件夹。Project为项目,Libraries为插件库,Facets和Artifacts为导出的一些包。Problems为问题

 其中红色的字体显示的是将java编译生成class文件,在java里面是放在target目录。如果右边不显示,则可以在上面点击Excluded。

如果以下<dependencies>里面有红色爆红,我们可以将scope这一行进行删除,即会自动下载。

 1.9IDEA配置Tomcat(专业版和社区版)

我们打开软件进行点击如下所示:

 点击+进行选择,找到Tomcat即可。

 如果我们所使用的是IDEA community(IDEA 社区版),我们来进行如下所示的操作:
我们点击file--点击setting

找到Plugins,在里面进行搜索Tomcat,进行安装即可。 

 我们再次重新进入即可发现:(我们设置如下)

我们进行run即可:

 我们在安装Tomcat的时候由于端口被占用,我们进行修改了端口号,即修改为9571,而默认端口号为8080,我们启动Tomcat之后,在浏览器里面进行访问,即可发现该界面。使用专业版我们可以设置默认的浏览器,直接在IDEA里面进入浏览器界面,社区版则需要手动进行操作。

这是我们所使用的社区版,之后我们来使用专业版进行操作。我们进入社区版之后根据以上相同的操作将所创建的项目的文件夹进行完善,同时选取Maven3.8.1版本的。

我们使用同样的操作进行如上操作:之后进入该页面后,我们点击Local,选择本地的Tomcat。

 

 选中之后,我们进入下面的界面:

 Name为服务器启动名称,可以随意配置。

Open browser为默认的浏览器配置。

JRE为配置的JDK环境(Java的版本配置)。

HTTP port,Tomcat为默认的启动的端口号为8080,本机由于端口号被占用,所以修改为9571端口。

如果我们配置完毕之后,下方出现Warning,警告我们没有一个artifaces,我们必须要创建一个artifaces。我们可以直接点击警告旁边的Fix即可消除。同时我们还有一种方法进行消除。

我们进行点击如下所示:

然后点击如下所示:

 如果有maven:war,则首选war。

为什么会有这个问题:我们访问一个网站时,需要指定一个文件夹名字。

在Application context地方,可以写,也可以不写,如果不写,在端口号没有修改的时候,默认路径为localhost:8080

图中的路径为localhost:9571/rgfsuccess_Web2_exploded,这个过程叫做虚拟路径映射。

 我们点击运行之后如下所示:

 我们访问到的Hello World!就是我们默认的index.jsp中的内容:

我们点击Maven,如下所示:

 这些为Maven的命令行操作:clean为清理,validate为验证,complie为清理,test为测试,package为打包,install为安装。下面为一些插件:

插件甚至可以删掉: 

 Plugins里面的为插件,甚至可以删掉。

Dependencies为项目依赖。里面为一个个的jar包。

1.10pom文件

pom.xml是Maven的核心配置文件。

 

<?xml version="1.0" encoding="UTF-8"?>
<!--Maven版本和头文件-->
<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>
  <!--这里就是我们刚刚配置的GAV-->
  <groupId>com.rgfypl</groupId>
  <artifactId>rgfsuccess</artifactId>
  <version>1.0-SNAPSHOT</version>
  <!--项目的打包方式
  jar:java应用
  war:javaweb应用-->
  <packaging>war</packaging>

  <name>rgfsuccess Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>
  <!--配置-->
  <properties>
    <!--项目的默认构建编码-->
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
  <!--项目依赖-->
  <dependencies>
    <!--具体依赖的jar包配置文件-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
    </dependency>
  </dependencies>
  <!--项目构建用的东西-->
  <build>
    <finalName>rgfsuccess</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

这是我们所使用的模板的代码形式。下来我们看看空模板的代码形式:

<?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>untitled1</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

</project>

这是我们未加dependencies的结构,如下所示:

 

我们在这里面加一个dependencies,然后我们再看结构如下所示:

<?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>untitled1</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <!--具体依赖的jar包配置文件-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>
    </dependencies>
</project>

我们发现多了一个Dependencies。

我们可以导入多个 dependencies,如下所示:

我们进入网址Maven仓库:https://mvnrepository.com ,然后搜索spring,随便找一个,比如点击spring Web MVC

之后进入该界面:

 我们任意找寻一个版本进行点击进去之后,将dependency复制进去即可。

 我们导入之后的代码如下所示:

<?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>untitled1</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <!--具体依赖的jar包配置文件-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>
        <!--Maven的高级之处在于,他会帮你导入这个JAR包所依赖的其他JAR包-->
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.22</version>
        </dependency>

    </dependencies>
</project>

Maven由于它的约定大于配置,我们之后可能遇到我们写的配置文件,无法被导出或者生效的问题,解决方案如下所示:在build下面加个resouces.

代码设计如下所示:

 <!--假如在build中配置resources,来防止我们资源导出失败的问题-->
<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>

1.11IDEA操作:

IDEA里面最右图标所示为目录树:

点击目录树之后如下所示:

Maven中jar包的联系关联图:

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一直再追梦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值