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.plugin.version;
20  
21  import java.util.Collections;
22  import java.util.List;
23  
24  import org.apache.maven.execution.MavenSession;
25  import org.apache.maven.model.Model;
26  import org.apache.maven.model.Plugin;
27  import org.apache.maven.project.MavenProject;
28  import org.eclipse.aether.RepositorySystemSession;
29  import org.eclipse.aether.repository.RemoteRepository;
30  
31  /**
32   * Collects settings required to resolve the version for a plugin.
33   *
34   * @since 3.0
35   */
36  public class DefaultPluginVersionRequest implements PluginVersionRequest {
37  
38      private String groupId;
39  
40      private String artifactId;
41  
42      private Model pom;
43  
44      private List<RemoteRepository> repositories = Collections.emptyList();
45  
46      private RepositorySystemSession session;
47  
48      /**
49       * Creates an empty request.
50       */
51      public DefaultPluginVersionRequest() {}
52  
53      /**
54       * Creates a request for the specified plugin by copying settings from the specified build session. If the session
55       * has a current project, its plugin repositories will be used as well.
56       *
57       * @param plugin The plugin for which to resolve a version, must not be {@code null}.
58       * @param session The Maven session to use, must not be {@code null}.
59       */
60      public DefaultPluginVersionRequest(Plugin plugin, MavenSession session) {
61          setGroupId(plugin.getGroupId());
62          setArtifactId(plugin.getArtifactId());
63  
64          setRepositorySession(session.getRepositorySession());
65  
66          MavenProject project = session.getCurrentProject();
67          if (project != null) {
68              setRepositories(project.getRemotePluginRepositories());
69          }
70      }
71  
72      /**
73       * Creates a request for the specified plugin using the given repository session and plugin repositories.
74       *
75       * @param plugin The plugin for which to resolve a version, must not be {@code null}.
76       * @param session The repository session to use, must not be {@code null}.
77       * @param repositories The plugin repositories to query, may be {@code null}.
78       */
79      public DefaultPluginVersionRequest(
80              Plugin plugin, RepositorySystemSession session, List<RemoteRepository> repositories) {
81          setGroupId(plugin.getGroupId());
82          setArtifactId(plugin.getArtifactId());
83  
84          setRepositorySession(session);
85  
86          setRepositories(repositories);
87      }
88  
89      @Override
90      public String getGroupId() {
91          return groupId;
92      }
93  
94      @Override
95      public DefaultPluginVersionRequest setGroupId(String groupId) {
96          this.groupId = groupId;
97  
98          return this;
99      }
100 
101     @Override
102     public String getArtifactId() {
103         return artifactId;
104     }
105 
106     @Override
107     public DefaultPluginVersionRequest setArtifactId(String artifactId) {
108         this.artifactId = artifactId;
109 
110         return this;
111     }
112 
113     @Override
114     public Model getPom() {
115         return pom;
116     }
117 
118     @Override
119     public DefaultPluginVersionRequest setPom(Model pom) {
120         this.pom = pom;
121 
122         return this;
123     }
124 
125     @Override
126     public List<RemoteRepository> getRepositories() {
127         return repositories;
128     }
129 
130     @Override
131     public DefaultPluginVersionRequest setRepositories(List<RemoteRepository> repositories) {
132         if (repositories != null) {
133             this.repositories = Collections.unmodifiableList(repositories);
134         } else {
135             this.repositories = Collections.emptyList();
136         }
137 
138         return this;
139     }
140 
141     @Override
142     public RepositorySystemSession getRepositorySession() {
143         return session;
144     }
145 
146     @Override
147     public DefaultPluginVersionRequest setRepositorySession(RepositorySystemSession session) {
148         this.session = session;
149 
150         return this;
151     }
152 }