KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > maven > MavenEmbedder


1 /*
2  * Copyright 2001-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package hudson.maven;
17
18 import org.apache.maven.BuildFailureException;
19 import org.apache.maven.artifact.Artifact;
20 import org.apache.maven.artifact.factory.ArtifactFactory;
21 import org.apache.maven.artifact.manager.WagonManager;
22 import org.apache.maven.artifact.repository.ArtifactRepository;
23 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
24 import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
25 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
26 import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
27 import org.apache.maven.artifact.resolver.ArtifactResolutionException;
28 import org.apache.maven.artifact.resolver.ArtifactResolver;
29 import org.apache.maven.embedder.MavenEmbedderException;
30 import org.apache.maven.embedder.MavenEmbedderLogger;
31 import org.apache.maven.embedder.MavenEmbedderLoggerManager;
32 import org.apache.maven.embedder.PlexusLoggerAdapter;
33 import org.apache.maven.embedder.SummaryPluginDescriptor;
34 import org.apache.maven.execution.MavenSession;
35 import org.apache.maven.execution.ReactorManager;
36 import org.apache.maven.lifecycle.LifecycleExecutionException;
37 import org.apache.maven.lifecycle.LifecycleExecutor;
38 import org.apache.maven.model.Model;
39 import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
40 import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
41 import org.apache.maven.monitor.event.DefaultEventDispatcher;
42 import org.apache.maven.monitor.event.EventDispatcher;
43 import org.apache.maven.monitor.event.EventMonitor;
44 import org.apache.maven.plugin.MojoExecutionException;
45 import org.apache.maven.plugin.descriptor.PluginDescriptor;
46 import org.apache.maven.plugin.descriptor.PluginDescriptorBuilder;
47 import org.apache.maven.profiles.DefaultProfileManager;
48 import org.apache.maven.profiles.ProfileManager;
49 import org.apache.maven.project.DuplicateProjectException;
50 import org.apache.maven.project.MavenProject;
51 import org.apache.maven.project.MavenProjectBuilder;
52 import org.apache.maven.project.ProjectBuildingException;
53 import org.apache.maven.settings.MavenSettingsBuilder;
54 import org.apache.maven.settings.RuntimeInfo;
55 import org.apache.maven.settings.Settings;
56 import org.apache.maven.wagon.events.TransferListener;
57 import org.codehaus.classworlds.ClassWorld;
58 import org.codehaus.classworlds.DuplicateRealmException;
59 import org.codehaus.plexus.PlexusContainer;
60 import org.codehaus.plexus.PlexusContainerException;
61 import org.codehaus.plexus.component.repository.ComponentDescriptor;
62 import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException;
63 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
64 import org.codehaus.plexus.configuration.PlexusConfiguration;
65 import org.codehaus.plexus.configuration.PlexusConfigurationException;
66 import org.codehaus.plexus.embed.Embedder;
67 import org.codehaus.plexus.util.DirectoryScanner;
68 import org.codehaus.plexus.util.dag.CycleDetectedException;
69 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
70
71 import java.io.File JavaDoc;
72 import java.io.FileNotFoundException JavaDoc;
73 import java.io.FileReader JavaDoc;
74 import java.io.IOException JavaDoc;
75 import java.io.InputStream JavaDoc;
76 import java.io.InputStreamReader JavaDoc;
77 import java.io.Writer JavaDoc;
78 import java.net.URL JavaDoc;
79 import java.util.ArrayList JavaDoc;
80 import java.util.Collections JavaDoc;
81 import java.util.Date JavaDoc;
82 import java.util.Iterator JavaDoc;
83 import java.util.List JavaDoc;
84 import java.util.Properties JavaDoc;
85
86 /**
87  * Class intended to be used by clients who wish to embed Maven into their applications
88  *
89  * @author <a HREF="mailto:jason@maven.org">Jason van Zyl</a>
90  */

