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.artifact.repository;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import org.apache.maven.execution.MavenSession;
25  import org.apache.maven.project.MavenProject;
26  
27  /**
28   * Collects basic settings to access the repository system.
29   *
30   */
31  public class DefaultRepositoryRequest implements RepositoryRequest {
32  
33      private boolean offline;
34  
35      private boolean forceUpdate;
36  
37      private ArtifactRepository localRepository;
38  
39      private List<ArtifactRepository> remoteRepositories;
40  
41      /**
42       * Creates an empty repository request.
43       */
44      public DefaultRepositoryRequest() {
45          // enables no-arg constructor
46      }
47  
48      /**
49       * Creates a shallow copy of the specified repository request.
50       *
51       * @param repositoryRequest The repository request to copy from, must not be {@code null}.
52       */
53      public DefaultRepositoryRequest(RepositoryRequest repositoryRequest) {
54          setLocalRepository(repositoryRequest.getLocalRepository());
55          setRemoteRepositories(repositoryRequest.getRemoteRepositories());
56          setOffline(repositoryRequest.isOffline());
57          setForceUpdate(repositoryRequest.isForceUpdate());
58      }
59  
60      public static RepositoryRequest getRepositoryRequest(MavenSession session, MavenProject project) {
61          RepositoryRequest request = new DefaultRepositoryRequest();
62  
63          request.setLocalRepository(session.getLocalRepository());
64          if (project != null) {
65              request.setRemoteRepositories(project.getPluginArtifactRepositories());
66          }
67          request.setOffline(session.isOffline());
68          request.setForceUpdate(session.getRequest().isUpdateSnapshots());
69  
70          return request;
71      }
72  
73      public boolean isOffline() {
74          return offline;
75      }
76  
77      public DefaultRepositoryRequest setOffline(boolean offline) {
78          this.offline = offline;
79  
80          return this;
81      }
82  
83      public boolean isForceUpdate() {
84          return forceUpdate;
85      }
86  
87      public DefaultRepositoryRequest setForceUpdate(boolean forceUpdate) {
88          this.forceUpdate = forceUpdate;
89  
90          return this;
91      }
92  
93      public ArtifactRepository getLocalRepository() {
94          return localRepository;
95      }
96  
97      public DefaultRepositoryRequest setLocalRepository(ArtifactRepository localRepository) {
98          this.localRepository = localRepository;
99  
100         return this;
101     }
102 
103     public List<ArtifactRepository> getRemoteRepositories() {
104         if (remoteRepositories == null) {
105             remoteRepositories = new ArrayList<>();
106         }
107 
108         return remoteRepositories;
109     }
110 
111     public DefaultRepositoryRequest setRemoteRepositories(List<ArtifactRepository> remoteRepositories) {
112         this.remoteRepositories = remoteRepositories;
113 
114         return this;
115     }
116 }