001package org.apache.maven.lifecycle.internal;
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 org.apache.maven.execution.MavenSession;
023import org.apache.maven.lifecycle.internal.builder.BuilderCommon;
024import org.apache.maven.project.MavenProject;
025import org.codehaus.plexus.component.annotations.Component;
026
027import java.util.ArrayList;
028import java.util.Collections;
029import java.util.List;
030
031/**
032 * @since 3.0
033 * @author Kristian Rosenvold
034 *         This class is not part of any public api and can be changed or deleted without prior notice.
035 */
036@Component( role = BuildListCalculator.class )
037public class BuildListCalculator
038{
039    public ProjectBuildList calculateProjectBuilds( MavenSession session, List<TaskSegment> taskSegments )
040    {
041        List<ProjectSegment> projectBuilds = new ArrayList<>();
042
043        MavenProject rootProject = session.getTopLevelProject();
044
045        for ( TaskSegment taskSegment : taskSegments )
046        {
047            List<MavenProject> projects;
048
049            if ( taskSegment.isAggregating() )
050            {
051                projects = Collections.singletonList( rootProject );
052            }
053            else
054            {
055                projects = session.getProjects();
056            }
057            for ( MavenProject project : projects )
058            {
059                BuilderCommon.attachToThread( project ); // Not totally sure if this is needed for anything
060                MavenSession copiedSession = session.clone();
061                copiedSession.setCurrentProject( project );
062                projectBuilds.add( new ProjectSegment( project, taskSegment, copiedSession ) );
063            }
064        }
065        return new ProjectBuildList( projectBuilds );
066    }
067}