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.
How do I do that?
Consider your favorite plug-in—Logifier. 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-in—test. 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 id="aspectjrt.classpath">
<ant:pathelement
path="${plugin.getDependencyPath('aspectj:aspectjrt')}"/>
</ant:path>
<maven:addPath id="maven.dependency.classpath"
refid="aspectjrt.classpath"/>
</goal>When your project executes, the logifier:init goal will be called ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access