Gradle Tutorial Description User Guide 7th building Java Engineering----Quick Start

Source: Internet
Author: User

Website address:http://www.gradle.org/docs/2.1/userguide/tutorial_java_projects.html


A basic Java Project with a basic Java engineeringusing the Java plugin in Build.gradle:Apply plugin: ' java '
Building The project construction projectThe Gradle/samples/java/quickstart sample project is used here. The shell enters the directory. > Gradle BuildThe tasks that the command runs on are (you can use Gradle TaskName to run one of the following tasks separately):
: Compilejava
:p rocessresources
: Classes
: Jar
: Assemble
: Compiletestjava
:p rocesstestresources
: testclasses
: Test
: Check
: Build prompt up-to-date indicates that the file is not updated and is up to date
after any successful execution, the build directory is generated under the current directory, and some of the directories generated under the build directory are:Libs/jar file in the directory Src/main/java/source Code
Src/test/java/test Source Code
Src/main/resources/will is include in the JAR file
Src/test/resources/will is include in the classpath used to run the tests

Some other useful tasks:
Clean clear the Build directory
< Gradle clean
Deletes the build directory, removing all built files.
Assemble compiling jar package < gradle assemble
: Compilejava
:p rocessresources
: Classes
: Jar
: Assemble
These five tasks are called. Compiling, processing resources, generating bytecode, generating jar packages

External Dependencies External dependenciesTypically, a project references some external jar packages. Then you need to tell Gradle where to find them. in Gradle, such as a jar file, is located in a storage warehouse (repository).
Storage warehouses can be relied upon or released to make it independent (such as jar packages, Javadoc documents). This example uses the MAVEN repository.
Build.gradle: Repositories {
Mavencentral ()
}

In this example, you need to add a. Java source compile-time dependency: commons-collections
and a test source when compiling the dependency: JUnitBuild.gradle:
dependencies {
Compile group: ' Commons-collections ', Name: ' Commons-collections ', Version: ' 3.2 '
Testcompile group: ' JUnit ', Name: ' JUnit ', version: ' 4.+ '
}

Customizing the Project customization ProjectThe Java plugin adds a number of features to your project. These properties usually have default values. If they don't fit, it's easy to change these values. example, we will specify the version number for our Java project and the compilation level of our source code. in Jar-manifest, we also added some properties.
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 ' property ': ' Value '
}

publishing the jar file releases the jar packagetypically a jar package needs to specify a location. is to tell gradle this position. Examples are packaged in a local directory. can also be published to a remote location or multiple locations Build.gradle:
uploadarchives {
repositories {
Flatdir {
Dirs ' repos '//publish to flat Dir:repos
}
}
}

Creating An Eclipse project creates an eclipse projectto create an eclipse-specific descriptor file, such as a. project file, you need to add another plugin to your build file: Build.gradle:
Apply plugin: ' Eclipse '
> Gradle Eclipse
Ls-al found out. Project
Similarly, discovering Gradle Java can also be performed:
: Compilejava Up-to-date
:p rocessresources 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 engineering buildThis section uses the Gradle/samples/java/multiproject example, in which the shell enters the directory. Build Layout:
multiproject/
api/
services/webservice/
shared/
services/shared/

Defining a Multi-Project build defines a multi-engineering buildTo define a multi-project build, you need to create a setup file. The settings file is in the root directory of the source code tree. Name is Settings.gradle
Settings.gradle:
Include "Shared", "API", "Services:webservice", "services:shared"
The description contains four directories
Common Configuration Common Configurations Define some configurations that are common to all projects. Adopt A technique called configuration reflection (injection).

Build.gradle:
subprojects {
Apply plugin: ' java '
Apply plugin: ' ECLIPSE-WTP '

repositories {
Mavencentral ()
}

dependencies {
Testcompile ' junit:junit:4.11 '
}

Version = ' 1.0 '

Jar {
Manifest.attributes provider: ' Gradle '
}
}
Apply the Java plug-in to each sub-project. means that you can use the tasks and configuration properties that are described in the previous section in a subproject. such as Gradle build.
Note that the files are built in a subproject, not in the root directory. Sub-projects can have their own build.gradle build file

Dependencies between projects dependencies between projectsOne project relies on another project's jar package. example, in Api/build, declares a dependency project shared
Api/build.gradle:
dependencies {
Compile project (': Shared ')
}


Creating A distribution creating a publishing 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
A jar package that publishes API Engineering in Zip type, relies on task Spijar, and is added from directory Src/dist to (packaged in) build/libs/ The command also generates a publish directory: build/distributions

Gradle Tutorial Description User Guide 7th building Java Engineering----Quick Start

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.