Gradle tutorial description Chapter 1 build a Java project ---- Quick Start, gradlejava
Address: http://www.gradle.org/docs/2.1/userguide/tutorial_java_projects.html
A basic Java project A basic java project uses the java Plug-in build. gradle: apply plugin: 'java'
Building the project build project here use the gradle/samples/java/quickstart sample project. Shell to the directory.> The gradle build command runs the following tasks (you can use gradle taskName to run the following tasks separately ):
: CompileJava
: ProcessResources
: Classes
: Jar
: Assemble
: CompileTestJava
: ProcessTestResources
: TestClasses
: Test
: Check
: After the build task is run, the prompt "UP-TO-DATE" is displayed, indicating that the file is not updated and is latest.
After any successful execution, the build directory is generated under the current directory. Some directories generated under the build directory include: libs/JAR file in the directory src/main/java/source code
Src/test/java/test source code
Src/main/resources/will be include in the JAR file
Src/test/resources/will be include in the classpath used to run the tests
Other useful tasks:
Clean clear build directory
<Gradle clean
Deletes the build directory, removing all built files.
Assemble: Compile the jar package <gradle assemble
: CompileJava
: ProcessResources
: Classes
: Jar
: Assemble
These five tasks are called. Compile and process resources, generate bytecode, and generate jar packages
External dependencies a project usually references some External JAR packages. Then you need to tell Gradle where to find them. In Gradle, for example, a JAR file is located in a storage warehouse (repository ).
The repository can obtain dependencies or publish them independently (such as jar packages and javadoc documents ). This example uses the Maven repository.
Build. gradle: repositories {
MavenCentral ()
}
In this example, you need to add a dependency for. java source code compilation: commons-collections
Dependencies with a test source code: junitbuild. gradle:
Dependencies {
Compile group: 'commons-collections ', name: 'commons-collections', version: '3. 2'
TestCompile group: 'junit', name: 'junit', version: '4. +'
}
Customizing the project custom project Java Plug-In adds many features to your project. These attributes usually have default values. If they are not suitable, it is easy to change these values. In this example, we will specify the version number for our Java project and the compilation level of our source code. In JAR-manifest, some attributes are added.
Build. gradle:
// Java compile version
SourceCompatibility = 1.5
// Project version
Version = '1. 0'
Jar {
Manifest {
Attributes 'Implementation-title': 'gradle quickstart ',
'Implementation-version': Version
}
}
Add a test system property build. gradle:
Test {
SystemProperties 'properties': 'value'
}
Publishing the JAR file publish a JAR package. Generally, you must specify a location for the JAR package. It is to tell Gradle this location. In this example, package it into a local directory. You can also release build. gradle to a remote location or multiple locations:
UploadArchives {
Repositories {
FlatDir {
Dirs 'repos' // publish to flat dir: repos
}
}
}
Creating an Eclipse project to create a unique descriptor file of Eclipse, such as The. project file, you need to add another plug-in to your build file: build. gradle:
Apply plugin: 'eclipse'
> Gradle eclipse
Ls-al found that there is a. project
Similarly, it is found that gradle java can also execute:
: CompileJava UP-TO-DATE
: ProcessResources UP-TO-DATE
: Classes UP-TO-DATE
: Javadoc
The result is that the javadoc document is compiled and generated.
Summary
<span style="font-size:14px;"><span style="font-family:SimSun;font-size:14px;">apply plugin: 'java'apply plugin: 'eclipse'sourceCompatibility = 1.5version = '1.0'jar { manifest { attributes 'Implementation-Title': 'Gradle Quickstart', 'Implementation-Version': version }}repositories { mavenCentral()}dependencies { compile group: 'commons-collections', name: 'commons-collections', version: '3.2' testCompile group: 'junit', name: 'junit', version: '4.+'}test { systemProperties 'property': 'value'}uploadArchives { repositories { flatDir { dirs 'repos' } }}</span></span>
Multi-project Java build Multi-Java project build this section uses the gradle/samples/java/multiproject example, shell into this directory. Build layout:
Multiproject/
Api/
Services/webservice/
Shared/
Services/shared/
Defining a multi-project build defines a multi-project build. to define a multi-project build, you need to create a configuration file. The setting file is in the root directory of the source code tree. The name is settings. gradle.
Settings. gradle:
Include "shared", "api", "services: webservice", "services: shared"
The description contains four directories.
Common configuration Common configurations define configurations that are Common to all projects. A configuration injection technique is used.
Build. gradle:
Subprojects {
Apply plugin: 'java'
Apply plugin: 'eclipse-wtp'
Repositories {
MavenCentral ()
}
Dependencies {
TestCompile 'junit: junit: 100'
}
Version = '1. 0'
Jar {
Manifest. attributes provider: 'gradle'
}
}
Apply the java Plug-in to each sub-project. This means that you can use the task and configuration attributes described in the previous section in the subproject. For example, gradle build.
Note that all the created files are in the sub-project, rather than in the root directory. Sub-projects can have their own build. gradle build files
Dependencies between projects one project depends on the jar package of another project. In the example, the dependency project shared is declared in api/build.
Api/build. gradle:
Dependencies {
Compile project (': shared ')
}
Creating a distribution create publish task api/build. gradle:
Task dist (type: Zip ){
DependsOn spiJar
From 'src/dist'
Into ('libs '){
From spiJar. archivePath
From configurations. runtime
}
}
Artifacts {
Archives dist
}> Gradle dist
The Jar package for publishing an api project in zip mode depends on the task spiJar. The command is added from the src/dist directory to (packaged in) build/libs/. The command also generates a release directory: build/distributions