Discussion:
Write dependency information to a file (new custom plugin)
Danny Schimke
2013-01-16 19:33:46 UTC
Permalink
Hi,

for 3 years now I have no more experience with Maven. But today I got a
challenge. I have to resolve the projects dependencies and write those into
a file. There already a template file exists with placeholders where to
replace with the dependencies. Finally the file will be copied to a
specified directory. The output should be plain text as well XML depending
upon the POMs configuration. It should be possible to configure in the pom
which artifacts are written/captured. Important is to gather the version of
depending artifacts in a file.

Entire thoughts were to write a new maven plugin. But then I need to call
the dependency:resolve plugin within my plugin and use its result to
achieve my goal. How can I do this?

Are there other possible and good solutions to do it (maybe using Groovy
script and call "mvn" on command line)?

Thanks a lot!
--
Mit freundlichen Grüßen / Kind regards
-Danny Schimke*


Place To Remember* (Android- App)
goo.gl/M9OGs - *Free*
goo.gl/A8bZ3 - *Donate*

Viel Spaß und vielen Dank!
Robert Scholte
2013-01-16 22:02:13 UTC
Permalink
Hi,

Use the dependencies-report[1] as inspiration.
This will create a HTML page looking something like this[2]
The source-repository page[3] will explain how you can get the sources.

Robert

[1]
http://maven.apache.org/plugins/maven-project-info-reports-plugin/dependencies-mojo.html
[2]
http://maven.apache.org/plugins/maven-project-info-reports-plugin/dependencies.html
[3]
http://maven.apache.org/plugins/maven-project-info-reports-plugin/source-repository.html


Op Wed, 16 Jan 2013 20:33:46 +0100 schreef Danny Schimke
Post by Danny Schimke
Hi,
for 3 years now I have no more experience with Maven. But today I got a
challenge. I have to resolve the projects dependencies and write those into
a file. There already a template file exists with placeholders where to
replace with the dependencies. Finally the file will be copied to a
specified directory. The output should be plain text as well XML depending
upon the POMs configuration. It should be possible to configure in the pom
which artifacts are written/captured. Important is to gather the version of
depending artifacts in a file.
Entire thoughts were to write a new maven plugin. But then I need to call
the dependency:resolve plugin within my plugin and use its result to
achieve my goal. How can I do this?
Are there other possible and good solutions to do it (maybe using Groovy
script and call "mvn" on command line)?
Thanks a lot!
Curtis Rueden
2013-01-16 22:32:01 UTC
Permalink
Hi Danny,

Does it *have* to be done from within the Maven build infrastructure? That
is, do you really need it to be a Maven goal? Because this problem sounds
much, much easier to achieve with a shell script.

Start with:
mvn dependency:list
or
mvn dependency:tree

And massage the result using GNU tools (sed, grep, etc.) to do what you
want.

E.g., for plain text, something like:
mvn dependency:list \
| grep '^\[INFO\] \w' \
| sed 's/^\[INFO\] *//' \
dependencies.txt
Or for XML, something like ("-E" flag on OS X only):
mvn dependency:list \
| grep '^\[INFO\] \w' \
| sed -E 's/^\[INFO\]
*([^:]*):([^:]*):([^:]*):([^:]*):([^:]*)/<dependency><groupId>\1<\/groupId><artifactId>\2<\/artifactId><packaging>\3<\/packaging><version>\4<\/version><scope>\5<\/scope><\/dependency>/'

I am not a shell scripting ninja so there are surely tons of
shorter/better/cuter ways of doing it, but you get the idea.

Regards,
Curtis
Hi,
for 3 years now I have no more experience with Maven. But today I got a
challenge. I have to resolve the projects dependencies and write those into
a file. There already a template file exists with placeholders where to
replace with the dependencies. Finally the file will be copied to a
specified directory. The output should be plain text as well XML depending
upon the POMs configuration. It should be possible to configure in the pom
which artifacts are written/captured. Important is to gather the version of
depending artifacts in a file.
Entire thoughts were to write a new maven plugin. But then I need to call
the dependency:resolve plugin within my plugin and use its result to
achieve my goal. How can I do this?
Are there other possible and good solutions to do it (maybe using Groovy
script and call "mvn" on command line)?
Thanks a lot!
--
Mit freundlichen Grüßen / Kind regards
-Danny Schimke*
Place To Remember* (Android- App)
goo.gl/M9OGs - *Free*
goo.gl/A8bZ3 - *Donate*
Viel Spaß und vielen Dank!
Danny Schimke
2013-01-17 06:58:15 UTC
Permalink
Thank you very much,

