Gradle User Guide (3)-Build a Java project

Source: Internet
Author: User
Tags checkstyle

1. Building a basic Java project

In order to use the Java plugin, add the following code to the build file:

Build.gradle     ' Java '

This is all about defining a Java project. It applies the Java plug-in to the project and adds many tasks.

Gradle will look for the product code in the Src/main/java directory and find the test code in Src/test/java . In addition to the JAR file containing the resource in src/main/resources , src/test/resources contains the run test. All outputs are in the build directory, and the JAR is in the build/libs directory.

PS: Some common task

Clean

Delete the build directory and remove all the built files

Assemble

Compiles packaged code, but does not run unit tests. Other plugins give this task more features, such as if you use the war plugin, the task will build the war file for project

Check

Compile your code to test. Other plugins bring more check types to this task. For example, if you use the Checkstyle plugin, this task is built to execute in your code Checkstyle

2. Dependent external jar files

Java projects often have dependencies on external jars. In order to refer to these JAR files, it needs to be configured in Gradle. In Gradle, similar and JAR files will be placed in the repository. A repository can be obtained from a dependent project, or a copy of the project is submitted to Repository, or both. For example, we use Maven repository:

Build.gradle    Repositories {        mavencentral ()    }

We add some dependencies, declaring the dependencies required at compile time and the dependencies required for testing

build.gradle    Dependencies {        'commons-collections '  Commons-collections'3.2'junit          'junit''4.+'    } 

3. Customizing project Properties

Add properties to your project in the Java plugin. These properties are usually sufficient to use the default values at startup. If they don't suit you, you can easily change them. Let's take a look at our example. Here, we will explain the version number of our Java project, including the Java version number. We also add some properties to the JAR file list.

Build.gradle     1.5              ' 1.0 ' jar {manifest {'       Implementation-title'Gradle Quickstart',                        ' implementation-version ' : Version        }    }

4. Publish the jar file

You need to tell Gradle where to publish the JAR. In Gradle, products such as JAR files are published to the library. In our example, we publish to a local path. You can also publish to a remote location or to multiple locations.

build.gradle    uploadarchives {        repositories {           Flatdir               {'repos'            }        }    }

Execute Gradle uploadarchives to publish

5. Create an Eclipse project

Create a profile of Eclipse features, such as. Project, which requires adding plugins

Build.gradle     ' Eclipse '

Execute gradle Eclipse to produce the Eclipse project file.

Summarize

The following is the complete sample build file

Build.gradle 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'           }        }    }
6. Build multiple Java Projects

Here is a project structure built by multiple projects:

multiproject/      API/      services/webservice/      shared/      Services/ shared/

It contains 4 of project. apiis to generate a JAR file for the client to load the XML webservice provided to the Java client. webserviceis a web app that returns XML. shared api webservice The code that contains and is used. services/sharedThe project contains the dependent shared code.

Defining the Build File

The configuration file is named Settings.gradle, as follows

Settings.gradle     " shared " " API " " Services:webservice " " services:shared "
Common configuration

There are a number of common configurations. In our example, we used configuration injection (config injection). Here, the root project is like a container, and the subprojects method iterates through the elements in the container (project in the instance) and assigns the specified configuration. This way we can easily define the manifest content of all archives, and some common dependencies:

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'        }    }

Note that the Java plug-in is applied to all child project in the example. This means that the task and configuration properties will appear in even child project. So, you can run the Gradle build in the root project directory to compile, test, and package all project into jars.

Note that plug-ins are only applied to areas included in subprojects, and other root levels will not apply.

7. Dependency on external projects

In the same build, you can add dependencies between projects so that, for example, a project's JAR file can be used to compile another project. In the api build file we will add shared a dependency on the project. Because of this dependency, Gradle will ensure that it is shared api built before.

api/build.gradle    Dependencies {        Compile project (': Shared' ) )    }

8. Create a Release package

Add a release package to the client to load

api/build.gradle    Task Dist (type:zip) {        dependsOn spijar        'src/dist' into         ('libs') {from            Spijar.archivepath            Configurations.runtime        }    }    artifacts {       Archives dist    }

Gradle User Guide (3)-Build a Java project

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.