NIHVIVO-2774 add a static analysis utility that will allow us to look for unused JARs in the distribution.

This commit is contained in:
j2blake 2011-10-12 20:54:34 +00:00
parent ff91b5a67e
commit fbf2f04d86
4 changed files with 299 additions and 66 deletions

Binary file not shown.

View file

@ -0,0 +1,93 @@
/**
* Copyright 2007 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* This class is created to work around a known bug in JarJar which did not get fixed in release 1.1.
* See the comments in edu.cornell.mannlib.vitro.utilities.jarlist.JarLister
*/
package com.tonicsystems.jarjar;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import com.tonicsystems.jarjar.asm.ClassReader;
import com.tonicsystems.jarjar.ext_util.ClassHeaderReader;
import com.tonicsystems.jarjar.ext_util.ClassPathEntry;
import com.tonicsystems.jarjar.ext_util.ClassPathIterator;
import com.tonicsystems.jarjar.ext_util.RuntimeIOException;
public class KlugedDepFind {
private File curDir = new File(System.getProperty("user.dir"));
public void setCurrentDirectory(File curDir) {
this.curDir = curDir;
}
public void run(String from, String to, DepHandler handler)
throws IOException {
try {
ClassHeaderReader header = new ClassHeaderReader();
Map<String, String> classes = new HashMap<String, String>();
ClassPathIterator cp = new ClassPathIterator(curDir, to, null);
try {
while (cp.hasNext()) {
ClassPathEntry entry = cp.next();
InputStream in = entry.openStream();
try {
header.read(in);
classes.put(header.getClassName(), entry.getSource());
} catch (Exception e) {
System.err.println("Error reading " + entry.getName()
+ ": " + e.getMessage());
} finally {
in.close();
}
}
} finally {
cp.close();
}
handler.handleStart();
cp = new ClassPathIterator(curDir, from, null);
try {
while (cp.hasNext()) {
ClassPathEntry entry = cp.next();
InputStream in = entry.openStream();
try {
new ClassReader(in).accept(new DepFindVisitor(classes,
entry.getSource(), handler),
ClassReader.SKIP_DEBUG
| ClassReader.EXPAND_FRAMES);
} catch (Exception e) {
System.err.println("Error reading " + entry.getName()
+ ": " + e.getMessage());
} finally {
in.close();
}
}
} finally {
cp.close();
}
handler.handleEnd();
} catch (RuntimeIOException e) {
throw (IOException) e.getCause();
}
}
}

View file

