4

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 ?

jackalope
  • 1,554
  • 3
  • 17
  • 37
  • 2
    Maybe [this answer](http://stackoverflow.com/questions/2059431/get-source-jars-from-maven-repository) can help to download sources. The sources will be places in the default maven repository (~/.m2/repository/). – Stephan Jan 18 '13 at 08:18

1 Answers1

5

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.

Martin Ellis
  • 9,603
  • 42
  • 53
  • but i'm using maven+vim, no ide. – jackalope Jan 18 '13 at 11:32
  • 1
    I understand that. The answer I've given doesn't require Eclipse - maven-eclipse-plugin is just a maven plugin that writes out a few files that Eclipse can read. Please give the commands I gave a try - you don't need Eclipse to do so. – Martin Ellis Jan 18 '13 at 12:03
  • 1
    amazing, exactly what i was looking for, i would add on the end of the command `ctags -R --languages=java .` so that code navigation would work with ctrl + ] – Mitermayer Reis Sep 09 '13 at 13:39