Section 6.9. Adding Dynamic Dependencies


6.9. Adding Dynamic Dependencies

Maven dependency handling supports only static dependencies defined in project.xml. However, there are times when a plug-in will need to add a dependency at runtime.

6.9.1. How do I do that?

Consider your favorite plug-inLogifier. The way you wrote it in the previous lab has a severe limitation. Imagine that you're running it on a Maven project that has unit tests. As you know, running the jar goal will automatically execute the test:test goal and run the unit tests. As your logifier:logify goal depends on the jar goal, it'll end up running the unit tests on the "logified" code... Yikes! This means that the test will fail, with this error:

java.lang.NoClassDefFoundError: org/aspectj/lang/Signature

Not only is this error message unexpected to someone not familiar with the Logifier plug-in, but also you have a plug-in causing problems in an unrelated plug-intest. In order to fix this, you need to rewrite the JAR plug-in so that it doesn't execute tests, or tell it to skip the tests. Or better yet, add the aspectjrt JAR to the classpath at runtime. This sounds like the best solution.

You can add a dependency dynamically by using the addPath tag provided by Maven:

  <goal name="logifier:init"> [...]     <ant:path >         <ant:pathelement              path="${plugin.getDependencyPath('aspectj:aspectjrt')}"/>     </ant:path>     <maven:addPath           ref/>       </goal>

When your project executes, the logifier:init goal will be called and the aspectjrt artifact will be added to the maven.dependency.classpath before any other goals are executed. You can see in the goal definition that you are using ${plugin.getDependencyPath('aspectj:aspectjrt')} to retrieve the path to this JAR file, and then you are adding it to the classpath with the maven:addPath tag which takes the id of the path to alter and the refid of the path to add.

6.9.2. What just happened?

You have taken the first version of the Logifier plug-in and transformed it into a more intelligent plug-in that now supports generating debug logs when the project unit tests are executed. You did this by making the runtime AspectJ dependency a dynamic dependency added during the initialization of the Logifier plug-in. Now you are ready to tackle the next challenge of the Logifier plug-in: adding a report to the project web site.



Maven. A Developer's Notebook
Maven: A Developers Notebook (Developers Notebooks)
ISBN: 0596007507
EAN: 2147483647
Year: 2003
Pages: 125

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net