91 class MavenEmbedder
92 {
93     public static final String JavaDoc userHome = System.getProperty( "user.home" );
94
95     // ----------------------------------------------------------------------
96
// Embedder
97
// ----------------------------------------------------------------------
98

99     private Embedder embedder;
100
101     // ----------------------------------------------------------------------
102
// Components
103
// ----------------------------------------------------------------------
104

105     private MavenProjectBuilder mavenProjectBuilder;
106
107     private ArtifactRepositoryFactory artifactRepositoryFactory;
108
109     private MavenSettingsBuilder settingsBuilder;
110
111     private LifecycleExecutor lifecycleExecutor;
112
113     private WagonManager wagonManager;
114
115     private MavenXpp3Reader modelReader;
116
117     private MavenXpp3Writer modelWriter;
118
119     private ProfileManager profileManager;
120
121     private PluginDescriptorBuilder pluginDescriptorBuilder;
122
123     private ArtifactFactory artifactFactory;
124
125     private ArtifactResolver artifactResolver;
126
127     private ArtifactRepositoryLayout defaultArtifactRepositoryLayout;
128
129     // ----------------------------------------------------------------------
130
// Configuration
131
// ----------------------------------------------------------------------
132

133     private Settings settings;
134
135     private ArtifactRepository localRepository;
136
137     private File JavaDoc localRepositoryDirectory;
138
139     private ClassLoader JavaDoc classLoader;
140
141     private MavenEmbedderLogger logger;
142
143     // ----------------------------------------------------------------------
144
// User options
145
// ----------------------------------------------------------------------
146

147     // release plugin uses this but in IDE there will probably always be some form of interaction.
148
private boolean interactiveMode;
149
150     private boolean offline;
151
152     private String JavaDoc globalChecksumPolicy;
153
154     /**
155      * This option determines whether the embedder is to be aligned to the user
156      * installation.
157      */

158     private boolean alignWithUserInstallation;
159
160     // ----------------------------------------------------------------------
161
// Accessors
162
// ----------------------------------------------------------------------
163

164     public void setInteractiveMode( boolean interactiveMode )
165     {
166         this.interactiveMode = interactiveMode;
167     }
168
169     public boolean isInteractiveMode()
170     {
171         return interactiveMode;
172     }
173
174     public void setOffline( boolean offline )
175     {
176         this.offline = offline;
177     }
178
179     public boolean isOffline()
180     {
181         return offline;
182     }
183
184     public void setGlobalChecksumPolicy( String JavaDoc globalChecksumPolicy )
185     {
186         this.globalChecksumPolicy = globalChecksumPolicy;
187     }
188
189     public String JavaDoc getGlobalChecksumPolicy()
190     {
191         return globalChecksumPolicy;
192     }
193
194     public boolean isAlignWithUserInstallation()
195     {
196         return alignWithUserInstallation;
197     }
198
199     public void setAlignWithUserInstallation( boolean alignWithUserInstallation )
200     {
201         this.alignWithUserInstallation = alignWithUserInstallation;
202     }
203
204     /**
205      * Set the classloader to use with the maven embedder.
206      *
207      * @param classLoader
208      */

209     public void setClassLoader( ClassLoader JavaDoc classLoader )
210     {
211         this.classLoader = classLoader;
212     }
213
214     public ClassLoader JavaDoc getClassLoader()
215     {
216         return classLoader;
217     }
218
219     public void setLocalRepositoryDirectory( File JavaDoc localRepositoryDirectory )
220     {
221         this.localRepositoryDirectory = localRepositoryDirectory;
222     }
223
224     public File JavaDoc getLocalRepositoryDirectory()
225     {
226         return localRepositoryDirectory;
227     }
228
229     public ArtifactRepository getLocalRepository()
230     {
231         return localRepository;
232     }
233
234     public MavenEmbedderLogger getLogger()
235     {
236         return logger;
237     }
238
239     public void setLogger( MavenEmbedderLogger logger )
240     {
241         this.logger = logger;
242     }
243
244     // ----------------------------------------------------------------------
245
// Embedder Client Contract
246
// ----------------------------------------------------------------------
247

248     // ----------------------------------------------------------------------
249
// Model
250
// ----------------------------------------------------------------------
251

252     public Model readModel( File JavaDoc model )
253         throws XmlPullParserException, FileNotFoundException JavaDoc, IOException JavaDoc {
254         return modelReader.read( new FileReader JavaDoc( model ) );
255     }
256
257     public void writeModel( Writer writer, Model model )
258         throws IOException JavaDoc
259     {
260         modelWriter.write( writer, model );
261     }
262
263     // ----------------------------------------------------------------------
264
// Project
265
// ----------------------------------------------------------------------
266

267     public MavenProject readProject( File JavaDoc mavenProject )
268         throws ProjectBuildingException {
269         return mavenProjectBuilder.build( mavenProject, localRepository, profileManager );
270     }
271
272     public MavenProject readProjectWithDependencies( File JavaDoc mavenProject, TransferListener transferListener )
273         throws ProjectBuildingException, ArtifactResolutionException, ArtifactNotFoundException {
274         return mavenProjectBuilder.buildWithDependencies( mavenProject, localRepository, profileManager, transferListener );
275     }
276
277     public MavenProject readProjectWithDependencies( File JavaDoc mavenProject )
278         throws ProjectBuildingException, ArtifactResolutionException, ArtifactNotFoundException
279     {
280         return mavenProjectBuilder.buildWithDependencies( mavenProject, localRepository, profileManager );
281     }
282
283     public List JavaDoc collectProjects( File JavaDoc basedir, String JavaDoc[] includes, String JavaDoc[] excludes )
284         throws MojoExecutionException {
285         List JavaDoc projects = new ArrayList JavaDoc();
286
287         List JavaDoc poms = getPomFiles( basedir, includes, excludes );
288
289         for ( Iterator JavaDoc i = poms.iterator(); i.hasNext(); )
290         {
291             File JavaDoc pom = (File JavaDoc) i.next();
292
293             try
294             {
295                 MavenProject p = readProject( pom );
296
297                 projects.add( p );
298
299             }
300             catch ( ProjectBuildingException e )
301             {
302                 throw new MojoExecutionException( "Error loading " + pom, e );
303             }
304         }
305
306         return projects;
307     }
308
309     // ----------------------------------------------------------------------
310
// Artifacts
311
// ----------------------------------------------------------------------
312

313     public Artifact createArtifact( String JavaDoc groupId, String JavaDoc artifactId, String JavaDoc version, String JavaDoc scope, String JavaDoc type )
314     {
315         return artifactFactory.createArtifact( groupId, artifactId, version, scope, type );
316     }
317
318     public Artifact createArtifactWithClassifier( String JavaDoc groupId, String JavaDoc artifactId, String JavaDoc version, String JavaDoc type, String JavaDoc classifier )
319     {
320         return artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, type, classifier );
321     }
322
323     public void resolve( Artifact artifact, List JavaDoc remoteRepositories, ArtifactRepository localRepository )
324         throws ArtifactResolutionException, ArtifactNotFoundException
325     {
326         artifactResolver.resolve( artifact, remoteRepositories, localRepository );
327     }
328
329     // ----------------------------------------------------------------------
330
// Plugins
331
// ----------------------------------------------------------------------
332

