使用Gradle构建项目,继承了Ant的灵活和Maven的生命周期管理,不再使用XML作为配置文件格式,采用了DSL格式,使得脚本更加简洁。
构建环境:
- jdk1.6以上,此处使用1.8
- Gradle 4.4.1
- SpringBoot
- idea
一、下载并安装Gradle
1.下载Gradle
2.解压Gradle
下载之后解压到你想存放的目录
3.设置Gradle环境变量
- 创建一个环境变量 GRADLE_HOME,并指向你的Grdle的安装路径:
- 添加 %GRADLE_HOME%\bin 到你的PATH 环境变量:
4.检测配置
二、创建项目
1.选择Gradle -> 勾选Java -> 选择SDK(jdk1.8)
2.设置groupId和artifactid
3.选择我们本地的Gradle,并设置JVM
![创建项目3]](//upload-images.jianshu.io/upload_images/7228029-4560fdecebd1979d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
三、配置项目
1.创建项目后没自动生成src等文件夹
我们自己创建文件夹
2.配置build.gradle
allprojects {
group ‘com.chen‘
version ‘1.0-SNAPSHOT‘
repositories {
maven { url ‘http://maven.aliyun.com/nexus/content/groups/public/‘ }
}
}
buildscript {
ext {
springIOVersion = ‘1.0.0.RELEASE‘
springBootVersion = ‘1.5.9.RELEASE‘
}
repositories {
jcenter()
mavenLocal()
mavenCentral()
maven { url "http://repo.spring.io/release" }
maven { url "http://repo.spring.io/milestone" }
maven { url "http://repo.spring.io/snapshot" }
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath "io.spring.gradle:dependency-management-plugin:${springIOVersion}"
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
}
}
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
apply plugin: ‘idea‘
apply plugin: ‘java‘
apply plugin: ‘spring-boot‘
apply plugin: ‘io.spring.dependency-management‘
sourceCompatibility = 1.8
targetCompatibility = 1.8
jar {
baseName = ‘gradle-demo‘
version = ‘0.0.1‘
manifest {
attributes "Manifest-Version": 1.0,
‘Main-Class‘: ‘com.chen.GradleDemoApplication‘
}
}
repositories {
jcenter()
mavenLocal()
mavenCentral()
}
dependencyManagement {
imports {
mavenBom ‘io.spring.platform:platform-bom:Brussels-SR6‘
mavenBom ‘org.springframework.cloud:spring-cloud-dependencies:Brixton.SR4‘
}
}
ext {
junitVersion = ‘4.12‘
}
dependencies {
compile ‘org.springframework:spring-core‘
compile ‘org.springframework.boot:spring-boot-starter-web‘
compile ‘org.springframework.boot:spring-boot-autoconfigure‘
compile ‘org.springframework.boot:spring-boot-starter-tomcat‘
testCompile ‘org.springframework.boot:spring-boot-starter-test‘
testCompile "junit:junit:${junitVersion}"
}
3.创建SpringBoot启动类
@Controller
@SpringBootApplication
public class GradleDemoApplication {
public static void main(String[] args) {
SpringApplication.run(GradleDemoApplication.class, args);
}
@ResponseBody
@GetMapping("/")
public String hello() {
return "Hello World!";
}
}
四、启动项目
- 先build项目
- 启动项目,点击bootRun
五、测试项目
六、打包jar项目
七、执行jar
执行命令:java -jar build/libs/gradle-demo-0.0.1.jar
作者:CatalpaFlat
链接:https://www.jianshu.com/p/9231b1f598c5
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。