View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.lifecycle.mapping;
20  
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.LinkedHashMap;
24  import java.util.List;
25  import java.util.Map;
26  import org.codehaus.plexus.util.StringUtils;
27  
28  /**
29   * Mojo (goals) bindings to a lifecycle phase.
30   *
31   * @see LifecycleMojo
32   */
33  public class LifecyclePhase {
34  
35      private List<LifecycleMojo> mojos;
36  
37      public LifecyclePhase() {}
38  
39      public LifecyclePhase(String goals) {
40          set(goals);
41      }
42  
43      public List<LifecycleMojo> getMojos() {
44          return mojos;
45      }
46  
47      public void setMojos(List<LifecycleMojo> mojos) {
48          this.mojos = mojos;
49      }
50  
51      public void set(String goals) {
52          mojos = new ArrayList<>();
53  
54          if (StringUtils.isNotEmpty(goals)) {
55              String[] mojoGoals = StringUtils.split(goals, ",");
56  
57              for (String mojoGoal : mojoGoals) {
58                  LifecycleMojo lifecycleMojo = new LifecycleMojo();
59                  lifecycleMojo.setGoal(mojoGoal.trim());
60                  mojos.add(lifecycleMojo);
61              }
62          }
63      }
64  
65      @Override
66      public String toString() {
67          StringBuilder sb = new StringBuilder();
68          boolean first = true;
69          List<LifecycleMojo> mojos = getMojos();
70          if (mojos != null) {
71              for (LifecycleMojo mojo : mojos) {
72                  if (first) {
73                      first = false;
74                  } else {
75                      sb.append(',');
76                  }
77                  sb.append(mojo.getGoal());
78              }
79          }
80          return sb.toString();
81      }
82  
83      @Deprecated
84      public static Map<String, String> toLegacyMap(Map<String, LifecyclePhase> lifecyclePhases) {
85          if (lifecyclePhases == null) {
86              return null;
87          }
88  
89          if (lifecyclePhases.isEmpty()) {
90              return Collections.emptyMap();
91          }
92  
93          Map<String, String> phases = new LinkedHashMap<>();
94          for (Map.Entry<String, LifecyclePhase> e : lifecyclePhases.entrySet()) {
95              phases.put(e.getKey(), e.getValue().toString());
96          }
97          return phases;
98      }
99  }