Gradle learning using the Java plugin

Source: Internet
Author: User

Download the GitHub Demo sample code for this series of articles in the following ways:

git clone https://github.com/davenkin/gradle-learning.git


Gradle's most frequently used plugin is Java plugin. Like other plugin, Java plugin is nothing special, but simply introduces multiple tasks and property to project. Of course, Java plugin also has a different place, among them is that it introduces the concept of building life cycle in the project, just like Maven. Unlike Maven, however, Gradle's project build life cycle is not a gradle built-in mechanism, but is introduced by plugin itself.




(1) main task introduced by Java plugin
Running "Gradle build", we have been able to see the main tasks introduced by Java plugin:
: Compilejava
:p rocessresources
: Classes
: Jar
: Assemble
: Compiletestjava
:p rocesstestresources
: testclasses
: Test
: Check
: Build

BUILD Successful

Total time:4.813 secs

Build is also a task introduced by Java plugin, which relies on other tasks, and other tasks depend on additional tasks, so there is a list of the above task runs. The above task run list is basically a description of the building life cycle concepts introduced by Java plugin to the project.


In addition to defining a large number of tasks, Java plugin adds some additional property to project. For example, sourcecompatibility is used to specify the Java version number that is used when compiling a Java source file, and Archivesbasename is used to specify the file name when packaged into a jar file.


(2) folder structure for Java projects
By default, Gradle uses the same Java project folder structure as Maven:






For the MAVEN standard folder structure, please refer to the MAVEN website. Of course, like Maven, the above is just the default folder structure, and we can change the folder structure by configuration.


(3) Configuring the existing source set
Gradle in the same time using the MAVEN folder structure, it also incorporates some of its own concepts, the source set. For the folder structure in, Gradle actually created 2 source set for us, one named Main, and one named Test.
Note that the name of the source set here is not necessarily associated with the main folder in the folder structure, just by default, gradle the same name for the mapping of the source set concept to the file system folder structure. The same is true for test. We were able to configure the corresponding folder structure of these source sets again in the Build.gradle file, and at the same time we were able to create a new source set.


Essentially, each source set of Gradle includes a name, and includes a property called Java and a property called resources, which are used to represent the source Set includes a collection of Java source files and resource files. In practical applications, we are able to set them to whatever folder value. For example, we can set the folder structure of main again:


sourcesets {
Main {
java {
Srcdir ' Java-sources '
}
Resources {
Srcdir ' Resources '
}
}
}

The corresponding project folder structure is as follows:






Once again we set the folder structure of main, and for test, we keep the Gradle default folder structure.


(4) Create a new source set
To create a new source set is also very easy, for example, we can create a source set called the API to hold the interface classes in the program:

Sourcesets {API}

Of course, the above configuration can also be put together with main. By default, the Java source file folder for the API is Gradle set to ${path-to-project}/src/api/java, and the Resource Files folder is set to ${path-to-project}/src/api/ Resources We can also configure the folder structure of the API once again, as in main above.


Gradle will create the corresponding task for each newly created source set on its own initiative, creating the rule that for source Set,gradle named Mysourceset, it will be created compile<mysourceset> Java, Process<mysourceset>resources, and <mysourceset>classes are 3 of these tasks. For this API, Gradle will create a Task named Compileapijava, Processapiresource, and apiclasses for it. We are able to run "Gradle apiclasses" on the command line.


As you may notice, for Main, Gradle does not have a corresponding Compilemainjava, because main is the source set created by default Gradle, and it is also its important source set, Gradle omitted the "main", but used the Compilejava as the compiler task of main. For test, Gradle still uses Compiletestjava.


Typically, the source set that we create ourselves with the name API is dependent on the other source set, for example, the class in main needs to implement an interface in the API, and so on. At this point we need to do two things. First, we need to compile the API before compiling main, that is, the task that compiles the Java source file in main should depend on the task in the API:

Classes.dependson apiclasses

Second, when compiling main, we need to place the class file generated by the API compilation under the Classpath of Main. At this point, we can configure the following for main and test:

sourcesets {
Main {
Compileclasspath = Compileclasspath + files (api.output.classesDir)
}
Test {
Runtimeclasspath = Runtimeclasspath + files (api.output.classesDir)
}
}

The Runtimeclasspath of test is required because we also need to load the classes in the API when we do the test.

Gradle learning using the Java plugin

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.