Gradle
Concepts: Open source project automation build tools, based on Ant and Maven, introduce Groovy-based Domain Specific Language (DSL), which no longer manages build scripts in XML.
Tools-->groovy Console-->Open groovy view
Groovy vs. Java:
// Classes and methods default to public Full compatibility Java Grammar of public class ProjectVersion{ // Large version private int major; // Small version private int minor; ProjectVersion(int major, int minor) { this.major = major this.minor = minor } int getMajor() { // The value of the last expression will be used as the return value(Of the last method return Can not write) major } void setMajor(int major) { this.major = major }} // Semicolon is optional ProjectVersion v1=new ProjectVersion(1,1) // The compiler adds properties to itself getter/setter Method properties can be obtained directly with a dot println v1.minor ProjectVersion v2=null // == Equivalent to equals(),No NullPlinterExceptions println v2==v1
Efficient Groovy features:
//1 Optional type definitions //def version=1 //2 assert The assertion failed because version 1 above //assert version==2 //3 Brackets are optional //println version //4 Character string /*def s1='imooc' //Just strings def s2="gradle version is ${version}" //Variables can be inserted def s3='''my name is imooc''' //Can wrap lines println s1 println s2 println s3*/ //5 aggregate api //list /*def buildTools=['ant','maven'] buildTools << 'gradle' //Add Elements assert buildTools.getClass() == ArrayList //The default is ArrayList assert buildTools.size() ==3 //Assert size*/ //map /*def buildYears=['ant':2000,'maven':2004] buildYears.gradle=2009//Add Elements println buildYears.ant println buildYears['gradle'] println buildYears.getClass() == LinkedHashMap //The default is LinkedHashMap*/ //6 Closure code blocks are typically used for method passing (Not required) /* def c1={ v -> print v } def c2={ print 'hello' } def method1(Closure closure){ closure('param') //Parameterized } def method2(Closure closure){ closure() //Parameterized } method1(c1) method2(c2)*/
The simplest example (build.gradle):
//Build scripts have one by default Project Instance's apply plugin:"java" //apply Method plugin:"java"Named parameters -->Represents use java This plugin version='0.1' //variable repositories{ mavenCentral() //Closure parameterless call repositories(Warehouse)Method } dependencies{ compile 'commons-codec:commons-codec:1.6' //Closure has parameter calls dependencies(Dependency Management)Method }
TODO Application Edition:
TodoItem class:
public class TodoItem { //To-do Name private String name; //Completed private boolean hasDone; public TodoItem(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean isHasDone() { return hasDone; } public void setHasDone(boolean hasDone) { this.hasDone = hasDone; } @Override public String toString() { return name+(hasDone ? "hasDone":"need to do")+"!"; }}
App class:
import java.util.Scanner; public class App { public static void main(String[] args) { int i=0; Scanner scanner = new Scanner(System.in); while (++i>0){ System.out.println(i+". please input todo item name:"); TodoItem item=new TodoItem(scanner.nextLine()); System.out.println(item); }}}
Enter the Build column:
And then jar-packed
Direct terminal enabled with main() method
TODO--web Edition:
Right-click on the item to select Add framework support --> Check web application (Note: Click Java EE to select Java EE version) == Add Web
Write a simple index.html
apply plugin:'war'//Use the war plugin
Click on war to build and then launch Tomcat under tomcat's webapps for testing
Build a script summary:
Building blocks:
Basic concepts:
Each build contains at least one project with one or more tasks.
1. Project
A project represents a component being built (such as a jar file), and when the build starts, Gradle instantiates an org.gradle.api.Project class based on build.gradle and makes it implicitly available through the project variable.
group, name, version to confirm the unique coordinates in the warehouse.
Important methods:
1.apply (plug-in) 2.dependencies (dependencies) 3.repositories (warehouses) 4.task (tasks)
Other ways to configure properties:
ext,gradle.properties
2. Tasks
Corresponds to org.gradle.api.Task.It mainly includes task action and task dependency.Task actions define a minimum unit of work.You can define conditions that depend on other tasks, action sequences, and execution.
Custom task (build.gradle):
//closure def createDir = { path -> File dir = new File(path); if (!dir.exists()) { dir.mkdirs(); }} //Custom Task 1 task makeJavaDir() { def paths = ['src/main/java', 'src/main/resources', 'src/test/java', 'src/test/resources'] //Insert before Action List doFirst { paths.forEach(createDir); }} //Custom Task 2 task makeWebDir(){ //Task Dependency dependsOn 'makeJavaDir' def paths=['src/main/webapp','src/test/webapp'] //Insert after action list doLast { paths.forEach(createDir) }}
Enter the Build column:
Custom tasks are here in other.
Build life cycle:
Initialization (Project) --> Configuration (Dependency and Execution Diagram) --> Execution (Actions such as doFirst doLast)
Dependency Management:
Common warehouses:
mavenLocal/mavenCentral/jcenter=="The first two are local warehouses and the last two are public warehouses
Custom maven repository=="Maven private service
Writing:
repositories{ maven{ url 'Private Service Warehouse Address' }}
Stage Configuration:
Source Code Phase:
1. compile (compile phase) 2. runtime (runtime phase)
Test phase:
1. testCompile (compilation phase) 2. testRuntime (runtime phase)
Extreme dependencies on compilation are typically used.
For example: compile'group, name, version'
Resolve version conflicts:
1. View dependency reports
Modify the default resolution policy first (gradle relies on the latest version by default)
//Auto-build fails when version conflicts are found configurations.all{ resolutionStrategy{ failOnVersionConflict() }}
2. Exclude transitive dependency
compile('org.hibernate:hibernate-core:3.6.3.Final'){ exclude group:"org.slf4j",module:"slf4j-api" }
3. Force a version
configurations.all{ resolutionStrategy{ force 'org.slf4j:slf4j-api:1.7.24' }}
Multi-project building:
Release:
allprojects{ apply plugin: 'java' sourceCompatibility=1.8 //Plug-in Publishing apply plugin: 'maven-publish' publishing{ publications{ //Multiple publishing packages can be defined myPublish(MavenPublication){ //Release java from components.java }} repositories{ maven{ name "Custom Name" url "Private Service Warehouse Address" }}}}