333     public List JavaDoc getAvailablePlugins()
334     {
335         List JavaDoc plugins = new ArrayList JavaDoc();
336
337         plugins.add( makeMockPlugin( "org.apache.maven.plugins", "maven-jar-plugin", "Maven Jar Plug-in" ) );
338
339         plugins.add( makeMockPlugin( "org.apache.maven.plugins", "maven-compiler-plugin", "Maven Compiler Plug-in" ) );
340
341         return plugins;
342     }
343
344     public PluginDescriptor getPluginDescriptor( SummaryPluginDescriptor summaryPluginDescriptor )
345         throws MavenEmbedderException {
346         PluginDescriptor pluginDescriptor;
347
348         try
349         {
350             InputStream JavaDoc is = classLoader.getResourceAsStream( "/plugins/" + summaryPluginDescriptor.getArtifactId() + ".xml" );
351
352             pluginDescriptor = pluginDescriptorBuilder.build( new InputStreamReader JavaDoc( is ) );
353         }
354         catch ( PlexusConfigurationException e )
355         {
356             throw new MavenEmbedderException( "Error retrieving plugin descriptor.", e );
357         }
358
359         return pluginDescriptor;
360     }
361
362     private SummaryPluginDescriptor makeMockPlugin( String JavaDoc groupId, String JavaDoc artifactId, String JavaDoc name )
363     {
364         return new SummaryPluginDescriptor( groupId, artifactId, name );
365     }
366
367     // ----------------------------------------------------------------------
368
// Execution of phases/goals
369
// ----------------------------------------------------------------------
370

371     // TODO: should we allow the passing in of a settings object so that everything can be taken from the client env
372
// TODO: transfer listener
373
// TODO: logger
374

