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 java.util.ArrayList; 023import java.util.Collections; 024import java.util.HashMap; 025import java.util.HashSet; 026import java.util.Iterator; 027import java.util.List; 028import java.util.Map; 029import java.util.Set; 030 031import org.apache.maven.artifact.ArtifactUtils; 032import org.apache.maven.execution.MavenSession; 033import org.apache.maven.project.MavenProject; 034 035/** 036 * A list of project segments, ordered so that all ProjectSegments from first TaskSegment come before any 037 * subsequent TaskSegments. 038 * 039 * @since 3.0 040 * @author Kristian Rosenvold 041 * <p/> 042 * NOTE: This class is not part of any public api and can be changed or deleted without prior notice. 043 */ 044public class ProjectBuildList 045 implements Iterable<ProjectSegment> 046{ 047 private final List<ProjectSegment> items; 048 049 public ProjectBuildList( List<ProjectSegment> items ) 050 { 051 this.items = Collections.unmodifiableList( items ); 052 } 053 054 // TODO: Optimize; or maybe just rewrite the whole way aggregating mojos are being run. 055 /** 056 * Returns aProjectBuildList that contains only items for the specified taskSegment 057 * @param taskSegment the requested tasksegment 058 * @return a project build list for the supplied task segment 059 */ 060 public ProjectBuildList getByTaskSegment( TaskSegment taskSegment ) 061 { 062 List<ProjectSegment> currentSegment = new ArrayList<ProjectSegment>(); 063 for ( ProjectSegment projectBuild : items ) 064 { 065 if ( taskSegment == projectBuild.getTaskSegment() ) 066 { // NOTE: There's no notion of taskSegment equality. 067 currentSegment.add( projectBuild ); 068 } 069 } 070 return new ProjectBuildList( currentSegment ); 071 } 072 073 public Map<MavenProject, ProjectSegment> selectSegment( TaskSegment taskSegment ) 074 { 075 Map<MavenProject, ProjectSegment> result = new HashMap<MavenProject, ProjectSegment>(); 076 for ( ProjectSegment projectBuild : items ) 077 { 078 if ( taskSegment == projectBuild.getTaskSegment() ) 079 { // NOTE: There's no notion of taskSegment equality. 080 result.put( projectBuild.getProject(), projectBuild ); 081 } 082 } 083 return result; 084 } 085 086 /** 087 * Finds the first ProjectSegment matching the supplied project 088 * @param mavenProject the requested project 089 * @return The projectSegment or null. 090 */ 091 public ProjectSegment findByMavenProject( MavenProject mavenProject ) 092 { 093 for ( ProjectSegment projectBuild : items ) 094 { 095 if ( mavenProject.equals( projectBuild.getProject() ) ) 096 { 097 return projectBuild; 098 } 099 } 100 return null; 101 } 102 103 public Iterator<ProjectSegment> iterator() 104 { 105 return items.iterator(); 106 } 107 108 public void closeAll() 109 { 110 for ( ProjectSegment item : items ) 111 { 112 MavenSession sessionForThisModule = item.getSession(); 113 sessionForThisModule.setCurrentProject( null ); 114 } 115 } 116 117 public int size() 118 { 119 return items.size(); 120 } 121 122 public ProjectSegment get( int index ) 123 { 124 return items.get( index ); 125 } 126 127 public Set<String> getReactorProjectKeys() 128 { 129 Set<String> projectKeys = new HashSet<String>( items.size() * 2 ); 130 for ( ProjectSegment projectBuild : items ) 131 { 132 MavenProject project = projectBuild.getProject(); 133 String key = ArtifactUtils.key( project.getGroupId(), project.getArtifactId(), project.getVersion() ); 134 projectKeys.add( key ); 135 } 136 return projectKeys; 137 } 138 139 140 public boolean isEmpty() 141 { 142 return items.isEmpty(); 143 } 144 145 /** 146 * @return a set of all the projects managed by the build 147 */ 148 public Set<MavenProject> getProjects() 149 { 150 Set<MavenProject> projects = new HashSet<MavenProject>(); 151 152 for ( ProjectSegment s : items ) 153 { 154 projects.add( s.getProject() ); 155 } 156 return projects; 157 } 158}