Skip to main content

Posts

Showing posts with the label Gradle

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 : Introduction to Gradle Build Lifecycle

Gradle Build Lifecycle Phases A gradle build has three phases Initialization - In this phase the gradle build is initialized. Gradle scans the project and its sub projects to create a Project instance of each project and sub project. Configuration - In this phase the gradle build is configured. In this phase a Task tree is created which determines how and when to run the tasks. Execution - In this phase the gradle build is executed. This phase actually runs the tasks configured in the configuration phase. What are the Gradle Build Phases Lets take the following example build.gradle println 'This is executed during the configuration phase.' task configured { println 'This is also executed during the configuration phase.' } task test { doLast { println 'This is executed during the execution phase.' } } task testBoth { doFirst { println 'This is executed first during the execution phase.' } doLast { println ...

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 disable Gradle Daemon

How to disable Gradle Daemon There are two ways in which you can disable gradle daemons. Using Environment Variable To disable gradle daemon using env variable you can add a new flag -Dorg.gradle.daemon=false to GRADLE_OPTS env variable. GRADLE_OPTS="-Dorg.gradle.daemon=false" Using gradle.properties To diable gradle daemon using gradle.properties, you can edit the gradle.properties file that is under the directory <User-Home>/.gradle/ . You need to set the following property in the file. org.gradle.daemon=false Also Read Introduction to Gradle Gradle : What is a Gradle Daemon ? How to continue a build even if a failure occurs How to import a gradle file in another gradle file How to stop a running Gradle Daemon ?

Gradle : How to list all running Gradle Daemon processes

How to list all running Gradle Daemon processes To see a list of all the running gradle Daemon processes you can use the following command. gradle --status This will give you a list of all the running processes with their versions as shown in the sample output below. PID STATUS INFO 3461 IDLE 3.0 31881 BUSY 3.0 Here the output is PID - the process id of the gradle daemon process. STATUS - status of the Gradle Daemon, Idle states that this gradle daemon can be used for making builds. BUSY states that it is currently used for running some build. INFO - it states the version of gradle that is being used for these daemons. Also Read Introduction to Gradle Gradle : What is a Gradle Daemon ? How to continue a build even if a failure occurs How to import a gradle file in another gradle file How to stop a running Gradle Daemon ?

Gradle : How to stop a running Gradle Daemon ?

How to stop a running Gradle Daemon ? To stop all the runnung gradle daemon processes you can using the following command. gradle --stop This will stop all the gradle daemon processess that are running with the same gradle version as version of gradle that runs this command. Also Read Introduction to Gradle Gradle : What is a Gradle Daemon ? How to continue a build even if a failure occurs How to import a gradle file in another gradle file

Gradle : What is a Gradle Daemon ?

Points to Remember Gradle runs on JVM and several other dependencies. Gradle has to do boootstrap stuff whenever gradle is started. This startup process takes time and makes the build slower. To overcome these points gradle daemon was introduced. What is a Gradle Daemon ? Gradle daemon is a long lived background background process that can be reused for building gradle projects faster. Gradle wrapper saves the time for starting a new gradle instance every time and saves time required for bootstrapping and initialization.Such bootstrap tasks include Initialization of JVM Cache Project information Cache files , tasks Project structures etc Gradle daemon is configurable and can be switched on of off using gradle.properties in path <user-home>/.gradle/gradle.properties . Also, gradle daemon should be used only for development environments and not for production environmnets. This is beacasue in production environments you want to build your projet independently since it is more...

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' ...