375     public void execute( MavenProject project,
376                          List JavaDoc goals,
377                          EventMonitor eventMonitor,
378                          TransferListener transferListener,
379                          Properties JavaDoc properties,
380                          File JavaDoc executionRootDirectory )
381         throws CycleDetectedException, LifecycleExecutionException, BuildFailureException, DuplicateProjectException {
382         execute( Collections.singletonList( project ), goals, eventMonitor, transferListener, properties, executionRootDirectory );
383     }
384
385     public void execute( List JavaDoc projects,
386                          List JavaDoc goals,
387                          EventMonitor eventMonitor,
388                          TransferListener transferListener,
389                          Properties JavaDoc properties,
390                          File JavaDoc executionRootDirectory )
391         throws CycleDetectedException, LifecycleExecutionException, BuildFailureException, DuplicateProjectException
392     {
393         ReactorManager rm = new ReactorManager( projects );
394
395         EventDispatcher eventDispatcher = new DefaultEventDispatcher();
396
397         eventDispatcher.addEventMonitor( eventMonitor );
398
399         // If this option is set the exception seems to be hidden ...
400

401         //rm.setFailureBehavior( ReactorManager.FAIL_AT_END );
402

403         rm.setFailureBehavior( ReactorManager.FAIL_FAST );
404
405         MavenSession session = new MavenSession( embedder.getContainer(),
406                                                  settings,
407                                                  localRepository,
408                                                  eventDispatcher,
409                                                  rm,
410                                                  goals,
411                                                  executionRootDirectory.getAbsolutePath(),
412                                                  properties,
413                                                  new Date JavaDoc() );
414
415         session.setUsingPOMsFromFilesystem( true );
416
417         if ( transferListener != null )
418         {
419             wagonManager.setDownloadMonitor( transferListener );
420         }
421
422         // ----------------------------------------------------------------------
423
// Maven should not be using system properties internally but because
424
// it does for now I'll just take properties that are handed to me
425
// and set them so that the plugin expression evaluator will work
426
// as expected.
427
// ----------------------------------------------------------------------
428

429         if ( properties != null )
430         {
431             for ( Iterator JavaDoc i = properties.keySet().iterator(); i.hasNext(); )
432             {
433                 String JavaDoc key = (String JavaDoc) i.next();
434
435                 String JavaDoc value = properties.getProperty( key );
436
437                 System.setProperty( key, value );
438             }
439         }
440
441         lifecycleExecutor.execute( session, rm, session.getEventDispatcher() );
442     }
443
444     // ----------------------------------------------------------------------
445
// Lifecycle information
446
// ----------------------------------------------------------------------
447

448     public List JavaDoc getLifecyclePhases()
449         throws MavenEmbedderException
450     {
451         List JavaDoc phases = new ArrayList JavaDoc();
452
453         ComponentDescriptor descriptor = embedder.getContainer().getComponentDescriptor( LifecycleExecutor.ROLE );
454
455         PlexusConfiguration configuration = descriptor.getConfiguration();
456
457         PlexusConfiguration[] phasesConfigurations = configuration.getChild( "lifecycles" ).getChild( 0 ).getChild( "phases" ).getChildren( "phase" );
458
459         try
460         {
461             for ( int i = 0; i < phasesConfigurations.length; i++ )
462             {
463                 phases.add( phasesConfigurations[i].getValue() );
464             }
465         }
466         catch ( PlexusConfigurationException e )
467         {
468             throw new MavenEmbedderException( "Cannot retrieve default lifecycle phasesConfigurations.", e );
469         }
470
471         return phases;
472     }
473
474     // ----------------------------------------------------------------------
475
// Remote Repository
476
// ----------------------------------------------------------------------
477

478     // ----------------------------------------------------------------------
479
// Local Repository
480
// ----------------------------------------------------------------------
481

