Gradle安装

 

Here’s the table of contents:

  1. Gradle下载
  2. 安装到Windows10
    1. 配置环境变量
    2. 配置环境变量
    3. 配置到IDEA
    4. build.gradle配置文件样例

Gradle下载

点击下载Gradle二进制安装包 Gradle官方网站

安装到Windows10

配置环境变量

变量名:GRADLE_HOME
变量值:解压到的目录 E:\software\gradle-7.0.1-bin\gradle-7.0.1
变量名:GRADLE_USER_HOME
变量值:自定义Gradle仓库目录或者Maven的仓库目录 C:\Users\mayc01\.m2\repository
变量名:Path
变量值:%GRADLE_HOME%\bin;

配置环境变量

  • 在Gradle安装目录下的 init.d 文件夹下,新建一个 init.gradle 文件,里面填写以下配置
    repositories中写的是获取jar包的顺序。先是本地的Maven仓库路径;接着的mavenLocal()是获取Maven本地仓库的路径,应该是和第一条一样,但是不冲突;第三条和第四条是从国内和国外的网络上仓库获取;最后的mavenCentral()是从Apache提供的中央仓库获取jar包
    allprojects {
      repositories {
          maven { url 'file:///C:/Users/mayc01/.m2/repository'}
          mavenLocal()
          maven { name "Alibaba" ; url "https://maven.aliyun.com/repository/public" }
          maven { name "Bstek" ; url "http://nexus.bsdn.org/content/groups/public/" }
          mavenCentral()
      }
      buildscript {
          repositories {
              maven { name "Alibaba" ; url 'https://maven.aliyun.com/repository/public' }
              maven { name "Bstek" ; url 'http://nexus.bsdn.org/content/groups/public/' }
              maven { name "M2" ; url 'https://plugins.gradle.org/m2/' }
          }
      }
    }
    

配置到IDEA

在IDEA的Setting里打开"Build, Execution, Deployment"-"Build Tools"-"Gradle"。
勾选 Use local Gradle distribution,在 Gradle home 中选择安装的Gradle的路径。
如果在变量和配置文件中设置了Gradle的仓库路径,在 Service directory path 中就会自动填写地址,如果想改的话可以手动修改。

build.gradle配置文件样例

plugins{
    id 'maven-publish'
    id 'java'
}
def projectName="thulac"
group 'org.thunlp'
version '1.0'
def mainClass="org.thunlp.thulac.main.Main"
def description="THULAC Tokenizer"
test {
    maxHeapSize = "1G"
}
// 指定编码
tasks.withType(JavaCompile) {
    options.encoding = "UTF-8"
}
// 打包源码
task sourceJar(type: Jar) {
    from sourceSets.main.allJava
}
//task javadoc(type: Javadoc) {
//    options.encoding "UTF-8"
//    options.charSet 'UTF-8'
//    source = sourceSets.main.java.srcDirs
//    classpath += project.files(getBootClasspath().join(File.pathSeparator))
//    options.links("http://docs.oracle.com/javase/8/docs/api/");
////    options.linksOffline "http://d.android.com/reference","${android.sdkDirectory}/docs/reference"
//    exclude '**/BuildConfig.java'
//    exclude '**/R.java'
//    failOnError = false
//}
//
//// 制作文档(Javadoc)
//task javadocJar(type: Jar, dependsOn: javadoc) {
//    classifier = 'javadoc'
//    from javadoc.destinationDir
//}
task fatJar(type: Jar) {
    manifest {
        attributes 'Implementation-Title': description,
                'Implementation-Version': project.version,
                'Main-Class': mainClass
    }
    baseName = project.name + '-all'
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    with jar
}
publishing {
    publications {
        maven(MavenPublication) {
            groupId project.group
            artifactId projectName
            version project.version
            from components.java
            artifact sourceJar {
                classifier "sources"
            }
            artifact fatJar {
                classifier "jar-with-dependencies"
            }
//            artifact javadocJar {
//                classifier "javadoc"
//            }
        }
    }
    repositories {
        mavenLocal()
    }
}
repositories {
    mavenLocal()
    jcenter()
    mavenCentral()
}
dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.+'
    testCompile group: 'net.sf.jopt-simple', name: 'jopt-simple', version: '5.+'
}
jar {
    version project.version
    manifest {
        attributes("Main-Class": mainClass)
    }
}
task release(dependsOn: 'jar') {
    copy {
        from 'build/libs'
        into 'release'
    }
    copy {
        from 'models'
        into 'release/models'
    }
}