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