Saturday 7 April 2018

Creating New Gradle Builds

Following this guide, you’ll create a trivial Gradle project, invoke some of the basic Gradle commands, and get a sense of how Gradle manages the project.

What you’ll need

  • About 11 minutes
  • A terminal or IDE application
  • A Java Development Kit (JDK), version 1.7 or better (only necessary to run Gradle)
  • Gradle distribution, version 4.6 or better
Shell commands will shown for Unix-based systems. Windows has analogous commands for each.

Initialize a project

First, let’s create a new directory where our project will go.
❯ mkdir basic-demo
❯ cd basic-demo
Now we can use Gradle’s init command to generate a simple project. We will explore everything that is generated so you know exactly what’s going on.
❯ gradle init
Starting a Gradle Daemon (subsequent builds will be faster)

BUILD SUCCESSFUL in 3s
2 actionable tasks: 2 executed
The command should show "BUILD SUCCESSFUL" and generate the following "empty" project. If it doesn’t, please ensure that Gradle is installed properly, and that you have the JAVA_HOME environment variable set correctly.
This is what Gradle generated for you.
.
├── build.gradle  
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar  
│       └── gradle-wrapper.properties  
├── gradlew  
├── gradlew.bat  
└── settings.gradle  
Project configuration script for configuring tasks in the current project
Gradle Wrapper executable JAR
Gradle Wrapper configuration properties
Gradle Wrapper script for Unix-based systems
Gradle Wrapper script for Windows
Settings configuration script for configuring which Projects participate in the build
gradle init can generate various different types of projects, and even knows how to translate simple pom.xml files to Gradle.
Boom! Roasted. We could just end the guide here, but chances are you want to know how to use Gradle in this project. Let’s do that.

Create a task

Gradle provides APIs for creating and configuring tasks through a Groovy or Kotlin-based DSL. A Project includes a collection of Tasks, each of which performs some basic operation.
Gradle comes with a library of tasks that you can configure in your own projects. For example, there is a core type called Copy, which copies files from one location to another. The Copy task is very useful (see the documentation for details), but here, once again, let’s keep it simple. Perform the following steps:
  1. Create a directory called src.
  2. Add a file called myfile.txt in the src directory. The contents are arbitrary (it can even be empty), but for convenience add the single line Hello, World! to it.
  3. Define a task called copy of type Copy (note the capital letter) in the main build file, build.gradle, that copies the srcdirectory to a new directory called dest. (You don’t have to create the dest directory — the task will do it for you.)
task copy(type: Copy, group: "Custom", description: "Copies sources to the dest directory") {
    from "src"
    into "dest"
}
Here, group and description can be anything you want. You can even omit them, but doing so will also omit them from the tasksreport, used later.
Now execute your new copy task:
❯ ./gradlew copy
:copy

BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
Verify that it worked as expected by checking that there is now a file called myfile.txt in the dest directory, and that its contents match the contents of the same one in the src directory.

Apply a plugin

Gradle includes a range of plugins, and many, many more are available at the Gradle plugin portal. One of the plugins included with the distribution is the base plugin. Combined with a core type called Zip, you can create a zip archive of your project with a configured name and location.
Add the base plugin to your build.gradle file using the plugins syntax. Be sure to add the plugins {} block at the top of the file.
plugins {
    id "base"
}

... rest of the build file ...
Now add a task that creates a zip archive from the src directory.
task zip(type: Zip, group: "Archive", description: "Archives sources in a zip file") {
    from "src"
}
The base plugin works with the settings to create an archive file called basic-demo-1.0.zip in the build/distributions folder.
In this case, simply run the new zip task and see that the generated zip file is where you expect.
❯ ./gradlew zip
:zip

BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed

Explore and debug your build

Let’s see what else we can do with Gradle in our new project. A full reference to the command-line interface is available as well.

Discover available tasks

The tasks command lists Gradle tasks that you can invoke, including those added by the base plugin, and custom tasks you just added as well.
❯ ./gradlew tasks

> Task :tasks

------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------

Archive tasks
-------------
zip - Archives sources in a zip file

Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
clean - Deletes the build directory.

Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.

Custom tasks
------------
copy - Simply copies sources to a the build directory

Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'basic-demo'.
components - Displays the components produced by root project 'basic-demo'. [incubating]
dependencies - Displays all dependencies declared in root project 'basic-demo'.
dependencyInsight - Displays the insight into a specific dependency in root project 'basic-demo'.
dependentComponents - Displays the dependent components of components in root project 'basic-demo'. [incubating]
help - Displays a help message.
model - Displays the configuration model of root project 'basic-demo'. [incubating]
projects - Displays the sub-projects of root project 'basic-demo'.
properties - Displays the properties of root project 'basic-demo'.
tasks - Displays the tasks runnable from root project 'basic-demo'.

Verification tasks
------------------
check - Runs all checks.

Rules
-----
Pattern: clean<TaskName>: Cleans the output files of a task.
Pattern: build<ConfigurationName>: Assembles the artifacts of a configuration.
Pattern: upload<ConfigurationName>: Assembles and uploads the artifacts belonging to a configuration.

To see all tasks and more detail, run gradlew tasks --all

To see more detail about a task, run gradlew help --task <task>

BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
SQL ALIAS Statement. SQL WHERE Clause. SQL AND Clause. SQL OR Clause. SQL UNION Operator. SQL LIKE operator. SQL ORDER BY Clause. SQL GROUP BY clause. SQL HAVING clause. SQL JOIN clause. SQL INNER JOIN. SQL LEFT JOIN. SQL RIGHT JOIN. SQL FULL JOIN. SQL SELF JOIN. SQL CARTESIAN JOIN. SQL Functions. iText tutorial. iText jar overview. Java iText create pdf Java itext document. Java iText chunk. Java iText phrase. Java iText paragraph. Java iText anchor. Java iText list. Java iText table. Java iText image. Java iText font. Java iText chapter and section. Java iText read pdf. Java iText split pdf. Java iText merge pdf. Java iText add password protection to PDF. JAXB tutorial. JAXB annotation overview. JAXB marshalling example1 JAXB marshalling example2 JAXB unmarshalling. Generate java class from xsd Generate xsd from java class JUnit tutorial. JUnit framework. Basic annotation example. Expected exception test. Junit ignore test. JUnit time test. JUnit suite test. JUnit parameterized test. Junit test runner. Generics tutorial. Generics in java. Generics terms and naming convention. Compile time checking test. ClassCastException at runtime test. Wildcard in generics. Unbounded wildcard. Upper bounded wildcard. Lower bounded wildcard. Generics class example. Generics class example with two parameters. Generics method example. Generics method example with two parameters. Generics constructor example. Generics constructor example with two parameters. Log4j tutorial. Log4j overview. Logger class. Log4j example. Log4j properties file. Log4j xml file. Logging levels. Log4j file appender. Log4j multiple appenders. JavaMail API tutorial. JavaMail API overview. JavaMail API core classes. JavaMail API sending email. Send email. Send email with attachment. Send HTML content. Send inline image. Send email through Gmail server using TLSconnection. Send email through Gmail server using SSL connection. Receiving emails. Receiving emails with attachments. Replying emails. Forwarding emails. Deleting emails. Ant tutorial. Ant overview. Ant build file. Ant-Build java project. Ant-Create java document. Ant-Use classpath. Ant-Create jar file. Ant-Create war file. Ant-Run junit test case. Run ant script from eclipse. Jsoup tutorial.

No comments:

Post a Comment