001package org.apache.maven.project;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.List;
023
024import org.apache.maven.artifact.repository.ArtifactRepository;
025import org.apache.maven.model.Model;
026import org.apache.maven.model.building.AbstractModelBuildingListener;
027import org.apache.maven.model.building.ModelBuildingEvent;
028import org.apache.maven.model.building.ModelProblem.Severity;
029import org.apache.maven.model.building.ModelProblem.Version;
030import org.apache.maven.model.building.ModelProblemCollectorRequest;
031import org.apache.maven.plugin.PluginManagerException;
032import org.apache.maven.plugin.PluginResolutionException;
033import org.apache.maven.plugin.version.PluginVersionResolutionException;
034
035/**
036 * Processes events from the model builder while building the effective model for a {@link MavenProject} instance.
037 *
038 * @author Benjamin Bentmann
039 */
040public class DefaultModelBuildingListener
041    extends AbstractModelBuildingListener
042{
043
044    private MavenProject project;
045
046    private ProjectBuildingHelper projectBuildingHelper;
047
048    private ProjectBuildingRequest projectBuildingRequest;
049
050    private List<ArtifactRepository> remoteRepositories;
051
052    private List<ArtifactRepository> pluginRepositories;
053
054    public DefaultModelBuildingListener( MavenProject project, ProjectBuildingHelper projectBuildingHelper,
055                                         ProjectBuildingRequest projectBuildingRequest )
056    {
057        if ( project == null )
058        {
059            throw new IllegalArgumentException( "project missing" );
060        }
061        this.project = project;
062
063        if ( projectBuildingHelper == null )
064        {
065            throw new IllegalArgumentException( "project building helper missing" );
066        }
067        this.projectBuildingHelper = projectBuildingHelper;
068
069        if ( projectBuildingRequest == null )
070        {
071            throw new IllegalArgumentException( "project building request missing" );
072        }
073        this.projectBuildingRequest = projectBuildingRequest;
074        this.remoteRepositories = projectBuildingRequest.getRemoteRepositories();
075        this.pluginRepositories = projectBuildingRequest.getPluginArtifactRepositories();
076    }
077
078    /**
079     * Gets the project whose model is being built.
080     *
081     * @return The project, never {@code null}.
082     */
083    public MavenProject getProject()
084    {
085        return project;
086    }
087
088    @Override
089    public void buildExtensionsAssembled( ModelBuildingEvent event )
090    {
091        Model model = event.getModel();
092
093        try
094        {
095            pluginRepositories =
096                projectBuildingHelper.createArtifactRepositories( model.getPluginRepositories(), pluginRepositories,
097                                                                  projectBuildingRequest );
098        }
099        catch ( Exception e )
100        {
101            event.getProblems().add( new ModelProblemCollectorRequest( Severity.ERROR, Version.BASE )
102                    .setMessage( "Invalid plugin repository: " + e.getMessage() )
103                    .setException( e ) );
104        }
105        project.setPluginArtifactRepositories( pluginRepositories );
106
107        if ( event.getRequest().isProcessPlugins() )
108        {
109            try
110            {
111                ProjectRealmCache.CacheRecord record =
112                    projectBuildingHelper.createProjectRealm( project, model, projectBuildingRequest );
113
114                project.setClassRealm( record.realm );
115                project.setExtensionDependencyFilter( record.extensionArtifactFilter );
116            }
117            catch ( PluginResolutionException e )
118            {
119                event.getProblems().add( new ModelProblemCollectorRequest( Severity.ERROR, Version.BASE )
120                        .setMessage( "Unresolveable build extension: " + e.getMessage() )
121                        .setException( e ) );
122            }
123            catch ( PluginVersionResolutionException e )
124            {
125                event.getProblems().add( new ModelProblemCollectorRequest( Severity.ERROR, Version.BASE )
126                        .setMessage( "Unresolveable build extension: " + e.getMessage() )
127                        .setException( e ) );
128            }
129            catch ( PluginManagerException e )
130            {
131                event.getProblems().add( new ModelProblemCollectorRequest( Severity.ERROR, Version.BASE )
132                        .setMessage( "Unresolveable build extension: " + e.getMessage() )
133                        .setException( e ) );
134            }
135
136            projectBuildingHelper.selectProjectRealm( project );
137        }
138
139        // build the regular repos after extensions are loaded to allow for custom layouts
140        try
141        {
142            remoteRepositories =
143                projectBuildingHelper.createArtifactRepositories( model.getRepositories(), remoteRepositories,
144                                                                  projectBuildingRequest );
145        }
146        catch ( Exception e )
147        {
148            event.getProblems().add( new ModelProblemCollectorRequest( Severity.ERROR, Version.BASE )
149                    .setMessage( "Invalid artifact repository: " + e.getMessage() )
150                    .setException( e ) );
151        }
152        project.setRemoteArtifactRepositories( remoteRepositories );
153    }
154
155}