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