Friday, 28 July 2017

Download a directory by wget in UNIX

Download entire directory from Nexus by wget command:

 wget -r -nH --cut-dirs=6 --no-parent --reject="index.html*"  --user=<USER> --ask-password  http://<host>:<port>/nexus/content/repositories/Test-Releases/com/edu/test/

For e.g.  In Nexus there is a folder test and inside that there are different folders with version number.

/test/1.0
/test/1.1

Below are arguments of wget commands.

-r  : Recursive
-nH : Ignore the hostname. If this parameter is not passed then it will create the folder with name of nexus host and port

--cut-dirs : Ignore the number of directories post Nexus host and port. If 5 is passed in this case then in local directories will be like edu/test/*

--reject="index.html*" : Download the actual content but not the index.html

--ask-password  : It will ask for password after running command

Friday, 14 July 2017

Call shell script from Java code

Below is the code to call the shell script from Java code. This will call the shell script and wait for it's execution and print the output.

public static void main(String[] args) throws Exception {
        try {
                String param1=args[0];
               
                String scriptName = "/root/script.ksh";
                String commands[] = new String[]{scriptName,param1};

                Runtime rt = Runtime.getRuntime();
                Process proc = rt.exec(commands);
                proc.waitFor();
                StringBuffer output = new StringBuffer();
                BufferedReader reader = new BufferedReader(new       InputStreamReader(proc.getInputStream()));
                String line = "";                      
                while ((line = reader.readLine())!= null) {
                        output.append(line + "\n");
                }
                System.out.println("===> " + output);
        } catch (Exception e) {
                e.printStackTrace();
        }

Clone/Checkout specific file/folder from GIT

By default git checkouts the entire repository to local. As it used to compress the data during storage and checkout so it takes very less time. But if you want to checkout only one file then use below command.

git archive --remote=ssh://git@host:port/project_key/repos/repoName.git HEAD <fileName/FolderName> | tar -x

Maven command to download and deploy to Nexus from command line


This will deploy any artifacts from command line without pom.xml. Here repositoryId will be the id defined in settings.xml.

mvn deploy:deploy-file -Dfile=file_FullPath
-DgroupId=*** -DartifactId=*** -Dversion=***
-Dpackaging=jar   -Durl={repo_url} -DrepositoryId=releases


This command will download the artifacts from Nexus via command line.

mvn -C org.apache.maven.plugins:maven-dependency-plugin:2.4:get
-DremoteRepositories=http://<host:port>/nexus/content/repositories/Test/
-DgroupId=com.edu.test -DartifactId=testArtifact-Dversion=1.0 -Dpackaging=jar

rsync with compress option

This is the fastest way to copy data from one server to another.It will compress the data and transfer .

1) rsync -avxH ~/source_dir/  username@remote_host:destination_directory

This will sync data under source_dir to destination_directory

2) rsync -avxH ~/source_dir  username@remote_host:destination_directory

This will create a directory source_dir under destination_directory and copy data under source_dir


In case of local server

rsync -avxH /source /destination

Difference between class level and object locking and static object lock

1) Class level locking will lock entire class, so no other thread can access any of other synchronized blocks. 2) Object locking will lo...