This is a bad idea and don't count on Maven 3.x supporting this because it makes determining what the dependencies are black magic. The dependency tree and visualization tools won't work. In Maven 3.x we are likely to make the dependency set immutable post resolution phase.

I don't feel there is any valid use case for programatically adding dependencies and if someone finds one then something is wrong with Maven itself.

On 26-Jan-09, at 4:39 AM, Costin Caraivan wrote:



Maarten Storm-2 wrote:

Hello all,
I would like to add programmatically dependencies to the MavenProject.
I have looked in the mail archives but I could not find info on this
topic.Is there a possible solution for this?
Kind regards,
Maarten Storm

Hello,

I'm also interested by this and I found this example:
http://www.eclipse.org/articles/article.php?file=Article-Eclipse-and-Maven2/index.html

However, when I do it, I get a NullPointerException later on with this code:
currentArtifact = artifactFactory.createBuildArtifact(                          
                
currentDependency.getGroupId(),  currentDependency.getArtifactId(),
currentDependency.getVersion(), currentDependency.getType() );
                                                        
// Add the jar to the dependency list.
getProject().getDependencies().add(currentDependency);
                                                        
// Get the artifact from the remote repository if it is not available
locally.
artifactResolver.resolve(currentArtifact,
getProject().getRemoteArtifactRepositories(), localRepository);

=>
[ERROR] FATAL ERROR
[INFO]
------------------------------------------------------------------------
[INFO] null
[INFO]
------------------------------------------------------------------------
[INFO] Trace
java.lang.NullPointerException
        at
org .apache .maven .project .artifact .MavenMetadataSource.createArtifacts(MavenMetadataSource.java:327)
        at
org .apache.maven.project.MavenProject.createArtifacts(MavenProject.java: 1577)
        at
org .apache .maven .plugin .DefaultPluginManager .resolveTransitiveDependencies(DefaultPluginManager.java:1409)
        at
org .apache .maven .plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java: 405)
        at
org .apache .maven .lifecycle .DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java: 558)
        at
org .apache .maven .lifecycle .DefaultLifecycleExecutor .executeGoalWithLifecycle(DefaultLifecycleExecutor.java:499)
        at
org .apache .maven .lifecycle .DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java: 478)
        at
org .apache .maven .lifecycle .DefaultLifecycleExecutor .executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:330)
        at
org .apache .maven .lifecycle .DefaultLifecycleExecutor .executeTaskSegments(DefaultLifecycleExecutor.java:291)
        at
org .apache .maven .lifecycle .DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:142)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:336)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:129)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:287)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at
sun .reflect .NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at
sun .reflect .DelegatingMethodAccessorImpl .invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:585)
at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java: 315)
        at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java: 430)
        at org.codehaus.classworlds.Launcher.main(Launcher.java:375)

BTW, some things are undocumented, like how to get access to artifactFactory or artifactResolver - I had to Google this page up to be able to write that
code: http://docs.codehaus.org/display/MAVENUSER/Mojo+Developer+Cookbook

Thanks for any tips.

Regards,
Costin.
--
View this message in context: 
http://www.nabble.com/Programmatically-adding-dependencies-to-a-MavenProject-tp21173440p21664740.html
Sent from the Maven Developers mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@maven.apache.org
For additional commands, e-mail: dev-h...@maven.apache.org


Thanks,

Jason

----------------------------------------------------------
Jason van Zyl
Founder,  Apache Maven
jason at sonatype dot com
----------------------------------------------------------

A language that doesn’t affect the way you think about programming is not worth knowing.

 -— Alan Perlis


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@maven.apache.org
For additional commands, e-mail: dev-h...@maven.apache.org

Reply via email to