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                          .build()))
80                  .build();
81      }
82  
83      /** Indicates the phase is after the phases given in arguments */
84      static Lifecycle.Link after(String b) {
85          return new Lifecycle.Link() {
86              @Override
87              public Kind kind() {
88                  return Kind.AFTER;
89              }
90  
91              @Override
92              public Lifecycle.Pointer pointer() {
93                  return new Lifecycle.PhasePointer() {
94                      @Override
95                      public String phase() {
96                          return b;
97                      }
98                  };
99              }
100         };
101     }
102 
103     /** Indicates the phase is after the phases for the dependencies in the given scope */
104     static Lifecycle.Link dependencies(String scope, String phase) {
105         return new Lifecycle.Link() {
106             @Override
107             public Kind kind() {
108                 return Kind.AFTER;
109             }
110 
111             @Override
112             public Lifecycle.Pointer pointer() {
113                 return new Lifecycle.DependenciesPointer() {
114                     @Override
115                     public String phase() {
116                         return phase;
117                     }
118 
119                     @Override
120                     public String scope() {
121                         return scope;
122                     }
123                 };
124             }
125         };
126     }
127 
128     static Lifecycle.Alias alias(String v3Phase, String v4Phase) {
129         return new DefaultAlias(v3Phase, v4Phase);
130     }
131 
132     static class DefaultPhase implements Lifecycle.Phase {
133         private final String name;
134         private final List<Plugin> plugins;
135         private final Collection<Lifecycle.Link> links;
136         private final List<Lifecycle.Phase> phases;
137 
138         DefaultPhase(
139                 String name, List<Plugin> plugins, Collection<Lifecycle.Link> links, List<Lifecycle.Phase> phases) {
140             this.name = name;
141             this.plugins = plugins;
142             this.links = links;
143             this.phases = phases;
144         }
145 
146         @Override
147         public String name() {
148             return name;
149         }
150 
151         @Override
152         public List<Plugin> plugins() {
153             return plugins;
154         }
155 
156         @Override
157         public Collection<Lifecycle.Link> links() {
158             return links;
159         }
160 
161         @Override
162         public List<Lifecycle.Phase> phases() {
163             return phases;
164         }
165 
166         @Override
167         public Stream<Lifecycle.Phase> allPhases() {
168             return Stream.concat(Stream.of(this), phases().stream().flatMap(Lifecycle.Phase::allPhases));
169         }
170     }
171 
172     static class DefaultAlias implements Lifecycle.Alias {
173         private final String v3Phase;
174         private final String v4Phase;
175 
176         DefaultAlias(String v3Phase, String v4Phase) {
177             this.v3Phase = v3Phase;
178             this.v4Phase = v4Phase;
179         }
180 
181         @Override
182         public String v3Phase() {
183             return v3Phase;
184         }
185 
186         @Override
187         public String v4Phase() {
188             return v4Phase;
189         }
190     }
191 }