1 package org.apache.maven.lifecycle.mapping;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25
26 /**
27 * DefaultLifecycleMapping
28 */
29 public class DefaultLifecycleMapping
30 implements LifecycleMapping
31 {
32
33 private List<Lifecycle> lifecycles;
34
35 private Map<String, Lifecycle> lifecycleMap;
36
37 /** @deprecated use lifecycles instead */
38 private Map<String, LifecyclePhase> phases;
39
40 /**
41 * Populates the lifecycle map from the injected list of lifecycle mappings (if not already done).
42 */
43 private void initLifecycleMap()
44 {
45 if ( lifecycleMap == null )
46 {
47 lifecycleMap = new HashMap<>();
48
49 if ( lifecycles != null )
50 {
51 for ( Lifecycle lifecycle : lifecycles )
52 {
53 lifecycleMap.put( lifecycle.getId(), lifecycle );
54 }
55 }
56 else
57 {
58 /*
59 * NOTE: This is to provide a migration path for implementors of the legacy API which did not know about
60 * getLifecycles().
61 */
62
63 String[] lifecycleIds = { "default", "clean", "site" };
64
65 for ( String lifecycleId : lifecycleIds )
66 {
67 Map<String, LifecyclePhase> phases = getLifecyclePhases( lifecycleId );
68 if ( phases != null )
69 {
70 Lifecycle lifecycle = new Lifecycle();
71
72 lifecycle.setId( lifecycleId );
73 lifecycle.setLifecyclePhases( phases );
74
75 lifecycleMap.put( lifecycleId, lifecycle );
76 }
77 }
78 }
79 }
80 }
81
82 public Map<String, Lifecycle> getLifecycles()
83 {
84 initLifecycleMap();
85
86 return lifecycleMap;
87 }
88
89 public List<String> getOptionalMojos( String lifecycle )
90 {
91 return null;
92 }
93
94 private Map<String, LifecyclePhase> getLifecyclePhases( String lifecycle )
95 {
96 initLifecycleMap();
97
98 Lifecycle lifecycleMapping = lifecycleMap.get( lifecycle );
99
100 if ( lifecycleMapping != null )
101 {
102 return lifecycleMapping.getLifecyclePhases();
103 }
104 else if ( "default".equals( lifecycle ) )
105 {
106 return phases;
107 }
108 else
109 {
110 return null;
111 }
112 }
113
114 @Deprecated
115 public Map<String, String> getPhases( String lifecycle )
116 {
117 return LifecyclePhase.toLegacyMap( getLifecyclePhases( lifecycle ) );
118 }
119
120 }