482     public static final String JavaDoc DEFAULT_LOCAL_REPO_ID = "local";
483
484     public static final String JavaDoc DEFAULT_LAYOUT_ID = "default";
485
486     public ArtifactRepository createLocalRepository( File JavaDoc localRepository )
487         throws ComponentLookupException {
488         return createLocalRepository( localRepository.getAbsolutePath(), DEFAULT_LOCAL_REPO_ID );
489     }
490
491     public ArtifactRepository createLocalRepository( Settings settings )
492         throws ComponentLookupException
493     {
494         return createLocalRepository( settings.getLocalRepository(), DEFAULT_LOCAL_REPO_ID );
495     }
496
497     public ArtifactRepository createLocalRepository( String JavaDoc url, String JavaDoc repositoryId )
498         throws ComponentLookupException
499     {
500         if ( !url.startsWith( "file:" ) )
501         {
502             url = "file://" + url;
503         }
504
505         return createRepository( url, repositoryId );
506     }
507
508     public ArtifactRepository createRepository( String JavaDoc url, String JavaDoc repositoryId )
509         throws ComponentLookupException
510     {
511         // snapshots vs releases
512
// offline = to turning the update policy off
513

514         //TODO: we'll need to allow finer grained creation of repositories but this will do for now
515

516         String JavaDoc updatePolicyFlag = ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS;
517
518         String JavaDoc checksumPolicyFlag = ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN;
519
520         ArtifactRepositoryPolicy snapshotsPolicy = new ArtifactRepositoryPolicy( true, updatePolicyFlag, checksumPolicyFlag );
521
522         ArtifactRepositoryPolicy releasesPolicy = new ArtifactRepositoryPolicy( true, updatePolicyFlag, checksumPolicyFlag );
523
524         return artifactRepositoryFactory.createArtifactRepository( repositoryId, url, defaultArtifactRepositoryLayout, snapshotsPolicy, releasesPolicy );
525     }
526
527     // ----------------------------------------------------------------------
528
// Internal utility code
529
// ----------------------------------------------------------------------
530

531     private RuntimeInfo createRuntimeInfo( Settings settings )
532     {
533         RuntimeInfo runtimeInfo = new RuntimeInfo( settings );
534
535         runtimeInfo.setPluginUpdateOverride( Boolean.FALSE );
536
537         return runtimeInfo;
538     }
539
540     private List JavaDoc getPomFiles( File JavaDoc basedir, String JavaDoc[] includes, String JavaDoc[] excludes )
541     {
542         DirectoryScanner scanner = new DirectoryScanner();
543
544         scanner.setBasedir( basedir );
545
546         scanner.setIncludes( includes );
547
548         scanner.setExcludes( excludes );
549
550         scanner.scan();
551
552         List JavaDoc poms = new ArrayList JavaDoc();
553
554         for ( int i = 0; i < scanner.getIncludedFiles().length; i++ )
555         {
556             poms.add( new File JavaDoc( basedir, scanner.getIncludedFiles()[i] ) );
557         }
558
559         return poms;
560     }
561
562     // ----------------------------------------------------------------------
563
// Lifecycle
564
// ----------------------------------------------------------------------
565

566     public void start()
567         throws MavenEmbedderException
568     {
569         detectUserInstallation();
570
571         // ----------------------------------------------------------------------
572
// Set the maven.home system property which is need by components like
573
// the plugin registry builder.
574
// ----------------------------------------------------------------------
575

576         if ( classLoader == null )
577         {
578             throw new IllegalStateException JavaDoc( "A classloader must be specified using setClassLoader(ClassLoader)." );
579         }
580
581         embedder = new Embedder();
582
583         if ( logger != null )
584         {
585             embedder.setLoggerManager( new MavenEmbedderLoggerManager( new PlexusLoggerAdapter( logger ) ) );
586         }
587
588         // begin changes by KK
589
if(overridingComponentsXml!=null) {
590             try {
591                 embedder.setConfiguration(overridingComponentsXml);
592             } catch (IOException JavaDoc e) {
593                 throw new MavenEmbedderException(e);
594             }
595         }
596         // end changes by KK
597

598         try
599         {
600             ClassWorld classWorld = new ClassWorld();
601
602             classWorld.newRealm( "plexus.core", classLoader );
603
604             embedder.start( classWorld );
605
606             // ----------------------------------------------------------------------
607
// Lookup each of the components we need to provide the desired
608
// client interface.
609
// ----------------------------------------------------------------------
610

611             modelReader = new MavenXpp3Reader();
612
613             modelWriter = new MavenXpp3Writer();
614
615             pluginDescriptorBuilder = new PluginDescriptorBuilder();
616
617             profileManager = new DefaultProfileManager( embedder.getContainer() );
618
619             mavenProjectBuilder = (MavenProjectBuilder) embedder.lookup( MavenProjectBuilder.ROLE );
620
621             // ----------------------------------------------------------------------
622
// Artifact related components
623
// ----------------------------------------------------------------------
624

625             artifactRepositoryFactory = (ArtifactRepositoryFactory) embedder.lookup( ArtifactRepositoryFactory.ROLE );
626
627             artifactFactory = (ArtifactFactory) embedder.lookup( ArtifactFactory.ROLE );
628
629             artifactResolver = (ArtifactResolver) embedder.lookup( ArtifactResolver.ROLE );
630
631             defaultArtifactRepositoryLayout = (ArtifactRepositoryLayout) embedder.lookup( ArtifactRepositoryLayout.ROLE, DEFAULT_LAYOUT_ID );
632
633             lifecycleExecutor = (LifecycleExecutor) embedder.lookup( LifecycleExecutor.ROLE );
634
635             wagonManager = (WagonManager) embedder.lookup( WagonManager.ROLE );
636
637             createMavenSettings();
638
639             profileManager.loadSettingsProfiles( settings );
640
641             localRepository = createLocalRepository( settings );
642         }
643         catch ( PlexusContainerException e )
644         {
645             throw new MavenEmbedderException( "Cannot start Plexus embedder.", e );
646         }
647         catch ( DuplicateRealmException e )
648         {
649             throw new MavenEmbedderException( "Cannot create Classworld realm for the embedder.", e );
650         }
651         catch ( ComponentLookupException e )
652         {
653             throw new MavenEmbedderException( "Cannot lookup required component.", e );
654         }
655     }
656
657     // ----------------------------------------------------------------------
658
//
659
// ----------------------------------------------------------------------
660

