001    package org.apache.maven.plugin.version;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *   http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import java.util.Collections;
023    import java.util.List;
024    
025    import org.apache.maven.execution.MavenSession;
026    import org.apache.maven.model.Model;
027    import org.apache.maven.model.Plugin;
028    import org.apache.maven.project.MavenProject;
029    import org.sonatype.aether.RepositorySystemSession;
030    import org.sonatype.aether.repository.RemoteRepository;
031    
032    /**
033     * Collects settings required to resolve the version for a plugin.
034     * 
035     * @since 3.0
036     * @author Benjamin Bentmann
037     */
038    public class DefaultPluginVersionRequest
039        implements PluginVersionRequest
040    {
041    
042        private String groupId;
043    
044        private String artifactId;
045    
046        private Model pom;
047    
048        private List<RemoteRepository> repositories = Collections.emptyList();
049    
050        private RepositorySystemSession session;
051    
052        /**
053         * Creates an empty request.
054         */
055        public DefaultPluginVersionRequest()
056        {
057        }
058    
059        /**
060         * Creates a request for the specified plugin by copying settings from the specified build session. If the session
061         * has a current project, its plugin repositories will be used as well.
062         * 
063         * @param plugin The plugin for which to resolve a version, must not be {@code null}.
064         * @param repositoryRequest The repository request to copy from, must not be {@code null}.
065         */
066        public DefaultPluginVersionRequest( Plugin plugin, MavenSession session )
067        {
068            setGroupId( plugin.getGroupId() );
069            setArtifactId( plugin.getArtifactId() );
070    
071            setRepositorySession( session.getRepositorySession() );
072    
073            MavenProject project = session.getCurrentProject();
074            if ( project != null )
075            {
076                setRepositories( project.getRemotePluginRepositories() );
077            }
078        }
079    
080        /**
081         * Creates a request for the specified plugin using the given repository session and plugin repositories.
082         * 
083         * @param plugin The plugin for which to resolve a version, must not be {@code null}.
084         * @param session The repository session to use, must not be {@code null}.
085         * @param repositories The plugin repositories to query, may be {@code null}.
086         */
087        public DefaultPluginVersionRequest( Plugin plugin, RepositorySystemSession session, List<RemoteRepository> repositories )
088        {
089            setGroupId( plugin.getGroupId() );
090            setArtifactId( plugin.getArtifactId() );
091    
092            setRepositorySession( session );
093    
094            setRepositories( repositories );
095        }
096    
097        public String getGroupId()
098        {
099            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    }