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