Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

Thursday, August 23, 2012

Finding a file from a bunch of jar files

Just had a need to quickly find a file eg. the super pom location in maven which was suspected to be in one of the jar files located within Maven's lib director ($MAVEN_HOME/lib).

The following command seems to work for me, it took me some time messing around with commands to get this out, so guess it would be beneficial to put it down here just to keep myself reminded and also hopefully help some others who are googling for the same stuff.

Eg. to find the super pom located in one of the jars within Maven's lib directory we could do

find . -iname '*.jar' | xargs -n 1 jar -tvf | grep -i '**.xml'
This would gives us something like
  ...
  1523 Thu Feb 24 12:34:04 EST 2011 META-INF/maven/org.sonatype.aether/aether-api/pom.xml
  3908 Mon Feb 28 18:28:26 EST 2011 META-INF/maven/org.apache.maven/maven-embedder/pom.xml
  2222 Mon Feb 28 18:27:48 EST 2011 META-INF/maven/org.apache.maven/maven-settings-builder/pom.xml
  ...
  1931 Mon Feb 28 18:28:12 EST 2011 META-INF/maven/org.apache.maven/maven-repository-metadata/pom.xml
  2248 Mon Feb 28 18:27:52 EST 2011 META-INF/maven/org.apache.maven/maven-model-builder/pom.xml
  ...
But it doesn't tell us which jar file it's in. The simplest way I found was to
find . -iname '*.jar' | xargs --verbose -n 1 jar -tvf | grep -i '**.xml' 
jar -tvf ./aether-api-1.11.jar 
  1523 Thu Feb 24 12:34:04 EST 2011 META-INF/maven/org.sonatype.aether/aether-api/pom.xml
jar -tvf ./maven-embedder-3.0.3.jar 
  3908 Mon Feb 28 18:28:26 EST 2011 META-INF/maven/org.apache.maven/maven-embedder/pom.xml
jar -tvf ./maven-settings-builder-3.0.3.jar 
....
jar -tvf ./maven-model-builder-3.0.3.jar 
 13206 Mon Feb 28 18:30:32 EST 2011 META-INF/plexus/components.xml
  4840 Mon Feb 28 18:30:30 EST 2011 org/apache/maven/model/pom-4.0.0.xml
  2248 Mon Feb 28 18:27:52 EST 2011 META-INF/maven/org.apache.maven/maven-model-builder/pom.xml
...
Now we could tell Maven's super pom, pom-4.0.0.xml, is in maven-model-builder-3.0.3.jar file under /org/apache/maven/modem/ directory.

Tuesday, February 28, 2012

Bash command that save my ass over and over again

The following linux commands are really no brainer stuff that every elementary course would have taught us, and it saved my butt many times over and over again.

   find . -iname <some file name>
To find files recursively

   find . -iname <some file name> | xargs
To find files recursively and list them out horizontally separated by space and doing escaping when necessary

  find . -iname <some file name> | xargs <some other bash commands>
To find files recursively and list them out as arguments such that we could perform other commands on it like rm -f (force delete) for example

  grep -ir 'some text' .
Find all files recursively starting with the current directory

We could do interesting stuff like

  find . -iname '*.xml' | xargs grep -i "testing"
Find all xml files recursively starting with current directory that has text 'testing' contains in it.

Bash commands ROCKS! It's always worthwhile if you are using a windows machine to install Cygwin, it comes with bash by default so you could dig up code segment quickly rather than relying on the pathetic doggy search that comes with Windows. Just me 2 cents !