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.project;
20  
21  import java.util.Arrays;
22  import java.util.Collections;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Optional;
27  
28  import org.apache.maven.api.Lifecycle;
29  import org.apache.maven.api.Packaging;
30  import org.apache.maven.api.Type;
31  import org.apache.maven.api.annotations.Nonnull;
32  import org.apache.maven.api.di.Inject;
33  import org.apache.maven.api.di.Named;
34  import org.apache.maven.api.di.Priority;
35  import org.apache.maven.api.di.Singleton;
36  import org.apache.maven.api.model.Plugin;
37  import org.apache.maven.api.model.PluginContainer;
38  import org.apache.maven.api.model.PluginExecution;
39  import org.apache.maven.api.services.LifecycleRegistry;
40  import org.apache.maven.api.services.PackagingRegistry;
41  import org.apache.maven.internal.impl.model.DefaultLifecycleBindingsInjector;
42  
43  import static org.apache.maven.api.Lifecycle.DEFAULT;
44  
45  @Singleton
46  @Named
47  @Priority(5)
48  @Deprecated
49  public class EmptyLifecycleBindingsInjector extends DefaultLifecycleBindingsInjector {
50  
51      private static LifecycleRegistry lifecycleRegistry;
52      private static PackagingRegistry packagingRegistry;
53  
54      private static final LifecycleRegistry emptyLifecycleRegistry = new LifecycleRegistry() {
55  
56          @Override
57          public Iterator<Lifecycle> iterator() {
58              return Collections.emptyIterator();
59          }
60  
61          @Override
62          public Optional<Lifecycle> lookup(String id) {
63              return Optional.empty();
64          }
65      };
66  
67      private static final PackagingRegistry emptyPackagingRegistry = new PackagingRegistry() {
68          @Override
69          public Optional<Packaging> lookup(String id) {
70              return Optional.of(new Packaging() {
71                  @Override
72                  public String id() {
73                      return id;
74                  }
75  
76                  @Override
77                  public Type type() {
78                      return null;
79                  }
80  
81                  @Override
82                  public Map<String, PluginContainer> plugins() {
83                      if ("JAR".equals(id)) {
84                          return Map.of(
85                                  DEFAULT,
86                                  PluginContainer.newBuilder()
87                                          .plugins(List.of(
88                                                  newPlugin("maven-compiler-plugin", "compile", "testCompile"),
89                                                  newPlugin("maven-resources-plugin", "resources", "testResources"),
90                                                  newPlugin("maven-surefire-plugin", "test"),
91                                                  newPlugin("maven-jar-plugin", "jar"),
92                                                  newPlugin("maven-install-plugin", "install"),
93                                                  newPlugin("maven-deploy-plugin", "deploy")))
94                                          .build());
95                      } else {
96                          return Map.of();
97                      }
98                  }
99              });
100         }
101     };
102 
103     @Inject
104     public EmptyLifecycleBindingsInjector(LifecycleRegistry lifecycleRegistry, PackagingRegistry packagingRegistry) {
105         super(new WrapperLifecycleRegistry(), new WrapperPackagingRegistry());
106         EmptyLifecycleBindingsInjector.lifecycleRegistry = lifecycleRegistry;
107         EmptyLifecycleBindingsInjector.packagingRegistry = packagingRegistry;
108     }
109 
110     public static void useEmpty() {
111         lifecycleRegistry = emptyLifecycleRegistry;
112         packagingRegistry = emptyPackagingRegistry;
113     }
114 
115     private static Plugin newPlugin(String artifactId, String... goals) {
116         return Plugin.newBuilder()
117                 .groupId("org.apache.maven.plugins")
118                 .artifactId(artifactId)
119                 .executions(Arrays.stream(goals)
120                         .map(goal -> PluginExecution.newBuilder()
121                                 .id("default-" + goal)
122                                 .goals(List.of(goal))
123                                 .build())
124                         .toList())
125                 .build();
126     }
127 
128     static class WrapperLifecycleRegistry implements LifecycleRegistry {
129         @Override
130         @Nonnull
131         public Optional<Lifecycle> lookup(String id) {
132             return getDelegate().lookup(id);
133         }
134 
135         @Override
136         public Iterator<Lifecycle> iterator() {
137             return getDelegate().iterator();
138         }
139 
140         protected LifecycleRegistry getDelegate() {
141             return lifecycleRegistry;
142         }
143     }
144 
145     static class WrapperPackagingRegistry implements PackagingRegistry {
146         @Override
147         public Optional<Packaging> lookup(String id) {
148             return getDelegate().lookup(id);
149         }
150 
151         private PackagingRegistry getDelegate() {
152             return packagingRegistry;
153         }
154     }
155 }