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