ºìÁªLinuxÃÅ»§
Linux°ïÖú

Gradle-ʹÓýéÉÜ

·¢²¼Ê±¼ä:2014-06-16 10:17:46À´Ô´:ºìÁª×÷Õß:velcbo
Gradle ʹÓýéÉÜ

1 ʲôÊÇGradle

¹ÙÍøwww.gradle.orgÉϽéÉÜGradleÊÇÉý¼¶°æ(evolved)µÄ×Ô¶¯»¯¹¹½¨¹¤¾ß¡£Ëü¿ÉÒÔ×Ô¶¯¹¹½¨£¬²âÊÔ£¬·¢²¼£¬²¿Êð£¬Í¬Ê±Ê¹¸ü¶àµÄÈí¼þ°ü»òÆäËûÀàÐÍÖîÈçÉú³É¾²Ì¬ÍøÕ¾£¬ÎĵµµÈÏîÄ¿×Ô¶¯»¯¡£

Gradle ½«AntµÄ¹¦ÄܺÍÉìËõÐÔÓëMavenµÄÒÀÀµ¹ÜÀí¼°Ô¼¶¨½áºÏ³ÉÒ»ÖÖ¸ü¼Ó¸ßЧµÄ·½Ê½È¥Íê³É¹¹½¨¡£Ëü²ÉÓÃÁËGroovy ÌØ¶¨ÁìÓòÓïÑÔºÍÖî¶à´´Ð·½·¨£¬ÌṩÁËÒ»ÖÖÉùÃ÷ʽµÄ·½Ê½ÓúÏÀíµÄĬÈÏÖµÀ´ÃèÊöËùÓÐÀàÐ͵Ĺ¹½¨¹¤×÷¡£GradleÕýѸËÙ³ÉΪÐí¶à¿ªÔ´ÏîÄ¿ºÍÇ°ÑØÆóÒµ¹¹½¨ÏµÍ³µÄÑ¡Ôñ£¬Í¬Ê±Ò²ÔÚÌôÕ½ÒÅÁôµÄ×Ô¶¯»¯¹¹½¨ÏîÄ¿¡£

2 ¿ªÊ¼Ê¹ÓÃGradle

×Ô¶¯»¯¹¹½¨Ò»¸öJavaÏîÄ¿

ƽ̨£ºUbuntu linux

±ê×¼µÄGradle¹¹½¨ÏîÄ¿µÄĿ¼¸ñʽÈçÏ£º

Ê×ÏȱàдHelloWorld.java³ÌÐò

> mkdir -p src/main/java/org/gradle/example/simple
> cd src/main/java/org/gradle/example/simple
> touch HelloWorld.java

³ÌÐòÈçÏ£º

package org.gradle.example.simple;

public class HelloWorld {
public static String getResult() {
return ¡°hello, world";
}
}

»Øµ½ºÍsrcͬ¼¶µÄĿ¼
> touch build.gradle
±à¼­build.gradle
apply plugin: 'java'
Ö»ÓÐÕâôһ¾ä¡£

ÔËÐÐgradleÃüÁî
> gradle build
build.gradle ÊÇĬÈÏÖ´ÐеÄgradleÎļþ¡£

ÆäÖÐðºÅ¼°Æäºó¶¯±ö¶ÌÓï¾ÍÊÇÈÎÎñ·¾¶(task path)¡£ÕâЩ¶¯±ö¶ÌÓï¾ÍÊÇÒ»¸ö¸öµÄÈÎÎñ¡£

ºÜÉñÆæµÄÊÇbuild.gradleÖÐÖ»ÓÐÒ»¾ä´úÂ룬¾ÍÍê³ÉÁËÕâô¶àµÄtask,ÄÇôÕâЩÈÎÎñ´ÓºÎ¶øÀ´ÄØ£¿
Java pluginÌí¼ÓÁËÒ»×étasksµ½ÎÒÃǵŤ³Ìµ±ÖС£

ÉÏÃæÕ¹Ê¾µÄĿ¼½á¹¹ÖÐbuild/test-resultsºÍbuild/reports ±ØÐëµÃÌí¼Ótest´úÂë²Å»áÉú³É¡£

ÔÚsrc/test/java/org/gradle/example/simpleÏÂн¨HelloWorldTest.java
´úÂëÈçÏÂ

package org.gradle.example.simple;

import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class HelloWorldTest {
@Test
public void testHelloWorld() throws Exception {
assertEquals("Hello, World", HelloWorld.result());
}
}

»Øµ½ºÍsrcͬ¼¶µÄĿ¼
±à¼­build.gradle ÈçÏ£º

apply plugin: 'java'
apply plugin: 'maven'
version = '0.0.1'

repositories {
mavenCentral()
}

dependencies {
testCompile 'junit:junit:4.11'
}

ÆäÖÐÌí¼ÓÁËmaven²å¼þ£¬
'junit:junit:4.11' ¾ÍÊÇ group: 'junit', name: 'junit', version: '4.11'

Dependencies{} ÊÇÒ»¸öconfiguration block¡£Ëü»áÔÚËùÓеÄTaskÖ´ÐÐ֮ǰÔËÐУ¬Æä×÷ÓþÍÊÇ×¼±¸ºÃÒªÓõıäÁ¿ºÍÊý¾Ý½á¹¹¡£

1£©dependenciesÓÃÓÚÉùÃ÷Ò»×éÒÀÀµ£¬ÕâЩÒÀÀµ»á±»×éºÏΪconfiguration;

2£©configuration´ú±íÁËÒ»×é¹¹¼þ¼°ÆäÒÀÀµ¡£

dependenciesµÄÓï·¨½á¹¹ÈçÏ£º

dependencies {
configurationName dependencyNotation1, dependencyNotation2, ...
}

testCompile ¾ÍÊÇconfiguration, 'junit:junit:4.11'ÔòÊÇdependencyNotation

java pluginÌí¼ÓÁË´óÁ¿µÄdependecy configurationµ½ÎÒÃǵŤ³ÌÖС£

Name

Extends

Used by tasks

Meaning

compile

-
compileJava

Compile time dependencies

runtime

compile
-
Runtime dependencies

testCompile

compile

compileTestJava

Additional dependencies for compiling tests.

testRuntime
runtime,testCompile

test

Additional dependencies for running tests only.

archives

-
uploadArchives

Artifacts(e.g.jars) produced by this project.

default

runtime

-

The default configuration used by a project dependency on this project. Contains the artifacts and dependencies required by this project at runtime.

compile
The dependencies required to compile the production source of the project.
runtime
The dependencies required by the production classes at runtime. By default, also includes the compile time dependencies.
testCompile
The dependencies required to compile the test source of the project. By default, also includes the compiled production classes and the compile time dependencies.
testRuntime
The dependencies required to run the tests. By default, also includes the compile, runtime and test compile dependencies.

Ö®ºóÎÒÃÇÔËÐÐ

> gradle build

²é¿´Éú³Ébuild/ϵÄĿ¼

> gradle test

»á·¢ÏÖÉÙÖ´ÐÐÁËÒ»²¿·ÖµÄtask£¬Õâ˵Ã÷buildÒÀÀµtestÀ´Ö´Ðй¹½¨ÈÎÎñµÄ¡£

×÷Õߣºwhy3query
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