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.project;
20  
21  import java.time.Instant;
22  import java.util.ArrayList;
23  import java.util.Date;
24  import java.util.List;
25  import java.util.Objects;
26  import java.util.Properties;
27  
28  import org.apache.maven.artifact.repository.ArtifactRepository;
29  import org.apache.maven.model.Profile;
30  import org.apache.maven.model.building.ModelBuildingRequest;
31  import org.apache.maven.properties.internal.SystemProperties;
32  import org.eclipse.aether.RepositorySystemSession;
33  
34  /**
35   * DefaultProjectBuildingRequest
36   */
37  public class DefaultProjectBuildingRequest implements ProjectBuildingRequest {
38  
39      private RepositorySystemSession repositorySession;
40  
41      private ArtifactRepository localRepository;
42  
43      private List<ArtifactRepository> remoteRepositories;
44  
45      private List<ArtifactRepository> pluginArtifactRepositories;
46  
47      private MavenProject project;
48  
49      private int validationLevel = ModelBuildingRequest.VALIDATION_LEVEL_STRICT;
50  
51      private boolean processPlugins;
52  
53      private List<Profile> profiles;
54  
55      private List<String> activeProfileIds;
56  
57      private List<String> inactiveProfileIds;
58  
59      private Properties systemProperties;
60  
61      private Properties userProperties;
62  
63      private Instant buildStartTime;
64  
65      private boolean resolveDependencies;
66  
67      @Deprecated
68      private boolean resolveVersionRanges;
69  
70      private RepositoryMerging repositoryMerging = RepositoryMerging.POM_DOMINANT;
71  
72      public DefaultProjectBuildingRequest() {
73          processPlugins = true;
74          profiles = new ArrayList<>();
75          activeProfileIds = new ArrayList<>();
76          inactiveProfileIds = new ArrayList<>();
77          systemProperties = new Properties();
78          userProperties = new Properties();
79          remoteRepositories = new ArrayList<>();
80          pluginArtifactRepositories = new ArrayList<>();
81      }
82  
83      public DefaultProjectBuildingRequest(ProjectBuildingRequest request) {
84          this();
85          setProcessPlugins(request.isProcessPlugins());
86          setProfiles(request.getProfiles());
87          setActiveProfileIds(request.getActiveProfileIds());
88          setInactiveProfileIds(request.getInactiveProfileIds());
89          setSystemProperties(request.getSystemProperties());
90          setUserProperties(request.getUserProperties());
91          setRemoteRepositories(request.getRemoteRepositories());
92          setPluginArtifactRepositories(request.getPluginArtifactRepositories());
93          setRepositorySession(request.getRepositorySession());
94          setLocalRepository(request.getLocalRepository());
95          setBuildStartTime(request.getBuildStartTime());
96          setProject(request.getProject());
97          setResolveDependencies(request.isResolveDependencies());
98          setValidationLevel(request.getValidationLevel());
99          setResolveVersionRanges(request.isResolveVersionRanges());
100         setRepositoryMerging(request.getRepositoryMerging());
101     }
102 
103     @Override
104     public MavenProject getProject() {
105         return project;
106     }
107 
108     @Override
109     public void setProject(MavenProject mavenProject) {
110         this.project = mavenProject;
111     }
112 
113     @Override
114     public ProjectBuildingRequest setLocalRepository(ArtifactRepository localRepository) {
115         this.localRepository = localRepository;
116         return this;
117     }
118 
119     @Override
120     public ArtifactRepository getLocalRepository() {
121         return localRepository;
122     }
123 
124     @Override
125     public List<ArtifactRepository> getRemoteRepositories() {
126         return remoteRepositories;
127     }
128 
129     @Override
130     public ProjectBuildingRequest setRemoteRepositories(List<ArtifactRepository> remoteRepositories) {
131         if (remoteRepositories != null) {
132             this.remoteRepositories = new ArrayList<>(remoteRepositories);
133         } else {
134             this.remoteRepositories.clear();
135         }
136 
137         return this;
138     }
139 
140     @Override
141     public List<ArtifactRepository> getPluginArtifactRepositories() {
142         return pluginArtifactRepositories;
143     }
144 
145     @Override
146     public ProjectBuildingRequest setPluginArtifactRepositories(List<ArtifactRepository> pluginArtifactRepositories) {
147         if (pluginArtifactRepositories != null) {
148             this.pluginArtifactRepositories = new ArrayList<>(pluginArtifactRepositories);
149         } else {
150             this.pluginArtifactRepositories.clear();
151         }
152 
153         return this;
154     }
155 
156     @Override
157     public Properties getSystemProperties() {
158         return systemProperties;
159     }
160 
161     @Override
162     public ProjectBuildingRequest setSystemProperties(Properties systemProperties) {
163         if (systemProperties != null) {
164             this.systemProperties = SystemProperties.copyProperties(systemProperties);
165         } else {
166             this.systemProperties.clear();
167         }
168 
169         return this;
170     }
171 
172     @Override
173     public Properties getUserProperties() {
174         return userProperties;
175     }
176 
177     @Override
178     public ProjectBuildingRequest setUserProperties(Properties userProperties) {
179         if (userProperties != null) {
180             this.userProperties = new Properties();
181             this.userProperties.putAll(userProperties);
182         } else {
183             this.userProperties.clear();
184         }
185 
186         return this;
187     }
188 
189     @Override
190     public boolean isProcessPlugins() {
191         return processPlugins;
192     }
193 
194     @Override
195     public ProjectBuildingRequest setProcessPlugins(boolean processPlugins) {
196         this.processPlugins = processPlugins;
197         return this;
198     }
199 
200     @Override
201     public ProjectBuildingRequest setResolveDependencies(boolean resolveDependencies) {
202         this.resolveDependencies = resolveDependencies;
203         return this;
204     }
205 
206     @Override
207     public boolean isResolveDependencies() {
208         return resolveDependencies;
209     }
210 
211     /**
212      * @since 3.2.2
213      * @deprecated This got added when implementing MNG-2199 and is no longer used.
214      * Commit 6cf9320942c34bc68205425ab696b1712ace9ba4 updated the way 'MavenProject' objects are initialized.
215      */
216     @Deprecated
217     @Override
218     public ProjectBuildingRequest setResolveVersionRanges(boolean value) {
219         this.resolveVersionRanges = value;
220         return this;
221     }
222 
223     /**
224      * @since 3.2.2
225      * @deprecated This got added when implementing MNG-2199 and is no longer used.
226      * Commit 6cf9320942c34bc68205425ab696b1712ace9ba4 updated the way 'MavenProject' objects are initialized.
227      */
228     @Deprecated
229     @Override
230     public boolean isResolveVersionRanges() {
231         return this.resolveVersionRanges;
232     }
233 
234     @Override
235     public ProjectBuildingRequest setValidationLevel(int validationLevel) {
236         this.validationLevel = validationLevel;
237         return this;
238     }
239 
240     @Override
241     public int getValidationLevel() {
242         return validationLevel;
243     }
244 
245     @Override
246     public List<String> getActiveProfileIds() {
247         return activeProfileIds;
248     }
249 
250     @Override
251     public void setActiveProfileIds(List<String> activeProfileIds) {
252         if (activeProfileIds != null) {
253             this.activeProfileIds = new ArrayList<>(activeProfileIds);
254         } else {
255             this.activeProfileIds.clear();
256         }
257     }
258 
259     @Override
260     public List<String> getInactiveProfileIds() {
261         return inactiveProfileIds;
262     }
263 
264     @Override
265     public void setInactiveProfileIds(List<String> inactiveProfileIds) {
266         if (inactiveProfileIds != null) {
267             this.inactiveProfileIds = new ArrayList<>(inactiveProfileIds);
268         } else {
269             this.inactiveProfileIds.clear();
270         }
271     }
272 
273     @Override
274     public void setProfiles(List<Profile> profiles) {
275         if (profiles != null) {
276             this.profiles = new ArrayList<>(profiles);
277         } else {
278             this.profiles.clear();
279         }
280     }
281 
282     @Override
283     public void addProfile(Profile profile) {
284         profiles.add(profile);
285     }
286 
287     @Override
288     public List<Profile> getProfiles() {
289         return profiles;
290     }
291 
292     @Deprecated
293     @Override
294     public Date getBuildStartTime() {
295         return buildStartTime != null ? new Date(buildStartTime.toEpochMilli()) : null;
296     }
297 
298     @Deprecated
299     @Override
300     public void setBuildStartTime(Date buildStartTime) {
301         setBuildStartInstant(buildStartTime != null ? Instant.ofEpochMilli(buildStartTime.getTime()) : null);
302     }
303 
304     @Override
305     public Instant getBuildStartInstant() {
306         return this.buildStartTime;
307     }
308 
309     @Override
310     public void setBuildStartInstant(Instant buildStartTime) {
311         this.buildStartTime = buildStartTime;
312     }
313 
314     @Override
315     public RepositorySystemSession getRepositorySession() {
316         return repositorySession;
317     }
318 
319     @Override
320     public DefaultProjectBuildingRequest setRepositorySession(RepositorySystemSession repositorySession) {
321         this.repositorySession = repositorySession;
322         return this;
323     }
324 
325     @Override
326     public DefaultProjectBuildingRequest setRepositoryMerging(RepositoryMerging repositoryMerging) {
327         this.repositoryMerging = Objects.requireNonNull(repositoryMerging, "repositoryMerging cannot be null");
328         return this;
329     }
330 
331     @Override
332     public RepositoryMerging getRepositoryMerging() {
333         return repositoryMerging;
334     }
335 }