How to Determine which JARs are Used in an Application

前端 未结 4 1651
傲寒
傲寒 2021-02-04 06:07

An existing application has a ton of JAR files in its classpath. Someone must have added all JARs initially just to be sure. Some of the JARs were obviously not being used and w

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-04 06:26

    I used the following shell script in a jboss portal project to get the list of jar files that are used in import statements. This will of yourse only work for direct dependencies, not for dynamically loaded or even when the fully qualified classname was used in the source. Furthermore, all jar files and their transitive dependencies are provided by the container so they are only needed to compile the code.

    The goal was to create a maven pom for the project and to find the files that needed to be deployed to our nexus repository manager. It might be useful as a starting point to list the files that are definitely needed, the remaining jar files would have to be checked in other ways. If the jar is also available in a maven repository you might look at its dependencies for example.

    #!/bin/sh
    JBOSS_HOME=/path/to/jboss/installation
    JBOSS_LIB=$JBOSS_HOME/server/default/lib
    JBOSS_DEPLOY=$JBOSS_HOME/server/default/deploy
    SRC_DIR=src
    
    for f in $JBOSS_LIB/*.jar $JBOSS_DEPLOY/jboss-portal.sar/lib/*.jar $JBOSS_DEPLOY/jboss-portal.sar/portal-cms.sar/lib/*.jar $JBOSS_DEPLOY/ejb3.deployer/*.jar
    do
        for c in `jar -tf $f | tr '/$' '..'`
        do
            #echo "^import ${c%.class};"
            if `grep "^import ${c%.class};" -h -r $SRC_DIR -q`
            then
                echo $f $c
            fi
        done
    done
    

提交回复
热议问题