Maven与Gradle的安装与配置

官网下载Binary压缩包, 解压到指定位置, 比如D:\Maven

  • 环境变量(系统变量):

    变量
    MAVEN_HOMED:\Maven
    MAVEN_OPTS-Xms256m -Xmx1024m
  • PATH: %MAVEN_HOME%\bin

MAVEN_HOME默认是C:\Users\Username\.m2, 所以Maven仓库配置文件默认路径是C:\Users\Username\.m2\settings.xml

自定义MAVEN_HOME后, Maven仓库配置文件路径是D:\Maven\conf\settings.xml, 将以下内容写入settings.xml:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<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>E:\MavenRepos</localRepository>
 
  <mirrors>
    <mirror>
      <mirrorOf>*</mirrorOf>
      <id>AliYun-Public</id>
      <name>AliYun-Public</name>
      <url>https://maven.aliyun.com/repository/public</url>
    </mirror>
  </mirrors>
 
  <profiles>
    <profile>
      <repositories>

        <repository>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
          <id>AliYun-Google</id>
          <name>AliYun-Google</name>
          <url>https://maven.aliyun.com/repository/google</url>
        </repository>

        <repository>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
          <id>AliYun-Spring</id>
          <name>AliYun-Spring</name>
          <url>https://maven.aliyun.com/repository/spring</url>
        </repository>

        <repository>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
          <id>AliYun-Grails-Core</id>
          <name>AliYun-Grails-Core</name>
          <url>https://maven.aliyun.com/repository/grails-core</url>
        </repository>

        <repository>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
          <id>AliYun-Apache-Snapshots</id>
          <name>AliYun-Apache-Snapshots</name>
          <url>https://maven.aliyun.com/repository/apache-snapshots</url>
        </repository>

      </repositories>

      <pluginRepositories>

        <pluginRepository>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
          <id>Aliyun-Gradle-Plugin</id>
          <name>Aliyun-Gradle-Plugin</name>
          <url>https://maven.aliyun.com/repository/gradle-plugin</url>
        </pluginRepository>

        <pluginRepository>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
          <id>AliYun-Spring-Plugin</id>
          <name>AliYun-Spring-Plugin</name>
          <url>https://maven.aliyun.com/repository/spring-plugin</url>
        </pluginRepository>

      </pluginRepositories>
    </profile>
  </profiles>

</settings>

参考官方文档, 或直接从Releases页面下载需要的版本, 解压到指定位置, 比如D:\Gradle

  • 环境变量(系统变量):

    变量
    GRADLE_HOMED:\Gradle
    GRADLE_USER_HOMEE:\GradleRepos
  • PATH: %GRADLE_HOME%\bin

GRADLE_HOME默认是C:\Users\Username\.gradle, 自定义GRADLE_HOME后, 在GRADLE_HOME下创建init.gradle文件来设定镜像仓库:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
allprojects {
   repositories {
       maven {
           url "https://maven.aliyun.com/repository/public"
        }
       maven {
           url "https://maven.aliyun.com/repository/google"
        }
       maven {
           url "https://maven.aliyun.com/repository/gradle-plugin"
        }
       maven {
           url "https://maven.aliyun.com/repository/spring"
        }
       maven {
           url "https://maven.aliyun.com/repository/spring-plugin"
        }
       maven {
           url "https://maven.aliyun.com/repository/grails-core"
        }
       maven {
           url "https://maven.aliyun.com/repository/apache-snapshots"
        }
    }
}

习惯使用自己安装的Maven和Gradle而不是IDEA中捆绑的, 这就需要在All Settings中进行设置了.

值得注意的是, 新版本的IDEA在未打开项目前, All SettingsBuild, Execution, Deployment里的Gradle只有一个Gradle user home的选项.

新建一个Gradle项目, 打开项目后, 再进入设置中, 就会发现多出来一些设置, 这时可以设置当前项目的设置, 也可以设置新项目的设置.