I used this command to copy all the downloaded maven JARs into a deployment directory I was using to build a java project:

find ~/.m2 -name *.jar | xargs -I file cp file ~/deploy/workspace

Let’s take a look at what it does.  The first part, “find ~/.m2 -name *.jar” is the basic use of the Linux “find” command.  We are telling it to look in the ~/.m2 directory (which is where maven caches its dependencies) for all files that match *.jar

Next we pipe “|” those results into a command called xargs. What xargs does is run a shell command once for each input line you send to it.  In our case, it will run once for each full file path that is returned from “find”, so you can see where we are going with this.  Xargs knows that you might want to run complicated commands, so it’s programmable.  By using the “-I” switch, we are basically declaring a variable, so “-I file” means “declare a variable called file that holds the input argument”.  Next comes the actual command:  “cp file ~/deploy/workspace”, which takes the “file” variable (which you remember holds the input parameter, which you hopefully still remember contains the full file path to one .jar file) and sends it to the “cp” command.

The net effect is this:

every .jar file in the local maven cache gets copied to ~/workspace/deploy (or wherever you want the files to go)

Leave a Reply

Post Navigation