Skip to main content

Posts

Showing posts with the label GradleTask

Gradle : How to upload the Jar or War file to custom path in local machine

Points to Remember Default location for gradle dependencies is ~/.gradle or /home/{user}/.gradle . We can specify the location from where to gradle should find dependencies and where it should upload the created artifacts. How to upload an Artifact to a custom location in file system buildscript { repositories { maven { url uri( "$buildDir/repo" ) } } } group 'com.ekiras' version '1.0-SNAPSHOT' apply plugin: 'groovy' apply plugin: 'maven' repositories { mavenCentral() } dependencies { // dependencies } uploadArchives { repositories { mavenDeployer { repository(url: uri( "$buildDir/repo" )) } } } In the above example, uploadArchives { repositories { mavenDeployer { repository(url:...

Gradle : How to make a custom War file

Points to Remember War task extends Jar You can create war files with any configuration defined in configurations { } closure You can also add files to an existing war file. You can select the files that needs to be included or excluded while creating a war file How to create a War file in Gradle To create a war file you have to create a task of type War as shown below // include java plugin apply plugin : 'java' task createWar(type : War){ destinationDir = file ( "$buildDir" ) baseName = "my-war" version = "1.1" caseSensitive = true classifier = "SNAPSHOT" from "src" } Run the above task with command gradle -q createWar , this will create a war file named my-war-1.1-SNAPSHOT.war in the build folder. See Full Documentation of War Task How to create a War file and exclude some files Now ...

Gradle : How to use Gradle Cargo Plugin to Deploy war on Tomcat

This post is based on the Gradle Cargo Plugin by bmuschko Include the Plugin You need to include the plugin in your build.gradle . buildscript { repositories { jcenter() } dependencies { classpath 'com.bmuschko:gradle-cargo-plugin:2.2.3' } } apply plugin: 'com.bmuschko.cargo' buildscript block should be the first thing in the build.gradle file. Include the Gradle Cargo plugin dependencies Add the following in your gradle dependencies block dependencies { def cargoVersion = '1.4.5' cargo "org.codehaus.cargo:cargo-core-uberjar:$cargoVersion" , "org.codehaus.cargo:cargo-ant:$cargoVersion" } Define the Tomcat Container To deploy The war files to Tomcat you need to create a dsl by name cargo and define the mandatory containerId . Note ContainerId will define the version of tomcat you...

Gradle : How To Exclude Files and Packages from a Jar file

How To Exclude File from a Jar file. Gradle task Jar has a property excludes which takes an array as an input and exclude these files from the Jar file it creates. Let's assume the following directory structure Following example excludes files from the Jar file. task createExcludingFiles(type :Jar){ from ('src'){ excludes = ["main/java/com/ekiras/demo/D.java","main/java/com/ekiras/demo/E.java"] } } This will exclude files D.java , E.java from the jar file. Run the gradle task using command gradle -q createExcludingFiles . The Jar file created will have the following files. META-INF/ META-INF/MANIFEST.MF main/ main/java/ main/java/com/ main/java/com/ekiras/ main/java/com/ekiras/demo/ main/java/com/ekiras/demo/A.java main/java/com/ekiras/demo/C.java main/java/com/ekiras/demo/F.java main/java/com/ekiras/demo/B.java main/java/com/ekiras/demo/p1/ main/java/com/ekiras/demo/p1/X.java main/java/com/ekiras/demo/p1/Z.java main/java/com/ekiras/de...

Gradle : How to create a Jar file

How to name a Jar file Lets take the following jar file for explaning the jar naming conventions. customName_wrapper_2.0.1_SNAPSHOT.jar A jar file has the following properties Property Description baseName customName appendix wrapper version 2.0.1 classifier SNAPSHOT archiveName - Here property archiveName if specified will override all the other naming conventions like baseName etc. It should be a fully qualified name of the Jar including the extension (.jar) Other Properties of Gradle Task :: Jar Property Description destinationDir Destination where jar is to be created manifest Include Manifest file from Directory from where files are to be read Creating a Jar file Let's assume the following directory structure Create a Jar file with all files. We can write a gradle task to include all files in src to our jar file as follows task createAll(type : Jar){ from 'src' } When we run the gradle task as gradle -q createAll will give the following files in the jar META-I...

Gradle : How to list files inside a Jar, Zip Tar Files in Gradle Task

How to load and read files in Gradle Project. The Gradle Project Api has the following methods for using files. file() - to load and read a particular file files() - to load and read multiple files fileTree() - to load and read file hierarchy in a directory or folder zipTree() - to load and read a zip file (includes Jar , War and Ear files) tarTree() - to load and read a tar file How to list files inside a Zip file Jar File and Tar file Lets say we have a file structure as shown in image below. Following Gradle task will list the files inside a zip file, jar file and a tar file at the location "src/main/resources/archives/" task showFiles(){ doLast{ FileTree zipFiles = zipTree("src/main/resources/archives/resources.zip"); FileTree jarFiles = zipTree("src/main/resources/archives/resources.jar"); FileTree tarFiles = tarTree("src/main/resources/archives/resources.tar.gz"); println " \n#Contents of Zip ...

Gradle : Difference between File, FileCollection and FileTree in Gradle

Difference Between File, FileCollection and FileTree in Gradle File FileCollection FileTree File is from java.io package FileCollection is from org.gradle.api.file package FileTree is from org.gradle.api.file package File represents a single file. FileCollection represents a collection of files FileTree represents hierarchy of files in a directory or folder. Difference Between File, FileCollection and FileTree Suppose we have a directory structure as shown in image below. We can write a gradle task as shown below task loadFiles(){ doLast{ File file = file('src/main/resources/sample1.txt') println file.name println "\n" FileCollection collection = files('src/main/resources/sample1.txt','src/main/resources/sample2.txt') collection.each { println it.name } println "\n" FileTree tree = fileTree('src') tree.each { println it.name ...

Gradle : How to read all files in a directory using FileTree

Introduction to FileTree in Gradle Api FileTree is an interface that extends FileCollection and introduces methods like matching() and visit() . It class represents a hierarchy of files in a given directory. It includes files in same folder and all sub folders. How to get all files in a Directory To load all files in a given directory and its sub directories you can use FileTree . Syntax for FileTree is as shown below. FileTree tree = fileTree('path/to/dir') The FileTree can get files in following ways from relative path from We can write a gradle task to list all the files in the src folder as shown below. task loadFiles(){ doLast{ FileTree tree = fileTree('src') tree.each { println it.name } } } When we run the aobe task using command gradle -q loadFiles it will give the following output sample1.txt sample2txt file1.properties A.java

Gradle : What is a FileCollection in Gradle Api

Introduction to FileCollection FileCollection is an interface that extends Iterable<File> , AntBuilderAware and Buildable interfaces. How to load files in a FileCollection Suppose we have a file structure as shown in image below We can load multiple files for operation using the following syntax FileCollection collection = files('relative/path/file1','absolute/path/file2',new File('file3')) As shown in the example above we can load files in three ways From a relative path From a absolute path From a File object. We can write a gradle task to read all files in a folder or directory as follows task fileTree(){ doLast{ FileTree tree = fileTree('src') tree.each { println it.name } } } Now when we run the above gradle task with the command gradle -q fileTree , it will give the following output sample1.txt sample2txt file1.properties resources.tar.gz resources.jar resources.zip A.java Operations and methods ...

Gradle : How to load and read a file in gradle task

How to read a file in Gradle Task To load a file, you can take following three approaches Load file using relative path File file = file('relative/path/file') Load file using absolute path File file = file('absolute/path/file') Load a file using File object File file = file(new File('somePath')) Lets see how these three approaches work task loadFiles << { println file('src/main/resources/file1.properties') println file('/home/ekiras/workspace/tutorials/gradle/src/main/resources/file1.properties') println file(new File('src/main/resources/file1.properties')) } When we run the above task using command gradle -q loadFiles it will give the following output /home/ekiras/workspace/tutorials/gradle/src/main/resources/file1.properties /home/ekiras/workspace/tutorials/gradle/src/main/resources/file1.properties /home/ekiras/workspace/tutorials/gradle/src/main/resources/file1.properties How to read a File in gradle task Say, we have...

Gradle : How to check if the operation system is Windows, Linux or Mac Os.

How to check if the operation system is Windows, Linux or Mac Os. org.gradle.internal.os.OperatingSystem is an interface in gradle. This interface provides methods to get th einformation about the operating system. This interface provides the following method to get the information about the operating system. task osInfo(){ doLast{ println OperatingSystem.current(); println OperatingSystem.current().name; println OperatingSystem.current().isLinux(); println OperatingSystem.current().isMacOsX(); println OperatingSystem.current().isWindows(); } } When we run the task with the command gradle -q osInfo it will give the following information. :osInfo Linux 4.4.0-51-generic amd64 Linux true false false BUILD SUCCESSFUL Total time: 0.68 secs

Gradle : How to make a task that depends on another task

How to write a Gradle Task A gradle task can be written in the following two ways task hello(){ doLast(){ println "Hello World" } } Or you can write a task as follows task hello << { println "Hello World" } Here in example 2, << operator is simply an alias for doLast. Write a Gradle Task that depends on another Gadle Task Lets take an example of the following gradle file. build.gradle task openFile(){ doLast{ println "Open file" } } task readFile(dependsOn : 'openFile'){ doLast{ println "Read File" } } task closeFile(dependsOn : ['openFile','readFile']){ doLast{ println "Close File" } } Here in this example, we have three tasks openFile - that will open a file readFile - that will read a file closeFile - that will close a file. Now, for able to read from a file, we should first open a file, so readFile depends on openFile , also a file will...

Gradle : What are task alias short forms

What is a Gradle Task alias Suppose we have a gradle file as below build.gradle task copyAllFiles(){ doLast{ println "copyAllFiles" } } task copySomeFiles(){ doLast{ println "copyAllFiles" } } task copyParticularFiles(){ doLast{ println "copyAllFiles" } } Now to run the above tasks we have to run the following commands gradle -q copyAllFiles gradle -q copySomeFiles gradle -q copyParticularFiles However we can also run the gradle tasks by unique names that diffrenetiates the tasks for example gradle -q cAF // short form for copyAllFiles gradle -q cSF // short form for copySomeFiles gradle -q cPF // short form for copyParticularFiles but if we run a task with a short name that does not uniquely define a task then it will trow an exception. for example gradle -q copy will throw the execption FAILURE: Build failed with an exception. * What went wrong: Task 'copy' is ambiguous in root project 'Grad...

Gradle : How to define a list of Default tasks in Gradle

What is a Gradle Default task ? In gradle, we can have default tasks . A default task is a task that will run if no task is specified. We can define any task a gradle default task using the following defaultTasks 'task1','task2' The defaultTasks takes a list of tasks as a parameter. These tasks will be run if no task is specidied to run. build.gradle defaultTasks 'banner','attribution' task banner(){ doLast { println "______ _ _"; println "| ____| | (_)" println "| |__ | | ___ _ __ __ _ ___ ___ ___ _ __ ___" println "| __| | |/ / | '__/ _` / __| / __/ _ \\| '_ ` _ \\" println "| |____| <| | | | (_| \\__ \\| (_| (_) | | | | | |" println "|______|_|\\_\\_|_| \\__,_|___(_)___\\___/|_| |_| |_|" } } task attribution(){ doLast { println "Developed by :: Ekansh Rastogi" } } Default tasks will be r...

Gradle : How to run a specific gradle task or multiple tasks

How to run a specific gradle task When you want to run a particular task only then you can run that task using the following command. gradle -q <task-name> -q option tells gradle that you want to run only the tasks that are provided. In this example we have instructed gradle to run only one task How to run multiple gradle tasks Multilple gradle tasks can be run using the following syntax. gradle -q <task-name> <task-name> <task-name> In second example we have told gradle to run 3 tasks, they are run in the order they are provided. However if one of the task is dependent on any other task, then that task will execute first. Also A task is run only one time even if it is specified multiple times. For example if we have the following tasks in build.gradle task A { doLast { println 'Running task A' } doFirst{ println(" Pring first") } } task B(dependsOn: A) { doLast { println 'Running task B' ...

Gradle : Difference between doFirst() and doLast() methods

Point to Remember doFirst() and doLast() are the methods of org.gradle.api.Task interface. Both these methods take a Closure as a parameter. When a task is called this closure is called. A Gradle task is sequence of Action objects and when a task is run all these actions are called in a sequence by calling Action.execute(T) What are doFirst() and doLast() methods in a Gradle task. doFirst(Closure action) This method will add your closure as an action to the beginning of the action sequence. All the code that is written in this closure is executed first when the task is executed. doLast(Closure action) This method will add your closure as an action to the end of the action sequence. All the code in this closure is executed in last when the task is executed. Let's take a look at the following example. task A { doLast { println("Running task A :: LAST") } doFirst{ println("Running task A :: FIRST ") } } When we run this task usin...

Gradle : How to continue a build even if a failure occurs

How to continue a build even if a failure occurs Many a times there is a need to continue running a build process even if some of the tasks fails. To run a build even if the some of the tasks fail we can use the --continue switch. It will execute all tasks even if some of them fails. Normal behavior is that build stops as soon as first failure is encountered. It will show the report of encountered failures at the end of the build. When a task fails all the tasks that depends on this task will not be executed . This is because, if compilation fails then, there is no point of packaging the project. Now lets take see a few examples build.gradle group 'com.ekiras' version '1.0-SNAPSHOT' apply plugin: 'java' sourceCompatibility = 1.8 repositories { mavenCentral() } dependencies { testCompile group: 'junit', name: 'junit', version: '4.11' } task failure(){ doLast{ println " I am going to fail now" throw ne...

Gradle : What is a Gradle Task and how to write a custom task

What is a Gradle Task. Lets first create a gradle file build.gradle with the following contents in it. group 'com.ekiras' version '1.0-SNAPSHOT' apply plugin: 'java' sourceCompatibility = 1.8 repositories { mavenCentral() } dependencies { testCompile group: 'junit', name: 'junit', version: '4.11' } Now, lets run the the following command from terminal in the root directory where build.gradle is saved. gradle tasks It will give the list of all the tasks that are defined. It includes both gradle default tasks and custom tasks . Now, you can run any defined task using the command. gradle <task-name> How to write a Custom Task in Gradle. A task in gradle is an imlpementation of org.gradle.api.Task interface. Task interface has two methods doLast() and doFirst() which can define the order of task execution. doFirst(Closure action) - This adds the given closure to the beginning of this task's action list. doLast(Closu...

Gradle : How to import a gradle file in another gradle file

How to Import Gradle file in another gradle file. To import a gradle file in a gradle file you can use the following command in .gradle . We have a gradle file build.gradle and we are including two other gradle files tasks.gradle and test.gradle . apply from : 'tasks.gradle' apply from : 'https://ekiras.github.io/temp/test.gradle' Here, in line 1, we have imported tasks.gradle which lies in the same directory as build.gradle and in line 2 we have imported a test.gradle from a remote server. (Git hub pages in this case, it can be an s3 url or your server file url) Test Imported gradle file task build.gradle has the following content group 'com.ekiras' version '1.0-SNAPSHOT' apply plugin: 'java' apply from : 'tasks.gradle' apply from : 'https://ekiras.github.io/temp/test.gradle' sourceCompatibility = 1.8 repositories { mavenCentral() } dependencies { testCompile group: 'junit', name: 'junit', version: ...

Gradle : Task to add selected files to jar

Points To Remember You can create local variable in gradle with the type def, e.g def jarName = "ekiras" You can print variable with help of println  e.g println "jar name is -> $jarName Gradle task declaration syntax is  task taskName(type: taskType){} Task to add selected files to jar We will be writing two tasks for this First task -  compileFiles  -> this will compile all the files in the project and then save them to the build/classes/  folder of the project Second Task - makeJar  -> this will take all the compile files from the base folder and make a jar of these files in build/lib/  folder of the project. task compileFiles(type: JavaCompile) { println '>>>>>>>>>>>>>>>>>>>>> start compiling' source = sourceSets.main.java.srcDirs classpath = sourceSets.main.compileClasspath destinationDir = sourceSets.main.output.classesDir println '>>>>>>...