Gradle Learning to build Java and Web projects

Source: Internet
Author: User
Tags maven central

I. Building a Java project using Gradle Java plug-in

1) Gradle plugin contains several interface definitions and existing task items, syntax structure: Apply plugin: ' plugin name ', here we define plugins

Apply plugin: ' java '

2) Gradle wants our Java project to adhere to the following specifications:

Src/main/java: placing Java source files
Src/test/java: Placing test files, such as unit tests, etc.
Src/main/resources: Files in this directory will be entered as resource files into the jar package
Src/test/resources: Placing a configuration file for testing


3) The Java plugin contains several tasks for building a project, the most common is the build task, when we run the build task, Gradle compiles, runs our test script (Class) and generates the jar file under Build/lib

4) Other common tasks:
Clean: Delete the already built directory and its files
Assemble: Compile and generate a jar or war file, and note that the test file will not run
Check: Compiling and testing the code

Two. External dependencies

1) similar to MAVEN, if you want to add dependent external jar files to your project, we have to tell Gradle where to find them.
Grammar:

1 Repositories {2 Jcenter ()3Mavenlocal ()//maven Local Warehouse4Mavencentral ()//Maven Central Warehouse5     /* 6 Specify the MAVEN remote warehouse address7      */8Maven {URL ' http://maven.aliyun.com/nexus/content/groups/public/' }9Maven {URL "http://repo.spring.io/snapshot" }TenMaven {URL "Http://repo.spring.io/milestone" } One}

2) When a warehouse address is specified, we can add a dependency
Grammar:

dependencies{Compile group:' similar to GroupID in Maven ', Name: ' Similar to Affactid in Maven ', version: ' Release number' group: Name:version '}

Three. Java Multi-Project building

1) Gradle can also support multi-project construction, such as the following projects:

multiproject/
api/
services/webservice/
shared/
services/shared/
2) At this point we will configure the dependent modules in the Settings.gradle:

Include "Shared", "API", "Services:webservice", "services:shared"


3) For example, GKFX relies on API syntax:

Dependencies {  compile project (': Shared ')}

4) Common Properties
Sourcecompatibility: Which JDK version to use for compiling
Targetcompatibility: Generate Java version of class

Four. Building a Web Project

1) need to add Web plugin: Apply plugin: ' War '
2) War task is primarily used to package a Web application as a war
3) Consistent with MAVEN specification: Web related resources are located under Src/main/webapp

Examples of Use:

War {    //  adds a file-setto the root of the archive    //  adds a file-set to The Web-inf dir.    // adds a file-set to the Web-inf/lib dir.    // adds a configuration to the Web-inf/lib dir.    // copies a file to Web-inf/web.xml}

In addition we can set Webappdirname to specify our web context path

Gradle Learning to build Java and Web projects

Related Article

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.