661     private void detectUserInstallation()
662     {
663         if ( new File JavaDoc( userHome, ".m2" ).exists() )
664         {
665             alignWithUserInstallation = true;
666         }
667     }
668
669     /**
670      * Create the Settings that will be used with the embedder. If we are aligning with the user
671      * installation then we lookup the standard settings builder and use that to create our
672      * settings. Otherwise we constructs a settings object and populate the information
673      * ourselves.
674      *
675      * @throws MavenEmbedderException
676      * @throws ComponentLookupException
677      */

678     private void createMavenSettings()
679         throws MavenEmbedderException, ComponentLookupException
680     {
681         if ( alignWithUserInstallation )
682         {
683             // ----------------------------------------------------------------------
684
// We will use the standard method for creating the settings. This
685
// method reproduces the method of building the settings from the CLI
686
// mode of operation.
687
// ----------------------------------------------------------------------
688

689             settingsBuilder = (MavenSettingsBuilder) embedder.lookup( MavenSettingsBuilder.ROLE );
690
691             try
692             {
693                 settings = settingsBuilder.buildSettings();
694             }
695             catch ( IOException JavaDoc e )
696             {
697                 throw new MavenEmbedderException( "Error creating settings.", e );
698             }
699             catch ( XmlPullParserException e )
700             {
701                 throw new MavenEmbedderException( "Error creating settings.", e );
702             }
703         }
704         else
705         {
706             if ( localRepository == null )
707             {
708                 throw new IllegalArgumentException JavaDoc( "When not aligning with a user install you must specify a local repository location using the setLocalRepositoryDirectory( File ) method." );
709             }
710
711             settings = new Settings();
712
713             settings.setLocalRepository( localRepositoryDirectory.getAbsolutePath() );
714
715             settings.setRuntimeInfo( createRuntimeInfo( settings ) );
716
717             settings.setOffline( offline );
718
719             settings.setInteractiveMode( interactiveMode );
720         }
721     }
722
723     // ----------------------------------------------------------------------
724
// Lifecycle
725
// ----------------------------------------------------------------------
726

727     public void stop()
728         throws MavenEmbedderException
729     {
730         try
731         {
732             embedder.release( mavenProjectBuilder );
733
734             embedder.release( artifactRepositoryFactory );
735
736             embedder.release( settingsBuilder );
737
738             embedder.release( lifecycleExecutor );
739         }
740         catch ( ComponentLifecycleException e )
741         {
742             throw new MavenEmbedderException( "Cannot stop the embedder.", e );
743         }
744     }
745
746
747     // ----------------------------------------------------------------------
748
// Local Changes in Hudson below
749
// ----------------------------------------------------------------------
750
private URL JavaDoc overridingComponentsXml;
751
752     /**
753      * Sets the URL of the <tt>components.xml</tt> that overrides those found
754      * in the rest of classpath. Hudson uses this to replace certain key components
755      * by its own versions.
756      *
757      * This should become unnecessary when MNG-2777 is resolved.
758      */

759     public void setOverridingComponentsXml(URL JavaDoc overridingComponentsXml) {
760         this.overridingComponentsXml = overridingComponentsXml;
761     }
762
763     /**
764      * Gets the {@link PlexusContainer} that hosts Maven.
765      *
766      * See MNG-2778
767      */

768     public PlexusContainer getContainer() {
769         return embedder.getContainer();
770     }
771 }
772
Popular Tags