<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Molindo Techblog &#187; Shell</title>
	<atom:link href="http://techblog.molindo.at/category/shell/feed" rel="self" type="application/rss+xml" />
	<link>http://techblog.molindo.at</link>
	<description>Molindo Techblog – formerly known as talk-on-tech.blogspot.com</description>
	<lastBuildDate>Tue, 20 Dec 2011 10:22:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Spotting duplicate classes in Jar files</title>
		<link>http://techblog.molindo.at/2008/10/spotting-duplicate-classes-in-jar-files.html?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=spotting-duplicate-classes-in-jar-files</link>
		<comments>http://techblog.molindo.at/2008/10/spotting-duplicate-classes-in-jar-files.html#comments</comments>
		<pubDate>Mon, 20 Oct 2008 17:29:00 +0000</pubDate>
		<dc:creator>Stefan Fußenegger</dc:creator>
				<category><![CDATA[Maven]]></category>
		<category><![CDATA[Shell]]></category>

		<guid isPermaLink="false">http://tech.molindo.at/2008/10/spotting-duplicate-classes-in-jar-files.html</guid>
		<description><![CDATA[After more than a month it&#8217;s time for another post. Sorry to all of you for keeping you waiting &#8230; well, honestly I don&#8217;t think somebody even noticed Today, I stumbled upon a classpath related problem &#8211; once again. As I doubt that I am the only one to ever face this problem, I want [...]]]></description>
			<content:encoded><![CDATA[<p>After more than a month it&#8217;s time for another post. Sorry to all of you for keeping you waiting &#8230; well, honestly I don&#8217;t think somebody even noticed <img src='http://techblog.molindo.at/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Today, I stumbled upon a classpath related problem &#8211; once again. As I doubt that I am the only one to ever face this problem, I want to share a short shell script that came to the rescue.</p>
<p>But first, what was the problem? After adding some additional dependencies to our <a href="http://maven.apache.org/">POM</a> &#8211; quite carelessly I have to admit &#8211; <a href="http://www.setlist.fm/">our application</a> started sending mails without subject, sender address and messed up special characters. Interestingly enough, I didn&#8217;t touch the mail part at all. I added <a href="http://cxf.apache.org/">Apache CXF</a> dependencies, i.e. web service stuff. So what was wrong?<br />
<span id="more-29"></span><br />
CXF comes with quite a lot of transitive dependencies (that should be declared optional, I&#8217;d say), including some <a href="http://geronimo.apache.org/">Geronimo</a> Spec jars. One of them was org.apache.geronimo.specs:geronimo-javamail_1.4_spec that superseded classes from <a href="http://java.sun.com/products/javamail/">javax.mail:mail</a>. A simple <a href="http://www.google.at/search?q=maven+exclusion">exclusion</a> fixed the problem.</p>
<p>However, I wanted to be prepared for the next time as this tends to happen quite regularly with a growing number of transitive dependencies. There are packages with same content and different id, (e.g. org.mortbay.jetty:servlet-api and javax.servlet:servlet-api), artifacts that contain subsets of other artifacts (e.g. org.springframework:spring contains everything from org.springframework:spring-web), and artifacts that should be replaced in favor of others (e.g. <a href="http://www.slf4j.org/">slf4j</a> instead of <a href="http://commons.apache.org/logging/">commons-logging</a>.</p>
<p>That&#8217;s why I wrote this little bash script:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #000000; font-weight: bold;">for</span> lib <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">find</span> . <span style="color: #660033;">-name</span> <span style="color: #ff0000;">'*.jar'</span><span style="color: #000000; font-weight: bold;">`</span>; <span style="color: #000000; font-weight: bold;">do</span>
<span style="color: #000000; font-weight: bold;">for</span> class <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">unzip</span> <span style="color: #660033;">-l</span> <span style="color: #007800;">$lib</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">egrep</span> <span style="color: #660033;">-o</span> <span style="color: #ff0000;">'[^ ]*.class$'</span><span style="color: #000000; font-weight: bold;">`</span>; <span style="color: #000000; font-weight: bold;">do</span>
<span style="color: #007800;">class</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$class</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> s<span style="color: #000000; font-weight: bold;">/</span>\\.class<span style="color: #000000; font-weight: bold;">//</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> s<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>-.\<span style="color: #000000; font-weight: bold;">/</span>$<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #000000; font-weight: bold;">/</span>_<span style="color: #000000; font-weight: bold;">/</span>g<span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #007800;">existing</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #7a0874; font-weight: bold;">eval</span> <span style="color: #ff0000;">&quot;echo <span style="color: #007800;">$CLS_</span><span style="color: #007800;">${class}</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$existing</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$lib</span> <span style="color: #007800;">$existing</span>&quot;</span>; <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">eval</span> CLS_<span style="color: #800000;">${class}</span>=<span style="color: #ff0000;">&quot;(&quot;</span><span style="color: #800000;">${lib}</span> <span style="color: #800000;">${existing}</span><span style="color: #ff0000;">&quot;)&quot;</span>
<span style="color: #000000; font-weight: bold;">done</span>
<span style="color: #000000; font-weight: bold;">done</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sort</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">uniq</span> <span style="color: #660033;">-c</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sort</span> <span style="color: #660033;">-nr</span></pre></div></div>

<p>These few lines of code print packages and the number of their common classes. Sample output is:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">stf<span style="color: #000000; font-weight: bold;">@</span>crabman:<span style="color: #000000; font-weight: bold;">/</span>path<span style="color: #000000; font-weight: bold;">/</span>to<span style="color: #000000; font-weight: bold;">/</span>my<span style="color: #000000; font-weight: bold;">/</span>webapp$ duplicates.sh
<span style="color: #000000;">127</span> .<span style="color: #000000; font-weight: bold;">/</span>WEB-INF<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>aspectjweaver-1.5.3.jar .<span style="color: #000000; font-weight: bold;">/</span>WEB-INF<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>aspectjrt-1.5.3.jar
<span style="color: #000000;">117</span> .<span style="color: #000000; font-weight: bold;">/</span>WEB-INF<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>spring-2.0.8.jar .<span style="color: #000000; font-weight: bold;">/</span>WEB-INF<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>spring-web-2.0.8.jar
<span style="color: #000000;">83</span> .<span style="color: #000000; font-weight: bold;">/</span>WEB-INF<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>commons-beanutils-1.7.0.jar .<span style="color: #000000; font-weight: bold;">/</span>WEB-INF<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>commons-beanutils-core-1.7.0.jar
<span style="color: #000000;">38</span> .<span style="color: #000000; font-weight: bold;">/</span>WEB-INF<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>servlet-api-<span style="color: #000000;">2.3</span>.jar .<span style="color: #000000; font-weight: bold;">/</span>WEB-INF<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>servlet-api-<span style="color: #000000;">2.5</span>-6.1.11.jar
<span style="color: #000000;">10</span> .<span style="color: #000000; font-weight: bold;">/</span>WEB-INF<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>commons-beanutils-core-1.7.0.jar .<span style="color: #000000; font-weight: bold;">/</span>WEB-INF<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>commons-collections-<span style="color: #000000;">3.2</span>.jar
<span style="color: #000000;">10</span> .<span style="color: #000000; font-weight: bold;">/</span>WEB-INF<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>commons-beanutils-1.7.0.jar .<span style="color: #000000; font-weight: bold;">/</span>WEB-INF<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>commons-beanutils-core-1.7.0.jar .<span style="color: #000000; font-weight: bold;">/</span>WEB-INF<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>commons-collections-<span style="color: #000000;">3.2</span>.jar
<span style="color: #000000;">9</span> .<span style="color: #000000; font-weight: bold;">/</span>WEB-INF<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>spring-2.0.8.jar .<span style="color: #000000; font-weight: bold;">/</span>WEB-INF<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>aopalliance-<span style="color: #000000;">1.0</span>.jar
<span style="color: #000000;">6</span> .<span style="color: #000000; font-weight: bold;">/</span>WEB-INF<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>commons-logging-<span style="color: #000000;">1.1</span>.jar .<span style="color: #000000; font-weight: bold;">/</span>WEB-INF<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>jcl104-over-slf4j-1.5.0.jar</pre></div></div>

<p>The code isn&#8217;t very fast, but makes solving a common classpath problem a lot less painful. (Note that I am using plain unzip instead of jar as didn&#8217;t have a JDK installed on this server/)</p>
]]></content:encoded>
			<wfw:commentRss>http://techblog.molindo.at/2008/10/spotting-duplicate-classes-in-jar-files.html/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

