Intellj IDEA中 Apache Maven 的环境配置

Intellj IDEA中 Apache Maven 的环境配置


前言

IDEA初始运行Maven管理的Java项目时,往往运行不起来,从远处仓库下载jar包慢等等问题。
本文介绍如何在Intellj IDEA中配置 Apache Maven环境,可解决大部分的运行的问题。


一、Maven是什么?

Maven是一个用于构建和管理Java项目的工具。它提供了一种结构化的方式来定义项目的构建过程,并自动处理依赖管理、编译、测试、打包等任务。Maven使用一个项目对象模型(Project Object Model,POM)文件来描述项目的结构和依赖关系。通过使用Maven,开发人员可以更方便地管理项目的构建和部署过程,提高开发效率。

Maven用法官方网站

IDEA中可以使用新版的Maven(需下载),也可以使用自带的Maven(版本较老):

二、Maven配置

Maven的配置,基本都在settings.xml中完成。

2.1 Maven-settings.xml

找到Maven所在的根目录,找到 conf 文件夹下的 settings.xml 文件,并用记事本打开:
Maven-settings.xml文件

2.2 本地仓库位置配置

本地仓库往往需求放置很多从远程仓库下载的项目依赖,最好给予较多空间,不建议放到C盘。在localRepository标签中填仓库所在的文件夹(文件夹地址替换成自己的即可)。
在这里插入图片描述

<localRepository>D:\program\proData\reposity</localRepository>

2.3 远程仓库镜像配置

远程仓库建议使用国内服务器,下载开源依赖包会快很多。在**<mirrors>**标签中书写。
在这里插入图片描述

  <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>aliyunmaven</id>
		<mirrorOf>*</mirrorOf>
		<name>阿里云公共仓库</name>
		<url>https://maven.aliyun.com/repository/public</url>
	</mirror>
	
  </mirrors>

2.4.JDK版本配置

在**<profiles>**标签中配置Java运行的环境,以JDK8为为例:
在这里插入图片描述

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

三、在IDEA中配置/加载Maven

  1. 配置3个路径即可,Maven所在的根目录Maven配置文件setting.xml文件本地仓库所在位置(setting.xml中配置正确的话,会自动加载出来)。点击确定,就完成 IDEA中 Apache Maven 的环境配置了。
    在这里插入图片描述

  2. 之后点击边栏Maven设置项,重新加载所有Maven项目,会依据pom.xml文件的依赖下载/在仓库中寻找所需的项目依赖。
    在这里插入图片描述

总结

以上是Intellj IDEA中 Apache Maven 的环境配置,仅是基本使用。在项目开发中,POM文件中有很多其他设置,可以根据需要配置。


附件1:setting.xml配置文件

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值