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.internal.impl;
20  
21  import java.util.Collection;
22  import java.util.Collections;
23  import java.util.List;
24  import java.util.stream.Stream;
25  
26  import org.apache.maven.api.Lifecycle;
27  import org.apache.maven.api.model.Plugin;
28  import org.apache.maven.api.model.PluginExecution;
29  
30  import static java.util.Arrays.asList;
31  
32  public class Lifecycles {
33  
34      static Lifecycle.Phase phase(String name) {
35          return new DefaultPhase(name, Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
36      }
37  
38      static Lifecycle.Phase phase(String name, Lifecycle.Phase... phases) {
39          return new DefaultPhase(name, Collections.emptyList(), Collections.emptyList(), asList(phases));
40      }
41  
42      static Lifecycle.Phase phase(String name, Lifecycle.Link link, Lifecycle.Phase... phases) {
43          return new DefaultPhase(name, Collections.emptyList(), Collections.singletonList(link), asList(phases));
44      }
45  
46      static Lifecycle.Phase phase(String name, Plugin plugin) {
47          return new DefaultPhase(
48                  name, Collections.singletonList(plugin), Collections.emptyList(), Collections.emptyList());
49      }
50  
51      static Lifecycle.Phase phase(String name, Lifecycle.Link link, Plugin plugin) {
52          return new DefaultPhase(
53                  name, Collections.singletonList(plugin), Collections.singletonList(link), Collections.emptyList());
54      }
55  
56      static Lifecycle.Phase phase(String name, Lifecycle.Link link1, Lifecycle.Link link2, Lifecycle.Phase... phases) {
57          return new DefaultPhase(name, Collections.emptyList(), asList(link1, link2), asList(phases));
58      }
59  
60      static Lifecycle.Phase phase(
61              String name, Lifecycle.Link link1, Lifecycle.Link link2, Lifecycle.Link link3, Lifecycle.Phase... phases) {
62          return new DefaultPhase(name, Collections.emptyList(), asList(link1, link2, link3), asList(phases));
63      }
64  
65      static Lifecycle.Phase phase(String name, Collection<Lifecycle.Link> links, Lifecycle.Phase... phases) {
66          return new DefaultPhase(name, Collections.emptyList(), links, asList(phases));
67      }
68  
69      static Plugin plugin(String coords, String phase) {
70          String[] c = coords.split(":");
71          return Plugin.newBuilder()
72                  .groupId(c[0])
73                  .artifactId(c[1])
74                  .version(c[2])
75                  .executions(Collections.singletonList(PluginExecution.newBuilder()
76                          .id("default-" + c[3])
77                          .phase(phase)
78                          .goals(Collections.singletonList(c[3]))
79                          .location("", DefaultLifecycleRegistry.DEFAULT_LIFECYCLE_INPUT_LOCATION)
80                          .location("id", DefaultLifecycleRegistry.DEFAULT_LIFECYCLE_INPUT_LOCATION)
81                          .location("phase", DefaultLifecycleRegistry.DEFAULT_LIFECYCLE_INPUT_LOCATION)
82                          .location("goals", DefaultLifecycleRegistry.DEFAULT_LIFECYCLE_INPUT_LOCATION)
83                          .build()))
84                  .location("", DefaultLifecycleRegistry.DEFAULT_LIFECYCLE_INPUT_LOCATION)
85                  .location("groupId", DefaultLifecycleRegistry.DEFAULT_LIFECYCLE_INPUT_LOCATION)
86                  .location("artifactId", DefaultLifecycleRegistry.DEFAULT_LIFECYCLE_INPUT_LOCATION)
87                  .location("version", DefaultLifecycleRegistry.DEFAULT_LIFECYCLE_INPUT_LOCATION)
88                  .build();
89      }
90  
91      /** Indicates the phase is after the phases given in arguments */
92      static Lifecycle.Link after(String b) {
93          return new Lifecycle.Link() {
94              @Override
95              public Kind kind() {
96                  return Kind.AFTER;
97              }
98  
99              @Override
100             public Lifecycle.Pointer pointer() {
101                 return new Lifecycle.PhasePointer() {
102                     @Override
103                     public String phase() {
104                         return b;
105                     }
106                 };
107             }
108         };
109     }
110 
111     /** Indicates the phase is after the phases for the dependencies in the given scope */
112     static Lifecycle.Link dependencies(String scope, String phase) {
113         return new Lifecycle.Link() {
114             @Override
115             public Kind kind() {
116                 return Kind.AFTER;
117             }
118 
119             @Override
120             public Lifecycle.Pointer pointer() {
121                 return new Lifecycle.DependenciesPointer() {
122                     @Override
123                     public String phase() {
124                         return phase;
125                     }
126 
127                     @Override
128                     public String scope() {
129                         return scope;
130                     }
131                 };
132             }
133         };
134     }
135 
136     static Lifecycle.Alias alias(String v3Phase, String v4Phase) {
137         return new DefaultAlias(v3Phase, v4Phase);
138     }
139 
140     static class DefaultPhase implements Lifecycle.Phase {
141         private final String name;
142         private final List<Plugin> plugins;
143         private final Collection<Lifecycle.Link> links;
144         private final List<Lifecycle.Phase> phases;
145 
146         DefaultPhase(
147                 String name, List<Plugin> plugins, Collection<Lifecycle.Link> links, List<Lifecycle.Phase> phases) {
148             this.name = name;
149             this.plugins = plugins;
150             this.links = links;
151             this.phases = phases;
152         }
153 
154         @Override
155         public String name() {
156             return name;
157         }
158 
159         @Override
160         public List<Plugin> plugins() {
161             return plugins;
162         }
163 
164         @Override
165         public Collection<Lifecycle.Link> links() {
166             return links;
167         }
168 
169         @Override
170         public List<Lifecycle.Phase> phases() {
171             return phases;
172         }
173 
174         @Override
175         public Stream<Lifecycle.Phase> allPhases() {
176             return Stream.concat(Stream.of(this), phases().stream().flatMap(Lifecycle.Phase::allPhases));
177         }
178     }
179 
180     static class DefaultAlias implements Lifecycle.Alias {
181         private final String v3Phase;
182         private final String v4Phase;
183 
184         DefaultAlias(String v3Phase, String v4Phase) {
185             this.v3Phase = v3Phase;
186             this.v4Phase = v4Phase;
187         }
188 
189         @Override
190         public String v3Phase() {
191             return v3Phase;
192         }
193 
194         @Override
195         public String v4Phase() {
196             return v4Phase;
197         }
198     }
199 }