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.repository.internal;
20  
21  import java.io.File;
22  import java.util.ArrayList;
23  import java.util.Date;
24  import java.util.LinkedHashMap;
25  import java.util.List;
26  
27  import org.apache.maven.artifact.repository.metadata.Metadata;
28  import org.apache.maven.artifact.repository.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), null, timestamp);
55          this.pluginInfo = pluginInfo;
56      }
57  
58      PluginsMetadata(PluginInfo pluginInfo, File file, Date timestamp) {
59          super(createRepositoryMetadata(pluginInfo), file, timestamp);
60          this.pluginInfo = pluginInfo;
61      }
62  
63      private static Metadata createRepositoryMetadata(PluginInfo pluginInfo) {
64          Metadata result = new Metadata();
65          Plugin plugin = new Plugin();
66          plugin.setPrefix(pluginInfo.goalPrefix);
67          plugin.setArtifactId(pluginInfo.artifactId);
68          plugin.setName(pluginInfo.name);
69          result.getPlugins().add(plugin);
70          return result;
71      }
72  
73      @Override
74      protected void merge(Metadata recessive) {
75          List<Plugin> recessivePlugins = recessive.getPlugins();
76          List<Plugin> plugins = metadata.getPlugins();
77          if (!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.setPlugins(new ArrayList<>(mergedPlugins.values()));
82          }
83      }
84  
85      @Override
86      public MavenMetadata setFile(File file) {
87          return new PluginsMetadata(pluginInfo, file, timestamp);
88      }
89  
90      @Override
91      public String getGroupId() {
92          return pluginInfo.groupId;
93      }
94  
95      @Override
96      public String getArtifactId() {
97          return "";
98      }
99  
100     @Override
101     public String getVersion() {
102         return "";
103     }
104 
105     @Override
106     public Nature getNature() {
107         return Nature.RELEASE_OR_SNAPSHOT;
108     }
109 }