Skip to main content

Posts

Showing posts from December, 2016

Tomcat : How to change Deployment directory from webapp to custom Directory.

How to change Working Directory of Tomcat to Custom Directory Default Tomcat working directory is webapp . If you want to change this to some other directory then you can follow the steps below. Open the server.xml file. You can find this file in conf/ directory of your tomcat folder. Find the following line in the file < Host name = "localhost" appBase = "webapps" unpackWARs = "true" autoDeploy = "true" > It states the following things name="localhost" - name of the server appBase="webapps" - working directory of tomcat where applications are deployed unpackWARs="true" - if tomcat should unpack war files. autoDeploy="true" - if tomcat should auto deploy changes How to change Work directory of tomcat. To change the work directory from webapp to some other directory you can give the absolute path to the directory where you want to deploy apps. relative path to the directory

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