@ -0,0 +1,175 @@
/* $This file is distributed unnownder the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.utilities.jarlist;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import com.tonicsystems.jarjar.AbstractDepHandler;
import com.tonicsystems.jarjar.DepHandler;
import com.tonicsystems.jarjar.KlugedDepFind;
/**
* This takes the place of the JarJar main routine, in doing a Find operation.
*
* One thing this lets us do is to call KlugedDepFind instead of DepFind.
* KlugedDepFind was created because JarJar had a known bug that wasn't fixed in
* the latest release. (see http://code.google.com/p/jarjar/issues/detail?id=6).
* I had to put KlugedDepFind into the com.tonicsystems.jarjar package so it
* wauld have access to DepFindVisitor, which is package-private.
*
* The other thing we can do is to provide a custom DepHandler which records the
* dependencies directly instead of writing them to a file which we would need
* to parse. Since we have the dependencies in a data structure, it's easy to
* walk the tree and find out what JARs are required, even through several
* layers of dependency.
*
* When calling this, pass 2 arguments. The first is the path to the JAR which
* contains the Vitro (or VIVO) classes. The second is the path to the directory
* that contains the JARs. (shouldn't end with a slash)
*
* There is a list of JARs which we know we need but which aren't shown by the
* analysis. For example, the MySQL driver is loaded dynamically by name, so an
* analysis of the class files in the JARs won't show that it is used. For now,
* these known dependencies are hard-coded, but it would be nice to read them
* from a data file instead.
*/
public class JarLister {
/**
* <pre>
*
* What I originally wanted to do was this:
*
* <target name="jarlist" depends="jar" description="Figure out what JARs are needed">
* <java classname="com.tonicsystems.jarjar.Main" fork="no" failonerror="true">
* <classpath refid="utility.run.classpath" />
* <arg value="find" />
* <arg value="jar" />
* <arg value="${build.dir}/${ant.project.name}.jar" />
* <arg value="${appbase.dir}/lib/*" />
* </java>
* </target>
*
* I ended up with this instead:
*
* <target name="jarlist" depends="jar" description="Figure out what JARs are needed">
* <java classname="edu.cornell.mannlib.vitro.utilities.jarlist.JarLister" fork="no" failonerror="true">
* <classpath refid="utility.run.classpath" />
* <arg value="${build.dir}/${ant.project.name}.jar" />
* <arg value="${appbase.dir}/lib" />
* </java>
* </target>
*
* </pre>
*/
private static final String[] KNOWN_DEPENDENCIES = {
"mysql-connector-java-5.1.16-bin.jar", "commons-logging-1.1.1.jar" };
private final String topJar;
private final String libDirectory;
private final Map<String, Set<String>> dependencyMap = new HashMap<String, Set<String>>();
private final Set<String> dependencySet = new TreeSet<String>();
public JarLister(String[] args) {
topJar = args[0];
libDirectory = args[1];
}
public void runDepFind() throws IOException {
DepHandler handler = new AbstractDepHandler(DepHandler.LEVEL_JAR) {
@Override
public void handle(String from, String to) {
addToMap(from, to);
}
};
String fullPath = topJar + ":" + libDirectory + "/*";
new KlugedDepFind().run(fullPath, fullPath, handler);
}
private void addToMap(String fromPath, String toPath) {
String fromName = new File(fromPath).getName();
String toName = new File(toPath).getName();
if (!dependencyMap.containsKey(fromName)) {
dependencyMap.put(fromName, new HashSet<String>());
}
dependencyMap.get(fromName).add(toName);
// System.out.println("Adding " + fromName + " ==> " + toName);
}
public void populateDependencySet() {
String topJarName = new File(topJar).getName();
addDependenciesFor(topJarName);
for (String known : KNOWN_DEPENDENCIES) {
dependencySet.add(known);
addDependenciesFor(known);
}
}
private void addDependenciesFor(String name) {
if (!dependencyMap.containsKey(name)) {
return;
}
for (String depend : dependencyMap.get(name)) {
if (!dependencySet.contains(depend)) {
dependencySet.add(depend);
// System.out.println("Depend: " + depend);
addDependenciesFor(depend);
}
}
}
public void dumpDependencySet(PrintWriter w) {
w.println("--------------------");
w.println("Known required JARs");
w.println("--------------------");
for (String d : KNOWN_DEPENDENCIES) {
w.println(" " + d);
}
w.println();
w.println("--------------------");
w.println("Dependent JARs");
w.println("--------------------");
for (String d : dependencySet) {
w.println(" " + d);
}
w.println();
File libDir = new File(libDirectory);
SortedSet<String> unused = new TreeSet<String>(Arrays.asList(libDir
.list()));
unused.removeAll(dependencySet);
w.println("--------------------");
w.println("Unused JARs");
w.println("--------------------");
for (String d : unused) {
w.println(" " + d);
}
w.println();
}
public static void main(String[] args) throws IOException {
JarLister jl = new JarLister(args);
jl.runDepFind();
jl.populateDependencySet();
PrintWriter output = new PrintWriter(System.out, true);
jl.dumpDependencySet(output);
output.close();
}
}

View file

