001package org.apache.maven.lifecycle.mapping;
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.LinkedHashMap;
025import java.util.List;
026import java.util.Map;
027
028import org.codehaus.plexus.util.StringUtils;
029
030public class LifecyclePhase
031{
032    
033    private List<LifecycleMojo> mojos;
034    
035    public LifecyclePhase()
036    {
037    }
038    
039    public LifecyclePhase( String goals )
040    {
041        set( goals );
042    }
043    
044    public List<LifecycleMojo> getMojos()
045    {
046        return mojos;
047    }
048    
049    public void setMojos( List<LifecycleMojo> mojos )
050    {
051        this.mojos = mojos;
052    }
053    
054    public void set( String goals )
055    {
056        mojos = new ArrayList<>();
057        
058        if ( StringUtils.isNotEmpty( goals ) )
059        {
060            String[] mojoGoals = StringUtils.split( goals, "," );
061            
062            for ( String mojoGoal: mojoGoals )
063            {
064                LifecycleMojo lifecycleMojo = new LifecycleMojo();
065                lifecycleMojo.setGoal( mojoGoal.trim() );
066                mojos.add( lifecycleMojo );
067            }
068        }
069    }
070    
071    @Override
072    public String toString()
073    {
074        StringBuilder sb = new StringBuilder();
075        boolean first = true;
076        List<LifecycleMojo> mojos = getMojos();
077        if ( mojos != null )
078        {
079            for ( LifecycleMojo mojo: mojos )
080            {
081                if ( first )
082                {
083                    first = false;
084                }
085                else
086                {
087                    sb.append( "," );
088                }
089                sb.append( mojo.getGoal() );
090            }
091        }
092        return sb.toString();
093    }
094    
095    @Deprecated
096    public static Map<String, String> toLegacyMap( Map<String, LifecyclePhase> lifecyclePhases )
097    {
098        if ( lifecyclePhases == null )
099        {
100            return null;
101        }
102        
103        if ( lifecyclePhases.isEmpty() )
104        {
105            return Collections.emptyMap();
106        }
107        
108        Map<String, String> phases = new LinkedHashMap<>();
109        for ( Map.Entry<String, LifecyclePhase> e: lifecyclePhases.entrySet() )
110        {
111            phases.put( e.getKey(), e.getValue().toString() );
112        }
113        return phases;
114    }
115    
116}