Gradle learning using the Java plugin

Source: Internet
Author: User

Download the GitHub sample code for this article series in the following ways:

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


The most common plugin of gradle is Java plugin. Like other plugin, Java plugin is nothing special, but it introduces more than one task and property to project. Of course, Java plugin also has a different place, one of which is that it introduces the concept of building life cycle in the project, just like Maven. However, unlike Maven, 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
Executing "gradle build", we can already see the main task 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 task executions above. The above task execution list basically describes 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 to use when compiling Java source files, and archivesbasename is used to specify the name of the file when packaged into a jar file.


(2) directory structure of Java projects
By default, Gradle uses the same Java project directory structure as maven:






For the MAVEN standard directory structure, please refer to MAVEN official website. Of course, like Maven, the above is just the default directory structure, which we can modify by configuring the directory structure.


(3) Configuring the existing source set
Gradle, while adopting the MAVEN directory structure, incorporates some of its own concepts, the source set. For the directory 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 directory structure, except that, by default, Gradle uses the same name in order to map the source set concept to the file system directory structure. The same is true for test. We can reconfigure the directory structure of these source sets in the Build.gradle file, and we can also create a new source set.


Essentially, each source set of Gradle contains a name and contains a property named Java and a property called resources, which are used to represent the source Set contains a collection of Java source files and resource files. In the actual application, we can set them to any directory value. For example, we can reset the directory structure of main:


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

The corresponding project directory structure is as follows:






We reset the directory structure of Main, and for test we keep the Gradle default directory structure.


(4) Create a new source set
It is also very simple to create a new source set, 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 directory for the API is Gradle set to ${path-to-project}/src/api/java, and the resource file directory is set to ${path-to-project}/src/api/ Resources We can also re-configure the directory structure of the API as in main above.


Gradle automatically creates the corresponding task for each newly created source set, creating the rule that for source Set,gradle named Mysourceset, it will be created Compile<mysourceset>java , Process<mysourceset>resources, and <mysourceset>classes 3 tasks. For this API, Gradle will create a Task named Compileapijava, Processapiresource, and apiclasses for it. We can execute "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 directly used Compilejava as the compilation task of Main. For test, Gradle still uses Compiletestjava.


As a general rule, the source set that we create ourselves with the name API is dependent on the other source set, such as 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 we compile main, we need to place the class file generated by the API compilation under the Classpath of Main. At this point, we can make the following configuration for main and test:

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

The Runtimeclasspath of test must be set because we also need to load the classes in the API when we run the tests.

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.