@ -97,22 +97,14 @@ deploy - Deploy the application directly into the Tomcat webapps directory.
<property file="${deploy.properties.file}" />
<fail unless="tomcat.home"
message="${deploy.properties.file} must contain a value for tomcat.home" />
<fail unless="webapp.name"
message="${deploy.properties.file} must contain a value for webapp.name" />
<fail unless="vitro.home.directory"
message="${deploy.properties.file} must contain a value for vitro.home.directory" />
<fail unless="Vitro.defaultNamespace"
message="${deploy.properties.file} must contain a value for Vitro.defaultNamespace" />
<fail unless="VitroConnection.DataSource.url"
message="${deploy.properties.file} must contain a value for VitroConnection.DataSource.url" />
<fail unless="VitroConnection.DataSource.username"
message="${deploy.properties.file} must contain a value for VitroConnection.DataSource.username" />
<fail unless="VitroConnection.DataSource.password"
message="${deploy.properties.file} must contain a value for VitroConnection.DataSource.password" />
<fail unless="rootUser.emailAddress"
message="${deploy.properties.file} must contain a value for rootUser.emailAddress" />
<fail unless="tomcat.home" message="${deploy.properties.file} must contain a value for tomcat.home" />
<fail unless="webapp.name" message="${deploy.properties.file} must contain a value for webapp.name" />
<fail unless="vitro.home.directory" message="${deploy.properties.file} must contain a value for vitro.home.directory" />
<fail unless="Vitro.defaultNamespace" message="${deploy.properties.file} must contain a value for Vitro.defaultNamespace" />
<fail unless="VitroConnection.DataSource.url" message="${deploy.properties.file} must contain a value for VitroConnection.DataSource.url" />
<fail unless="VitroConnection.DataSource.username" message="${deploy.properties.file} must contain a value for VitroConnection.DataSource.username" />
<fail unless="VitroConnection.DataSource.password" message="${deploy.properties.file} must contain a value for VitroConnection.DataSource.password" />
<fail unless="rootUser.emailAddress" message="${deploy.properties.file} must contain a value for rootUser.emailAddress" />
<fail message="The vitro.home.directory &quot;${vitro.home.directory}&quot; does not exist.">
<condition>
@ -138,20 +130,11 @@ deploy - Deploy the application directly into the Tomcat webapps directory.
- - - - - - - - - - - - - - - - - -->
<target name="compileUtilities">
<mkdir dir="${utility.classes.dir}" />
<javac srcdir="${utilities.source.dir}"
destdir="${utility.classes.dir}"
debug="true"
deprecation="${javac.deprecation}"
encoding="UTF8"
includeantruntime="false"
optimize="false"
source="1.6">
<javac srcdir="${utilities.source.dir}" destdir="${utility.classes.dir}" debug="true" deprecation="${javac.deprecation}" encoding="UTF8" includeantruntime="false" optimize="false" source="1.6">
<classpath refid="utility.compile.classpath" />
</javac>
<typedef name="dirDifference"
classname="edu.cornell.mannlib.vitro.utilities.anttasks.DirDifferenceFileSet"
classpathref="utility.run.classpath" />
<typedef name="dirDifference" classname="edu.cornell.mannlib.vitro.utilities.anttasks.DirDifferenceFileSet" classpathref="utility.run.classpath" />
</target>
<!-- - - - - - - - - - - - - - - - - -
@ -180,8 +163,7 @@ deploy - Deploy the application directly into the Tomcat webapps directory.
</copy>
<!-- use the production Log4J properties, unless a debug version exists. -->
<available file="${appbase.dir}/config/debug.log4j.properties"
property="debug.log4j.exists" />
<available file="${appbase.dir}/config/debug.log4j.properties" property="debug.log4j.exists" />
<copy tofile="${war.classes.dir}/log4j.properties" filtering="true" overwrite="true">
<fileset dir="${appbase.dir}/config">
<include name="default.log4j.properties" unless="debug.log4j.exists" />
@ -210,21 +192,11 @@ deploy - Deploy the application directly into the Tomcat webapps directory.
================================= -->
<target name="compile" depends="prepare" description="--> Compile Java sources">
<!-- deletes all files that depend on changed .java files -->
<depend srcdir="${appbase.dir}/src"
destdir="${war.classes.dir}"
closure="false"
cache="${build.dir}/.depcache">
<depend srcdir="${appbase.dir}/src" destdir="${war.classes.dir}" closure="false" cache="${build.dir}/.depcache">
<classpath refid="compile.classpath" />
</depend>
<javac srcdir="${appbase.dir}/src"
destdir="${war.classes.dir}"
debug="true"
deprecation="${javac.deprecation}"
encoding="UTF8"
includeantruntime="false"
optimize="true"
source="1.6">
<javac srcdir="${appbase.dir}/src" destdir="${war.classes.dir}" debug="true" deprecation="${javac.deprecation}" encoding="UTF8" includeantruntime="false" optimize="true" source="1.6">
<classpath refid="compile.classpath" />
</javac>
</target>
@ -233,20 +205,11 @@ deploy - Deploy the application directly into the Tomcat webapps directory.
target: test
================================= -->
<target name="test" depends="compile" unless="skiptests" description="--> Run JUnit tests">
<javac srcdir="${appbase.dir}/test"
destdir="${test.classes.dir}"
debug="true"
deprecation="${javac.deprecation}"
encoding="UTF8"
includeantruntime="false"
optimize="false"
source="1.6">
<javac srcdir="${appbase.dir}/test" destdir="${test.classes.dir}" debug="true" deprecation="${javac.deprecation}" encoding="UTF8" includeantruntime="false" optimize="false" source="1.6">
<classpath refid="test.compile.classpath" />
</javac>
<java classname="edu.cornell.mannlib.vitro.utilities.testing.VitroTestRunner"
fork="yes"
failonerror="true">
<java classname="edu.cornell.mannlib.vitro.utilities.testing.VitroTestRunner" fork="yes" failonerror="true">
<classpath refid="test.run.classpath" />
<arg file="${appbase.dir}/test" />
<arg value="${testlevel}" />
@ -263,10 +226,7 @@ deploy - Deploy the application directly into the Tomcat webapps directory.
<!-- =================================
target: revisionInfo
================================= -->
<target name="revisionInfo"
depends="test"
unless="skipinfo"
description="--> Store revision info in build">
<target name="revisionInfo" depends="test" unless="skipinfo" description="--> Store revision info in build">
<tstamp>
<format property="revisionInfo.timestamp" pattern="yyyy-MM-dd HH:mm:ss" />
</tstamp>
@ -282,8 +242,7 @@ deploy - Deploy the application directly into the Tomcat webapps directory.
<target name="prepareSolr" depends="properties">
<property name="solr.distrib.dir" location="${corebase.dir}/../solr" />
<property name="solr.example.dir" location="${solr.distrib.dir}/exampleSolr" />
<property name="solr.context.config.example"
location="${solr.distrib.dir}/exampleSolrContext.xml" />
<property name="solr.context.config.example" location="${solr.distrib.dir}/exampleSolrContext.xml" />
<property name="solr.war" location="${solr.distrib.dir}/apache-solr-3.1.0.war" />
<property name="solr.docbase" location="${solr.home.dir}/solr.war" />
@ -329,9 +288,7 @@ deploy - Deploy the application directly into the Tomcat webapps directory.
<!-- =================================
target: deploy
================================= -->
<target name="deploy"
depends="revisionInfo, deploySolr"
description="--> Build the app and install in Tomcat">
<target name="deploy" depends="revisionInfo, deploySolr" description="--> Build the app and install in Tomcat">
<property name="webapp.deploy.home" value="${tomcat.home}/webapps/${webapp.name}" />
<mkdir dir="${webapp.deploy.home}" />
@ -357,11 +314,21 @@ deploy - Deploy the application directly into the Tomcat webapps directory.
At release time, applies license text to source files.
================================= -->
<target name="licenser" description="--> Check source files for licensing tags">
<property name="licenser.properties.file"
location="${corebase.dir}/config/licenser/licenser.properties" />
<property name="licenser.properties.file" location="${corebase.dir}/config/licenser/licenser.properties" />
<runLicenserScript productname="Vitro core" propertiesfile="${licenser.properties.file}" />
</target>
<!-- =================================
target: jarlist
================================= -->
<target name="jarlist" depends="jar" description="Figure out what JARs are not needed">
<java classname="edu.cornell.mannlib.vitro.utilities.jarlist.JarLister" fork="no" failonerror="true">
<classpath refid="utility.run.classpath" />
<arg value="${build.dir}/${ant.project.name}.jar" />
<arg value="${appbase.dir}/lib" />
</java>
</target>
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
MACROS
@ -391,9 +358,7 @@ deploy - Deploy the application directly into the Tomcat webapps directory.
<attribute name="productName" />
<attribute name="productCheckoutDir" />
<sequential>
<java classname="edu.cornell.mannlib.vitro.utilities.revisioninfo.RevisionInfoBuilder"
fork="no"
failonerror="true">
<java classname="edu.cornell.mannlib.vitro.utilities.revisioninfo.RevisionInfoBuilder" fork="no" failonerror="true">
<classpath refid="utility.run.classpath" />
<arg value="@{productName}" />
<arg file="@{productCheckoutDir}" />