View Javadoc
1   package org.apache.maven.plugin.version;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Collections;
23  import java.util.List;
24  
25  import org.apache.maven.execution.MavenSession;
26  import org.apache.maven.model.Model;
27  import org.apache.maven.model.Plugin;
28  import org.apache.maven.project.MavenProject;
29  import org.eclipse.aether.RepositorySystemSession;
30  import org.eclipse.aether.repository.RemoteRepository;
31  
32  /**
33   * Collects settings required to resolve the version for a plugin.
34   *
35   * @since 3.0
36   * @author Benjamin Bentmann
37   */
38  public class DefaultPluginVersionRequest
39      implements PluginVersionRequest
40  {
41  
42      private String groupId;
43  
44      private String artifactId;
45  
46      private Model pom;
47  
48      private List<RemoteRepository> repositories = Collections.emptyList();
49  
50      private RepositorySystemSession session;
51  
52      /**
53       * Creates an empty request.
54       */
55      public DefaultPluginVersionRequest()
56      {
57      }
58  
59      /**
60       * Creates a request for the specified plugin by copying settings from the specified build session. If the session
61       * has a current project, its plugin repositories will be used as well.
62       *
63       * @param plugin The plugin for which to resolve a version, must not be {@code null}.
64       * @param session The Maven session to use, must not be {@code null}.
65       */
66      public DefaultPluginVersionRequest( Plugin plugin, MavenSession session )
67      {
68          setGroupId( plugin.getGroupId() );
69          setArtifactId( plugin.getArtifactId() );
70  
71          setRepositorySession( session.getRepositorySession() );
72  
73          MavenProject project = session.getCurrentProject();
74          if ( project != null )
75          {
76              setRepositories( project.getRemotePluginRepositories() );
77          }
78      }
79  
80      /**
81       * Creates a request for the specified plugin using the given repository session and plugin repositories.
82       *
83       * @param plugin The plugin for which to resolve a version, must not be {@code null}.
84       * @param session The repository session to use, must not be {@code null}.
85       * @param repositories The plugin repositories to query, may be {@code null}.
86       */
87      public DefaultPluginVersionRequest( Plugin plugin, RepositorySystemSession session, List<RemoteRepository> repositories )
88      {
89          setGroupId( plugin.getGroupId() );
90          setArtifactId( plugin.getArtifactId() );
91  
92          setRepositorySession( session );
93  
94          setRepositories( repositories );
95      }
96  
97      public String getGroupId()
98      {
99          return groupId;
100     }
101 
102     public DefaultPluginVersionRequest setGroupId( String groupId )
103     {
104         this.groupId = groupId;
105 
106         return this;
107     }
108 
109     public String getArtifactId()
110     {
111         return artifactId;
112     }
113 
114     public DefaultPluginVersionRequest setArtifactId( String artifactId )
115     {
116         this.artifactId = artifactId;
117 
118         return this;
119     }
120 
121     public Model getPom()
122     {
123         return pom;
124     }
125 
126     public DefaultPluginVersionRequest setPom( Model pom )
127     {
128         this.pom = pom;
129 
130         return this;
131     }
132 
133     public List<RemoteRepository> getRepositories()
134     {
135         return repositories;
136     }
137 
138     public DefaultPluginVersionRequest setRepositories( List<RemoteRepository> repositories )
139     {
140         if ( repositories != null )
141         {
142             this.repositories = repositories;
143         }
144         else
145         {
146             this.repositories = Collections.emptyList();
147         }
148 
149         return this;
150     }
151 
152     public RepositorySystemSession getRepositorySession()
153     {
154         return session;
155     }
156 
157     public DefaultPluginVersionRequest setRepositorySession( RepositorySystemSession session )
158     {
159         this.session = session;
160 
161         return this;
162     }
163 
164 }