Gradle

Concept: An open source project automation build tool. On the basis of Ant and Maven, it introduces a Groovy-based domain-specific language (DSL), and no longer uses XML to manage build scripts.

Tools-->groovy Console-->Open groovy view

Groovy vs Java:

// Classes and methods are public and fully compatible with Java syntax by default 
public  class ProjectVersion{ 
 // major version private int major; 
 // minor 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 (the return of the last method can be omitted) major 
} 
 void setMajor( int major) {
 this .major = major
}
}
// Semicolon is optional 
ProjectVersion v1= new ProjectVersion(1,1 ) 
 // The compiler adds getter/setter methods to the property. The property can be obtained directly with dot notation println v1.minor 
ProjectVersion v2= null  
// == Equivalent to equals(), no NullPlinterExceptions 
println v2==v1

 

Efficient Groovy Features:

// 1 optional type definition 
 // def version=1 
 // 2 assert failed assertion because version above is 1 
 // assert version==2 
 // 3 parentheses are optional 
 // println version 
 // 4 string 
/* def s1='imooc'
//just the string def s2="gradle version is ${version}"
//You can insert variables def s3='''my name is imooc'''
//Can wrap println s1 println s2 println s3 */  
// 5 collection api 
 // list 
/* def buildTools=['ant','maven'] buildTools << 'gradle'
//Add element 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 element
println buildYears.ant
println buildYears['gradle']
println buildYears.getClass() == LinkedHashMap
//The default is LinkedHashMap */  
// 6 Closure blocks are generally used to pass parameters to methods (not required) 
/* def c1={ v -> print v }
def c2={ print 'hello' }
def method1(Closure closure){
closure('param')
//with parameters
}
def method2(Closure closure){
closure() // without parameters
}
method1(c1) method2(c2)*/
 

 

Simplest example (build.gradle):

// There is a Project instance by default in the build script
apply plugin:"java"
// apply method
plugin: "java" named parameter -- "represents the use of the java plugin version='0.1'
// variable 
repositories{
mavenCentral()
// The closure calls the repositories method without parameters 
}
dependencies{
compile 'commons-codec:commons-codec:1.6' 
 // The closure has parameters to call the dependencies (dependency management) method 
}

 

TODO app version:

TodoItem class:

public  class TodoItem { 
 // to-do item 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 section:

Then make a jar package

With the main() method, the terminal can be enabled directly

 

TODO--web version:

Right-click the project and select add framework support --> check the web application (Note: Click Java EE to select the java ee version) == "Add web

Write a simple index.html

apply plugin:'war' //Use the war plugin

Click war to build and then put it under tomcat's webapps to start tomcat for testing

 

Build script synopsis:

Building blocks:

basic concept:

Each build contains at least one project, which contains 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 method:

1.apply (plug-in) 2.dependencies (dependency) 3.repositories (warehouse) 4.task (task)

Other ways to configure properties:

ext、gradle.properties

2. Task

Corresponds to org.gradle.api.Task. It mainly includes task actions and task dependencies (dependsOn). A task action defines a minimal unit of work. You can define conditions that depend on other tasks, action sequences, and executions.

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 doLast after the action list {

paths.forEach(createDir)
}
}

 

 

Enter the build section:

 

 

Custom tasks are in other here.

 

Build Lifecycle:

Initialization (project)-->Configuration (dependency and execution graph)-->Execution (action such as: doFirst doLast)

 

Dependency management:

Common warehouses:

mavenLocal/mavenCentral/jcenter==" The first one is the local warehouse and the last two are public warehouses

Custom maven repository=="maven private server

Writing:

repositories{ maven{ url 'private server warehouse address' }}

 

Stage configuration:

Source code stage:

1.compile (compile phase) 2.runtime (runtime phase)

Test phase:

1.testCompile (compile phase) 2.testRuntime (runtime phase)

Usually compiled with extreme dependencies.

如:compile 'group、name、version'

To resolve version conflicts:

1. View the dependency report

Modify the default resolution strategy first (gradle relies on the latest version by default)

 

// Automatic build fails when a version conflict is found. configurations.all{ resolutionStrategy{ failOnVersionConflict() }}

2. Exclude transitive dependencies

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 build:

release:

allprojects{
apply plugin: 'java' sourceCompatibility=1.8 
 // Plugin publishing 
apply plugin: 'maven-publish' publishing{
publications{
// Can define multiple release packages 
myPublish(MavenPublication){ 
 // Publish java from components.java 
}
}
repositories{
maven{
name "custom name" url "private server warehouse address" 
}
}
}
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324922976&siteId=291194637