Search code examples
javamavenvimctags

How to get maven dependency sources and unpack them to a specified directory?


While I combine maven and vim , I can't find a way to download all sources that my project depends to a specified directory and unpack them together.

So that i can generate tags easy.

Does someone know how to ?


Solution

  • You could use the maven-eclipse-plugin plugin to download the sources, and give you a list of the source jars that are available (some of your dependencies might not have sources available).

    The dependency plugin can also download sources, but it's harder to get the list of jars you need.

    You could try something like this:

    dir=target/sources
    mkdir -p $dir
    mvn eclipse:eclipse -DdownloadSources
    sed -rn '/sourcepath/{s/.*sourcepath="M2_REPO.([^"]*).*/\1/;p}' .classpath | \
      (cd $dir && xargs -i jar xf ~/.m2/repository/{})
    

    This runs mvn eclipse:eclipse -DdownloadSources, which will download the sources, and write a .classpath file to the local directory. This file contains the paths to your source jars. It looks a bit like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <classpath>
      <classpathentry kind="src" path="src/main/java" including="**/*.java"/>
      <classpathentry kind="output" path="target/classes"/>
      <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
      <classpathentry kind="var" path="M2_REPO/net/sourceforge/findbugs/jsr305/1.3.7/jsr305-1.3.7.jar"/>
      <classpathentry kind="var" path="M2_REPO/net/jcip/jcip-annotations/1.0/jcip-annotations-1.0.jar" sourcepath="M2_REPO/net/jcip/jcip-annotations/1.0/jcip-annotations-1.0-sources.jar"/>
    </classpath>
    

    In my example, you can see that there are sources for the JCIP annotations jar, but not the FindBugs JSR305 jar.

    The sed command extracts the paths of the source jars (relative to your maven local repository). The xargs command then unpacks each source jar into $dir.

    The eclipse plugin creates the files .classpath and .project and a directory .settings - you can delete these if you never use Eclipse.