Write Gradle builds

Now that you know what Gradle is and what Gradle can do for you, how do you use Gradle?

Build a Java Application

If we now need to build a Java Application, how do we do it? The process of building a Java Application, of course, starts with compiling Java source files, creating manifest files to configure the main classes, and finally packing all files into JAR packages.

Gradle has already written this process for us. We just need to use Gradle’s built-in Application plug-in to complete the Java Application package

plugins {
  	// Apply the application plug-in
    application
}

// Add the javaSrc folder to the Java source file path. If not, default is SRC /main/ Java
java {
    sourceSets {
        named("main") {
            java {
                srcDir("javaSrc")}}}}// Configure the loaded main class
application {
    mainClassName = "com.kevinxie.gradledemo.Main"
}
Copy the code

Our Task that executes run is then ready to compile and package the Java Application.

Build a c + + Application

Gradle can build almost any type of software. Can I build an application in c++? Of course, Gradle has the CPP Application plug-in built in for us

// Apply the cpp-application plugin
apply(plugin = "cpp-application")


configure<CppApplication>() {
    source {
        from("cppSrc/cpp")
    }
    privateHeaders {
        from("cppSrc/headers")}}Copy the code

Gradle built-in plugins

Each.properties file corresponds to an implementation class of the plugin. The names of these files are the ids of their plugins. The plug-in id is org. Gradle opening, so we use plug-ins, can be omitted in front of the plug-in id org. Gradle., let’s take a look at org. Gradle. Java. The contents of the properties file

implementation-class=org.gradle.api.plugins.JavaPlugin
Copy the code

Corresponding implementation class is the Java plug-in org. Gradle. API. Plugins. The JavaPlugin class, when we used Java plug-in is invoked JavaPlugin the apply methods of class, some universal construction task for us to create, and expand the configuration items.

How to use Gradle built-in plug-ins

Gradle has so many built-in plugins. How do you get started using each one quickly?

  • Look at the documentation, go to the official website to find the documentation for the plug-in
  • Look directly at the Extension source to see which fields are configurable

Taking the application plug-in as an example, we can view the source code of Plugin and see which extensions the plug-in extends. Then, we can follow the source code to see what configuration there is. Why can we configure application directly?

public void apply(final Project project) {
    / / application JavaPluginproject.getPluginManager().apply(JavaPlugin.class); ./ / add ExtensionsApplicationPluginConvention pluginConvention = addExtensions(project); . }private ApplicationPluginConvention addExtensions(Project project) {
    ApplicationPluginConvention pluginConvention = new DefaultApplicationPluginConvention(project);
    pluginConvention.setApplicationName(project.getName());
    project.getConvention().getPlugins().put("application", pluginConvention);
    // Create an extension of the application, corresponding to the extension object JavaApplication
    project.getExtensions().create(JavaApplication.class, "application", DefaultJavaApplication.class, pluginConvention);
    return pluginConvention;
}
Copy the code

Then we can read the JavaApplication source code to see what we can configure, and all set properties are configurable

public interface JavaApplication {
		// Set the application name
    void setApplicationName(String applicationName);
		// Configure the main class name
    void setMainClassName(String mainClassName);
		// Configure the default JVM parameters for the application
    void setApplicationDefaultJvmArgs(Iterable<String> applicationDefaultJvmArgs);
		// Configure the application execution folder
    void setExecutableDir(String executableDir);
		// Configure the application distribution
    void setApplicationDistribution(CopySpec applicationDistribution);
}
Copy the code

Of course, this would be basic if you could customize the plug-in