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.lifecycle.internal;
20  
21  import java.util.*;
22  
23  import org.apache.maven.project.MavenProject;
24  
25  /**
26   * <p>
27   * Context of dependency artifacts for a particular project.
28   * </p>
29   * <strong>NOTE:</strong> This class is not part of any public api and can be changed or deleted without prior notice.
30   *
31   * @since 3.0
32   */
33  // TODO From a concurrency perspective, this class is not good. The combination of mutable/immutable state is not nice
34  public class DependencyContext {
35  
36      private final MavenProject project;
37  
38      private final Collection<String> scopesToCollectForCurrentProject;
39  
40      private final Collection<String> scopesToResolveForCurrentProject;
41  
42      private final Collection<String> scopesToCollectForAggregatedProjects;
43  
44      private final Collection<String> scopesToResolveForAggregatedProjects;
45  
46      private volatile Collection<?> lastDependencyArtifacts = Collections.emptyList();
47  
48      private volatile int lastDependencyArtifactCount = -1;
49  
50      public DependencyContext(
51              MavenProject project, Collection<String> scopesToCollect, Collection<String> scopesToResolve) {
52          this.project = project;
53          scopesToCollectForCurrentProject = scopesToCollect;
54          scopesToResolveForCurrentProject = scopesToResolve;
55          scopesToCollectForAggregatedProjects = Collections.synchronizedSet(new TreeSet<>());
56          scopesToResolveForAggregatedProjects = Collections.synchronizedSet(new TreeSet<>());
57      }
58  
59      public MavenProject getProject() {
60          return project;
61      }
62  
63      public Collection<String> getScopesToCollectForCurrentProject() {
64          return scopesToCollectForCurrentProject;
65      }
66  
67      public Collection<String> getScopesToResolveForCurrentProject() {
68          return scopesToResolveForCurrentProject;
69      }
70  
71      public Collection<String> getScopesToCollectForAggregatedProjects() {
72          return scopesToCollectForAggregatedProjects;
73      }
74  
75      public Collection<String> getScopesToResolveForAggregatedProjects() {
76          return scopesToResolveForAggregatedProjects;
77      }
78  
79      public boolean isResolutionRequiredForCurrentProject() {
80          return lastDependencyArtifacts != project.getDependencyArtifacts()
81                  || (lastDependencyArtifacts != null && lastDependencyArtifactCount != lastDependencyArtifacts.size());
82      }
83  
84      public boolean isResolutionRequiredForAggregatedProjects(
85              Collection<String> scopesToCollect, Collection<String> scopesToResolve) {
86          return scopesToCollectForAggregatedProjects.addAll(scopesToCollect)
87                  || scopesToResolveForAggregatedProjects.addAll(scopesToResolve);
88      }
89  
90      public void synchronizeWithProjectState() {
91          lastDependencyArtifacts = project.getDependencyArtifacts();
92          lastDependencyArtifactCount = (lastDependencyArtifacts != null) ? lastDependencyArtifacts.size() : 0;
93      }
94  }