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.Arrays;
23  import java.util.Collections;
24  import java.util.LinkedHashMap;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Optional;
28  import java.util.function.Function;
29  import java.util.stream.Collectors;
30  
31  /**
32   * Mojo (goals) bindings to a lifecycle phase.
33   *
34   * @see LifecycleMojo
35   */
36  public class LifecyclePhase {
37  
38      private List<LifecycleMojo> mojos;
39  
40      public LifecyclePhase() {}
41  
42      public LifecyclePhase(String goals) {
43          set(goals);
44      }
45  
46      public List<LifecycleMojo> getMojos() {
47          return mojos;
48      }
49  
50      public void setMojos(List<LifecycleMojo> mojos) {
51          this.mojos = mojos;
52      }
53  
54      public void set(String goals) {
55          mojos = new ArrayList<>();
56  
57          if (goals != null && !goals.isEmpty()) {
58              String[] mojoGoals = goals.split(",");
59              mojos = Arrays.stream(mojoGoals).map(fromGoalIntoLifecycleMojo).collect(Collectors.toList());
60          }
61      }
62  
63      private final Function<String, LifecycleMojo> fromGoalIntoLifecycleMojo = s -> {
64          LifecycleMojo lifecycleMojo = new LifecycleMojo();
65          lifecycleMojo.setGoal(s.trim());
66          return lifecycleMojo;
67      };
68  
69      @Override
70      public String toString() {
71          return Optional.ofNullable(getMojos()).orElse(Collections.emptyList()).stream()
72                  .map(LifecycleMojo::getGoal)
73                  .collect(Collectors.joining(","));
74      }
75  
76      @Deprecated
77      public static Map<String, String> toLegacyMap(Map<String, LifecyclePhase> lifecyclePhases) {
78          if (lifecyclePhases == null) {
79              return null;
80          }
81  
82          if (lifecyclePhases.isEmpty()) {
83              return Collections.emptyMap();
84          }
85  
86          Map<String, String> phases = new LinkedHashMap<>();
87          for (Map.Entry<String, LifecyclePhase> e : lifecyclePhases.entrySet()) {
88              phases.put(e.getKey(), e.getValue().toString());
89          }
90          return phases;
91      }
92  }