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.resolver;
20  
21  import java.io.File;
22  import java.nio.file.Path;
23  import java.util.Date;
24  import java.util.LinkedHashMap;
25  import java.util.List;
26  
27  import org.apache.maven.api.annotations.Nonnull;
28  import org.apache.maven.api.annotations.Nullable;
29  import org.apache.maven.api.metadata.Metadata;
30  import org.apache.maven.api.metadata.Plugin;
31  
32  /**
33   * Maven G level metadata.
34   */
35  final class PluginsMetadata extends MavenMetadata {
36      static final class PluginInfo {
37          @Nonnull
38          final String groupId;
39  
40          @Nonnull
41          private final String artifactId;
42  
43          @Nullable
44          private final String goalPrefix;
45  
46          @Nullable
47          private final String name;
48  
49          PluginInfo(String groupId, String artifactId, String goalPrefix, String name) {
50              this.groupId = groupId;
51              this.artifactId = artifactId;
52              this.goalPrefix = goalPrefix;
53              this.name = name;
54          }
55      }
56  
57      private final PluginInfo pluginInfo;
58  
59      PluginsMetadata(PluginInfo pluginInfo, Date timestamp) {
60          super(createRepositoryMetadata(pluginInfo), (Path) null, timestamp);
61          this.pluginInfo = pluginInfo;
62      }
63  
64      PluginsMetadata(PluginInfo pluginInfo, Path path, Date timestamp) {
65          super(createRepositoryMetadata(pluginInfo), path, timestamp);
66          this.pluginInfo = pluginInfo;
67      }
68  
69      private static Metadata createRepositoryMetadata(PluginInfo pluginInfo) {
70          return Metadata.newBuilder()
71                  .plugins(List.of(Plugin.newBuilder()
72                          .prefix(pluginInfo.goalPrefix)
73                          .artifactId(pluginInfo.artifactId)
74                          .name(pluginInfo.name)
75                          .build()))
76                  .build();
77      }
78  
79      @Override
80      protected void merge(Metadata recessive) {
81          List<Plugin> recessivePlugins = recessive.getPlugins();
82          List<Plugin> plugins = metadata.getPlugins();
83          if (!recessivePlugins.isEmpty() || !plugins.isEmpty()) {
84              LinkedHashMap<String, Plugin> mergedPlugins = new LinkedHashMap<>();
85              recessivePlugins.forEach(p -> mergedPlugins.put(p.getPrefix(), p));
86              plugins.forEach(p -> mergedPlugins.put(p.getPrefix(), p));
87              metadata = metadata.withPlugins(mergedPlugins.values());
88          }
89          // just carry-on as-is
90          if (recessive.getVersioning() != null) {
91              metadata = metadata.withVersioning(recessive.getVersioning());
92          }
93      }
94  
95      @Deprecated
96      @Override
97      public MavenMetadata setFile(File file) {
98          return new PluginsMetadata(pluginInfo, file.toPath(), timestamp);
99      }
100 
101     @Override
102     public MavenMetadata setPath(Path path) {
103         return new PluginsMetadata(pluginInfo, path, timestamp);
104     }
105 
106     @Override
107     public String getGroupId() {
108         return pluginInfo.groupId;
109     }
110 
111     @Override
112     public String getArtifactId() {
113         return "";
114     }
115 
116     @Override
117     public String getVersion() {
118         return "";
119     }
120 
121     @Override
122     public Nature getNature() {
123         return Nature.RELEASE_OR_SNAPSHOT;
124     }
125 }