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