Tech Blog

Coding

Avoidable mistakes while Working with Gradle

By 42Gears Team3 min read
← Back to blog

Deleting a Single Directory with Directory Name matching a pattern

Resource : https://stackoverflow.com/a/35635432

As per the given resource the below solution works.

import groovy.io.FileType

class DeleteDir extends DefaultTask {
   def baseDir = "."
   def deleteMe(Object... targets) {
       def directoryList = []
       def baseDirectory = new File(baseDir).eachFileRecurse(FileType.DIRECTORIES) {
           if ( targets.contains(it.name) ) {
               directoryList << it
           }
       }
       directoryList.each { dirObj -> 
           if ( dirObj.exists() ) {
               println "Removing directory: $dirObj"
               dirObj.deleteDir()
           }
       }
   }
}

task hello( type: DeleteDir) {
    baseDir = '.'
    deleteMe 'obj', 'gen', 'jniLibs'
}

The above solution works with a small change

Object… targets > Object target

targets.contains(it.name) > it.name.contains(target)

Avoid using of Thick(Fat) JAR

#Depends Up-On the Project type

#Before Starting check if creating a Fat jar would be a good approach for your Project.

Method to create a Thick(Fat) JAR 

The maven-shade-plugin can be used to create a fat JAR. This takes the app and all its dependencies and piles it into one big JAR. This JAR is immutable and has no external dependencies, making it easy to supply and operate.

Or 

The Java Plugin Jar task can be used to create a fat JAR.

Use

from {
        configurations.implementation.filter{ it.exists() }.collect {
         it.isDirectory() ? it : zipTree(it)
         }
    }
duplicatesStrategy = 'exclude'

But it comes with some serious drawbacks.

  1. JARs are not supposed to be aggregated in this way. There may be files with the same path present in several JARs and automatically the shadow plugin merges the first file into the fat JAR and discards the rest. This might cause some really frustrating bugs.

  2. The process is slow and inefficient.

  3. For Obfuscation, we now have to pass the fat JAR which comes with added challenges which could be completely avoided.

  4. Your business logic and application container are all in one file. Yes, the pro is a con sometimes and not an insignificant one. When your business logic changes, you will need to send another copy of the application container(which has not changed) with it.

  5. Spin-up times are higher compared to the deployment of a thin war to a running application container.

Local External Jar Files should be verified before use

The external Jar files added in the project should be verified after decompiling, external Jar files with Similar names might have a different version.

Eg : derby.jar might have a version 10.14 or 10.12 with a same name might throw an exception

[getDatabaseConnection::::: Message: Failed to start database] in case of a downgrade 

The Below resource can be used for decompile 

https://www.ezyzip.com/open-extract-jar-file.html

Converting Multiple Single Unit operations to Single batch operations

Single Unit operations like permission change using a commandLine can be converted to batch operations using predefined tasks like ant.chmod 

Also it supports including patterns

Before

task permission1(type: Exec) {
    commandLine 'chmod', '444', "${releaseFolder}/testproject/app/*.jar"
}

task permission2(type: Exec) {
    commandLine 'chmod', '444', "${releaseFolder}/testproject/app/lib/*.jar"
}

After

task permissionChange {
	ant.chmod(dir: "${releaseFolder}/testproject/app/", perm: "444", includes: "*.jar")
	ant.chmod(dir: "${releaseFolder}/testproject/app/lib/", perm: "444", includes: "*.jar")
}

Important tasks can be repeated in every Build irrespective of their UP-TO-DATE Status

By setting outputs.upToDateWhen { false }

Use Proguard terminal commands

Proguard terminal commands can be used to debug the proguard configuration properly 

Proguard Manual

http://www.comscigate.com/tutorial/ajay/Part%202/Development%20for%20Mobile%20Devices/Software/proguard2.1/proguard2.1/docs/manual/usage.html

Sample commands

java -jar proguard.jar @proguard_conf.cfg -injars InJar.jar -outjars OutJar.jar -printmapping mapping_file.txt >> command_output.txt

Keep reading

You may be interested in

Browse all articles →
Active Directory Federation Service
July 5, 20235 min read

Active Directory Federation Service

Active Directory Federation Service (ADFS) uses claims-based identity model to maintain application security and to implement federated identity. ADFS is a concept of Windows Server operating system, that provides users with single sign-on access to the application, those are located across organizational boundaries. For Example: We have 2 organizations – Organization 1 and Organization 2. […]

By Abinash MishraRead More →
Remote Access to Microsoft SQL Database Server
April 5, 20232 min read

Remote Access to Microsoft SQL Database Server

Remote access to a SQL server is important because it allows users to access the server from anywhere, at any time. This enables remote workers to access the data they need when they need it, even if they are not physically in the same location as the server. It also allows IT admins to access […]

By Malkeet SinghRead More →
Types of Load Balancer in AWS
February 6, 20235 min read

Types of Load Balancer in AWS

What is Load Balancing ? Load balancing is a critical component of any scalable and highly available Cloud or On-premise architecture. It helps distribute incoming traffic across multiple servers, improving the availability and scalability of a website or application. In the cloud, load balancing is offered as a service by various cloud providers such as […]

By Keerthik ShenoyRead More →