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