Gradle近些年比较流行的项目搭建工具
话不多说,直接上硬货
使用gradle搭建简版springBoot项目;
- 首先现在新版gradle安装包;下载地址如下:http://services.gradle.org/distributions/
- 直接绿色安装到本机,并记下安装路径;
- 创建一个build.gradle文件;
- 输入以下代码:
configurations.all {
exclude group: "org.slf4j", module: "slf4j-log4j12"
exclude group: "log4j", module: "log4j"
}
buildscript {
ext {
springBootVersion = '1.4.0.BUILD-SNAPSHOT'
}
repositories {
mavenCentral()
maven { url "https://repo.spring.io/plugins-snapshot" }
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
classpath "io.spring.gradle:dependency-management-plugin:0.6.0.BUILD-SNAPSHOT"
classpath "org.springframework.boot:spring-boot-gradle-plugin:1.3.3.RELEASE"
}
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'war'
war {
baseName = 'TESTBOOT'
version = '1.0.0'
}
repositories {
mavenCentral()
}
//JDK版本
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-test")
compile "ch.qos.logback:logback-classic:1.1.3"
compile "org.slf4j:log4j-over-slf4j:1.7.13"
compile fileTree(dir: './libs', include: '*.jar')
testCompile("junit:junit")
}
//添加以下配置
task wrapper(type: Wrapper) {
gradleVersion = '3.4'
}
- 手动创建项目目录(libs可为空文件夹)
- 创建必备项目测试文件;
- 引入项目到开发工具中;点击(File–>open)打开如图手动新建目录
- 点击“OK”;然后如图配置gradle路径;
- 再次点击“OK”;
- 待项目依赖加载完毕之后;键入以下代码。
package com.ant.app.controller;
import org.springframework.web.bind.annotation.*;
/**
* @author lchunlei
* @date 2018/8/3
*/
@RestController
@RequestMapping("/lcl")
public class TestController {
@RequestMapping(value = "/one/test",method = RequestMethod.GET)
public String testOne(String req){
System.out.println("--------->"+req);
return "OK";
}
}
package com.ant.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.web.SpringBootServletInitializer;
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
application.properties内容:
#工程配置环境选择(dev test pro)
spring.profiles.active=dev
application-dev.properties内容:
#开发环境端口配置
server.port=8725
logback-spring.xml内容:
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<contextName>logback</contextName>
<!--输出到控制台-->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<!-- <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>-->
<encoder>
<pattern>%d{HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<!--按天生成日志-->
<appender name="logFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<Prudent>true</Prudent>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>
/usr/antrun/antlog/%d{yyyy-MM-dd}/%d{yyyy-MM-dd}.log
</FileNamePattern>
</rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} -%msg%n
</Pattern>
</layout>
</appender>
<root level="DEBUG">
<appender-ref ref="console" />
<appender-ref ref="logFile" />
</root>
<!-- <logger name="com.pos" level="INFO" additivity="false">
<appender-ref ref="console"/>
</logger> -->
</configuration>
最后运行唯一的主函数;项目启动。
验证项目