@Robert Scholte: I'll have a look at this. Maybe it already does what I
want and I can take some stuff into my possibly new plugin.

@Curtis Rueden: It should be triggered within maven on a specified goal. It
must be possible to execute it at a desired point in the Maven build
lifecycle. But it does not have to be a Maven plugin necessarily. Are there
(standard) plugins to trigger some command line tools?

Thank you very much!
--
Mit freundlichen Grüßen / Kind regards
-Danny Schimke*
*
Dirk Olmes
2013-01-17 10:40:23 UTC
Permalink
Post by Danny Schimke
Hi,
for 3 years now I have no more experience with Maven. But today I got a
challenge. I have to resolve the projects dependencies and write those into
a file. There already a template file exists with placeholders where to
replace with the dependencies.
I had to solve a similar challenge. The maven-dependency-plugin's
properties goal comes in handy - it adds the dependencies as system
properties.

I'm using a custom maven plugin to generate the template but I guess an
ant task will do the job, too.

-dirk
Danny Schimke
2013-01-17 12:59:20 UTC
Permalink
Hi Dirk,

I read your answer. Thanks for that. Can you please explain more detailed,
what you've done to solve the problem? Do I understand correctly that the
maven-dependency-plugin offers properties that I can use in my own plugin?
How can I access them and what do I need to do that they get filled?

We try don't using ANT as far as it is possible. I just started to write my
own, first Maven plugin but I am still not able to get the dependencies.

Thank you very much!
--
Mit freundlichen Grüßen / Kind regards
-Danny Schimke*
*
Dirk Olmes
2013-01-17 15:26:48 UTC
Permalink
Post by Danny Schimke
Hi Dirk,
I read your answer. Thanks for that. Can you please explain more detailed,
what you've done to solve the problem? Do I understand correctly that the
maven-dependency-plugin offers properties that I can use in my own plugin?
Yes, that's correct. This is what I'm talking about:
http://maven.apache.org/plugins/maven-dependency-plugin/properties-mojo.html
Post by Danny Schimke
How can I access them and what do I need to do that they get filled?
The properties are "just there" after the maven-dependency-plugin was
executed. Now you only need a plugin that has access to the MavenProject
instance. This can either be a custom plugin - you request injection of
a MavenProject into your custom mojo, that's standard Maven plugin
coding practice. Alternatively you might use the groovy maven plugin as
described here:
http://docs.codehaus.org/display/GMAVEN/Executing+Groovy+Code

Note that your groovy scripts will have access to the MavenProject
instance that has the properties.

-dirk
Danny Schimke
2013-01-17 16:09:28 UTC
Permalink
Hi Dirk,

thank you very much. Yes, it works, I figured out, but something very
important is missing here: the properties does not contain the version
number of the artifact. My coworker said I should not use Groovy - I am
with him, this should work out of the box without being dependent on a
further technology.

Maybe the simplest way is to call the maven-dependency-plugins "resolve"
goal, write it into a file and work on that. But then I always have to call
the "resolve" goal explicit if I want to use my new plugin (or I use
https://github.com/TimMoore/mojo-executor to call the resolve from my
plugin)

On the other hand I could try using ANT, but I dont like this.

Thanks!!
--
Mit freundlichen Grüßen / Kind regards
-Danny Schimke*
*
Dirk Olmes
2013-01-18 06:44:36 UTC
Permalink
Post by Danny Schimke
Hi Dirk,
thank you very much. Yes, it works, I figured out, but something very
important is missing here: the properties does not contain the version
number of the artifact.
Well the property does not but the value points to an absolute path of
the artifact in the local repository, which includes the version.

You did not say much about your use case - for me, the full path was all
I needed. See https://github.com/dirk-olmes/dependency-plugin-demo for a
demo that shows the properties and the values.
Post by Danny Schimke
My coworker said I should not use Groovy - I am
with him, this should work out of the box without being dependent on a
further technology.
Well yes it should. But it doesn't so you can now either give up or use
the groovy plugin as "yet another plugin in the build".

-dirk

Loading...