List of dependency jar files in Maven
Asked Answered
F

9

117

Using Maven 2, is there a way I can list out the jar dependencies as just the file names?

mvn dependency:build-classpath 

can list the jar files, but that will include the full path to their location in my local repository. What I need is essentially just a list of the file names (or the file names that the copy-dependencies goal copied).

So the list I need would be something like

activation-1.1.jar,antlr-2.7.6.jar,aopalliance-1.0.jar etc...

ideally as a maven property, but I guess, a file such as build-classpath can generate will do.

What I am trying to achieve is writing a Bundle-ClassPath to an otherwise manually maintained MANIFEST.MF file for a OSGi bundle. (You shouldn't need to understand this bit to answer the question.)

To clarify: The question is not about how to write manifest headers into the MANIFEST.MF file in a jar (that is easily googleble). I am asking about how to get the data I want to write, namely the list shown above.

Fungistat answered 10/11, 2008 at 17:36 Comment(1)
just submit a patch for the dependency plugin and i'll apply it to the next release. The build classpath is the closest thing to what you want, we just need to tweak the options to get the desired output.Solly
M
131

This command will generate the dependencies tree of your maven project:

$ mvn dependency:tree

I am sure that you will like the result :-)

Marrs answered 14/5, 2010 at 13:9 Comment(4)
great. I was searching a lot to get list of all maven dependenciesProcessional
why on earth isn't this shown in the mvn -h info? It doesn't mention dependencies at all...Brainy
I think the help output is just for the mvn binary, but not for the documentation of available goals. Would be nice to have a list of available goals/tasks beside the possibility to use autocompletion (manual activation is neede for this). #3997302Anemic
"I am sure that you will like the result :-)" - not so much if the output size is Big Data..Vino
D
34

Actually, for just the final list of jars, simply use

mvn dependency:list

Which is lot more simple than dependency:tree which is an overkill to simply get the final list as it shows detailed transitive tree and conflict resolution (with verbose).

Here's the doc for additional parameters

Distort answered 30/3, 2018 at 18:52 Comment(1)
And if you want them sorted: mvn dependency:list -Dsort=trueMazuma
T
22

As best as I can tell, you can't get exactly that output, with the commas and no spaces. Both via the command line and via the pom.xml file, the maven-dependency-plugin or the CLI freaks out if you specify spaces or the '' (empty string) as a substitute with either pathSeparator or fileSeparator. So, you may be forced to reach something of a compromise. You can

    mvn dependency:build-classpath -Dmdep.pathSeparator=":" -Dmdep.prefix='' -Dmdep.fileSeparator=":" -Dmdep.outputFile=classpath

However, that should get you a full list, separated by '::' instead of just ',', but it works. If you run:

    mvn dependency:build-classpath -Dmdep.pathSeparator="@REPLACEWITHCOMMA" -Dmdep.prefix='' -Dmdep.fileSeparator="@" -Dmdep.outputFile=classpath

and attach this to the generate-resources phase and filter that resource later by setting the correct property in the process-resources phase of the lifecycle, you should be able to get just the comma.

You can see the full list of options at: http://maven.apache.org/plugins/maven-dependency-plugin/build-classpath-mojo.html

Thetos answered 2/4, 2009 at 6:6 Comment(1)
Great answer! If you use -Dmdep.pathSeparator=',' -Dmdep.prefix='.' -Dmdep.fileSeparator='/' you get a comma-separated list of relative file names a la ./file1.jar,./file2.jar which then avoids the need for filtering.Brahe
P
21

Here's the command you're asking for

$ mvn dependency:tree

For large projects it can output a lot of text. I assume that you want to check that dependency tree contains a certain dependency, so you don't need a full list.

Here's how you can filter output on Windows:

$ mvn dependency:tree | findstr javax.persistence

And here's how you can do it on Linux:

$ mvn dependency:tree | grep javax.persistence

Maven way to filter the dependency tree (works in Windows cmd, MacOS and Linux shell):

$ mvn dependency:tree -Dincludes=javax.persistence:*

Maven way (Windows PowerShell):

$ mvn dependency:tree '-Dincludes=javax.persistence:*'
Planchette answered 24/11, 2017 at 13:50 Comment(0)
C
4

Have you looked at the Apache Felix project? It has a whole mess of plugins, including a bundle plugin that should do what you want.

Also, have you tried the <addClasspath> tag with <manifestFile>? That should have the desired effect of merging the classpath into your manifest.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  ...
  <configuration>
    <archive>
      <addClasspath>true</addClasspath>
      <manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
    </archive>
  </configuration>
  ...
</plugin>
Churn answered 10/11, 2008 at 17:45 Comment(3)
I'm aware of Apache Felix, but that's not doing what I need, which is the reason I'm doing this manually.Fungistat
addClasspath will add a list of jar's to the Class-Path manifest header, I don't see maven-jar-plugin docs mention being able to put the class path into other headers. Also the Bundle-Classpath needs more than just a list of jars.Fungistat
links to Apache Felix are broken 💀Planchette
C
3

I may be missing something here, but as you've already used copy-dependencies it sounds like what you're really after is just a list of files in a specified directory.

Ant can do this for you without any problems, as can a shell script.

Coprophagous answered 9/1, 2010 at 1:5 Comment(1)
I agree, I just did it.Delirious
I
2

Maven can build the classpath in your manifest automatically: http://maven.apache.org/guides/mini/guide-manifest.html

It's a configuration of the Maven archive plugin.

Inflatable answered 10/11, 2008 at 17:44 Comment(0)
F
1

To add a notch to the existing answers, the current maven-dependency-plugin allows saving the classpath to a property with the outputProperty parameter.

Ferrand answered 16/8, 2013 at 0:24 Comment(0)
D
1

Here is an awk script to pipe mvn dependency:list:

mvn dependency:list | awk -f mvnfmt.awk

You can | sort if you want to sort by name, or | tr '\n' ':' to format it to a classpath.

mvnfmt.awk is:

BEGIN {
    found = 0
}

/The following files have been resolved/ {
    found = 1
    next
}

/^\[INFO\] \$/ {
    print "Empty " found
    if (found != 0) found = 0
}

{
    if (!found) next
    n = split($0, a, " ")
    if (n != 2) {
        found = 0
        next
    }
    split(a[2], a, ":")
    print a[2] "-" a[4] "." a[3]
}   
Delirious answered 2/9, 2019 at 11:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.