plugins{ id 'java' //這是個java項目,由於gradle能夠構建多種語言項目 id 'org.springframework.boot' version '2.1.3.RELEASE' } apply plugin: 'io.spring.dependency-management' group 'com.study.wzy' version '1.0-SNAPSHOT' sourceCompatibility = 1.8 jar { // 打jar包的配置 baseName = 'mytest' version = '0.0.1' manifest { attributes "Manifest-Version": 1.0, 'Main-Class': 'com.study.wzy.GradletestApplication' } } repositories { //maven資源庫配置 mavenLocal() //優先使用本地庫,win下能夠跟maven共用,mac os下未找到如何配置共用 mavenCentral() } configurations { compileOnly { //僅在編譯時依賴,此處爲lombok使用,相關注解在編譯時生成getter、setter等 extendsFrom annotationProcessor } } dependencies { //依賴jar包,每一個jar包座標包含三個基本元素:group,name,version annotationProcessor 'org.projectlombok:lombok:1.18.6' compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.1.3.RELEASE' testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '2.1.3.RELEASE' testCompile group: 'junit', name: 'junit', version: '4.12' //testCompile 表示做用域 }
運行idea右邊gradle下的build,能夠生成jar包,java -jar xxx.jar 進行運行,能夠訪問:html
示例代碼:java
@Service public class TestService { public String getName(){ return "jack"; } }
@RequestMapping("/test") @RestController @SpringBootApplication public class GradletestApplication { public static void main(String[] args) { SpringApplication.run(GradletestApplication.class, args); } @Autowired private TestService testService; @RequestMapping("/mytest") public String mytest(){ return "hello " + testService.getName(); } }
rootProject.name = 'zzzgradletest' includeFlat 'zzzservicetest'
compile project(':zzzservicetest')