Please enable Javascript to view the contents

gradle使用

 ·  ☕ 3 分鐘  ·  🐻 Cloud

build.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
 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
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'eclipse-wtp'

sourceCompatibility = 1.8		         //設定編譯階段的JDK版本
sourceSets.main.java.srcDir  'src'	     //使用src目錄放置Java程式原始碼      (gradle原預設位置是src/main/java目錄)
sourceSets.main.resources.srcDir  'src'	 //使用src目錄放置resources原始碼 (gradle原預設位置是src/main/resources目錄) (在進行cargoRedeployRemote以熱部署匯出war檔時,將可包含xml檔)
                                                                     
webAppDirName = 'WebContent'		     //使用WebContent目錄放置網頁資源
									
repositories {
	mavenCentral()					     //使用 maven central repository (中央存儲庫)
}

// In this section you declare where to find the dependencies of your project
repositories {
    // Use jcenter for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

// 設置依賴   // Gradle 7.0 -- 參考 https://tomgregory.com/gradle-implementation-vs-compile-dependencies/
// Maven 中央儲存庫網址 https://mvnrepository.com
// 依需要自行加入會用到的函式庫Library套件,格式如下 :<groupId> : <artifactId> : <version>
dependencies {
    
    // =====================================================================================
    // 以下為 Spring Boot 使用
    
    // 用途說明1: Inherit defaults from Spring Boot
    implementation("org.springframework.boot:spring-boot-starter-parent:2.4.2"){
                exclude(module: "spring-boot-starter-logging")
    }
    
    // 用途說明2: Add typical dependencies for a web application -->
    implementation("org.springframework.boot:spring-boot-starter-web:2.4.2"){
                exclude(module: "spring-boot-starter-logging")
    }
    
    // 用途說明3: Starter for using Spring Data JPA with Hibernate -->
    implementation("org.springframework.boot:spring-boot-starter-data-jpa:2.4.2") {
                exclude(module: "spring-boot-starter-logging")
    }
    
    // 用途說明4: 用途說明4: for log4j.properties -->
	//	<!-- Spring Boot日誌系統預設是使用logback -->
	//	<!-- 對於習慣了log4j的開發者,Spring Boot依然有很好的支援 -->
	//	<!-- 不過我們在引入log4j之前,需要先排除該logback jar的依賴(如上所示),再引入log4j的依賴(如下所示): -->
    implementation("org.springframework.boot:spring-boot-starter-log4j:1.3.8.RELEASE")
    
    
    // =====================================================================================
	// 以下為 hibernate 使用
    
    // hibernate-core          <!-- spring-boot-starter-data-jpa(2.4.2) 已內建 Hibernate 5.4.27 依賴,但預設未下載 -->
    implementation("org.hibernate:hibernate-core:5.4.27.Final")
    
    // hibernate-c3p0 連線池           <!-- spring-boot 已內建依賴,但預設未下載 -->
    implementation("org.hibernate:hibernate-c3p0:5.4.27.Final")
    
    // hibernate-proxool 連線池    <!-- spring-boot 已內建依賴,但預設未下載 -->
    implementation("org.hibernate:hibernate-proxool:5.4.27.Final")
    
    
    // =====================================================================================
	// 以下為 Web App 預備使用
    
    // Servlet 4.0.1   <!-- spring-boot 已內建 Servlet 4..01 依賴,但預設未下載 -->
    compileOnly("javax.servlet:javax.servlet-api:4.0.1")
    
    // jstl            <!-- spring-boot 未內建依賴 -->
    runtimeOnly("org.apache.taglibs:taglibs-standard-impl:1.2.5")
    // jstl            <!-- spring-boot 未內建依賴 -->
    runtimeOnly("org.apache.taglibs:taglibs-standard-spec:1.2.5")
    
    // websocket       <!-- spring-boot 已內建依賴,但預設未下載 -->
    compileOnly("javax.websocket:javax.websocket-api:1.1")
    
    // javax.json      <!-- spring-boot 未內建依賴 -->
    implementation("org.glassfish:javax.json:1.1.2")
    
    // com.sun.mail    <!-- spring-boot(2.3.3) 未內建依賴 -->
    implementation("com.sun.mail:javax.mail:1.6.2")
    
    
    // =====================================================================================
	// 以下為 JSR 303/349/380 Bean Validation Framework 預備使用 -->
	
	// javax.validation         <!-- spring-boot(2.3.3) 已內建依賴,但預設未下載 -->
    implementation group: 'javax.validation',        name: 'validation-api',      version: '2.0.1.Final'
    
    // org.hibernate.validator  <!-- spring-boot(2.3.3) 已內建依賴,但預設未下載 -->
    implementation group: 'org.hibernate.validator', name: 'hibernate-validator', version: '6.1.7.Final'
    
    
    // ========================================================================
	// Oracle JDBC驅動程式 ojdbc8.jar <!-- spring-boot(2.4.2) 已內建依賴19.8.0.0,但預設未下載 
	// Oracle JDBC Driver compatible with JDK8, JDK11, JDK12, JDK13, JDK14 and JDK15 -->
    implementation("com.oracle.database.jdbc:ojdbc8:19.8.0.0")
    
    // ========================================================================
	// MySQL8 JDBC驅動程式 ojdbc8.jar--> <!-- spring-boot(2.4.2) 已內建依賴8.022,但預設未下載 -->
	// MySQL Connector/J
    implementation("mysql:mysql-connector-java:8.0.22")
    
}

buildscript {
	repositories {
		jcenter()
	}
	dependencies {
		classpath 'com.bmuschko:gradle-cargo-plugin:2.2.2'
	}
}

//使用gradle-cargo-plugin熱部署(輕量級)war
//需先啟動Tomcat,並於tomcat-users.xml添加:<user password="manager-script" roles="manager-script" username="manager-script"/>
apply plugin: 'com.bmuschko.cargo'
cargo {
    containerId = 'tomcat9x'
    port = 8081
    remote {
		protocol = 'http'
		hostname = 'localhost'
		username = 'manager-script'
		password = 'manager-script'
    }
}

//Eclipse Project Facets 只有在 Dynamic Web Project 時才有功能,只是方便"快速擴充套件" 與 "控管套件版本"
import org.gradle.plugins.ide.eclipse.model.Facet
eclipse {
	wtp {
		facet {
			facet name: 'jst.web', type: Facet.FacetType.fixed
			facet name: 'jst.web', version: '3.1'

			facet name: 'jst.java', type: Facet.FacetType.fixed
			facet name: 'jst.java', version: '1.8'
			
			facet name: 'wst.jsdt.web', type: Facet.FacetType.fixed
			facet name: 'wst.jsdt.web', version: '1.0'
		}
	}
}

//在進行cargoRedeployRemote以熱部署匯出war檔時,將可包含xml檔與java原始檔
war {
    webInf{
        from('src') {
            include '**/*.*'
            into('classes/')
        }
           
    }
}

分享

cloud
作者
Cloud
蔡逼八工程師