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.artifact;
20  
21  import java.io.File;
22  import java.util.Collection;
23  import java.util.List;
24  
25  import org.apache.maven.artifact.Artifact;
26  import org.apache.maven.artifact.handler.ArtifactHandler;
27  import org.apache.maven.artifact.metadata.ArtifactMetadata;
28  import org.apache.maven.artifact.repository.ArtifactRepository;
29  import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
30  import org.apache.maven.artifact.versioning.ArtifactVersion;
31  import org.apache.maven.artifact.versioning.OverConstrainedVersionException;
32  import org.apache.maven.artifact.versioning.VersionRange;
33  import org.apache.maven.project.MavenProject;
34  
35  /**
36   * Wraps an active project instance to be able to receive updates from its artifact without affecting the original
37   * attributes of this artifact.
38   *
39   * TODO I think this exposes a design flaw in that the immutable and mutable parts of an artifact are in one class and
40   * should be split. ie scope, file, etc depend on the context of use, whereas everything else is immutable.
41   */
42  @Deprecated
43  public class ActiveProjectArtifact implements Artifact {
44      private final Artifact artifact;
45  
46      private final MavenProject project;
47  
48      public ActiveProjectArtifact(MavenProject project, Artifact artifact) {
49          this.artifact = artifact;
50          this.project = project;
51  
52          artifact.setFile(project.getArtifact().getFile());
53          artifact.setResolved(true);
54      }
55  
56      /** {@inheritDoc} */
57      public File getFile() {
58          // we need to get the latest file for the project, not the artifact that was created at one point in time
59          return project.getArtifact().getFile();
60      }
61  
62      /** {@inheritDoc} */
63      public String getGroupId() {
64          return artifact.getGroupId();
65      }
66  
67      /** {@inheritDoc} */
68      public String getArtifactId() {
69          return artifact.getArtifactId();
70      }
71  
72      /** {@inheritDoc} */
73      public String getVersion() {
74          return artifact.getVersion();
75      }
76  
77      /** {@inheritDoc} */
78      public void setVersion(String version) {
79          artifact.setVersion(version);
80      }
81  
82      /** {@inheritDoc} */
83      public String getScope() {
84          return artifact.getScope();
85      }
86  
87      /** {@inheritDoc} */
88      public String getType() {
89          return artifact.getType();
90      }
91  
92      /** {@inheritDoc} */
93      public String getClassifier() {
94          return artifact.getClassifier();
95      }
96  
97      /** {@inheritDoc} */
98      public boolean hasClassifier() {
99          return artifact.hasClassifier();
100     }
101 
102     /** {@inheritDoc} */
103     public void setFile(File destination) {
104         artifact.setFile(destination);
105         project.getArtifact().setFile(destination);
106     }
107 
108     /** {@inheritDoc} */
109     public String getBaseVersion() {
110         return artifact.getBaseVersion();
111     }
112 
113     /** {@inheritDoc} */
114     public void setBaseVersion(String baseVersion) {
115         artifact.setBaseVersion(baseVersion);
116     }
117 
118     /** {@inheritDoc} */
119     public String getId() {
120         return artifact.getId();
121     }
122 
123     /** {@inheritDoc} */
124     public String getDependencyConflictId() {
125         return artifact.getDependencyConflictId();
126     }
127 
128     /** {@inheritDoc} */
129     public void addMetadata(ArtifactMetadata metadata) {
130         artifact.addMetadata(metadata);
131     }
132 
133     /** {@inheritDoc} */
134     public Collection<ArtifactMetadata> getMetadataList() {
135         return artifact.getMetadataList();
136     }
137 
138     /** {@inheritDoc} */
139     public void setRepository(ArtifactRepository remoteRepository) {
140         artifact.setRepository(remoteRepository);
141     }
142 
143     /** {@inheritDoc} */
144     public ArtifactRepository getRepository() {
145         return artifact.getRepository();
146     }
147 
148     /** {@inheritDoc} */
149     public void updateVersion(String version, ArtifactRepository localRepository) {
150         artifact.updateVersion(version, localRepository);
151     }
152 
153     /** {@inheritDoc} */
154     public String getDownloadUrl() {
155         return artifact.getDownloadUrl();
156     }
157 
158     /** {@inheritDoc} */
159     public void setDownloadUrl(String downloadUrl) {
160         artifact.setDownloadUrl(downloadUrl);
161     }
162 
163     /** {@inheritDoc} */
164     public ArtifactFilter getDependencyFilter() {
165         return artifact.getDependencyFilter();
166     }
167 
168     /** {@inheritDoc} */
169     public void setDependencyFilter(ArtifactFilter artifactFilter) {
170         artifact.setDependencyFilter(artifactFilter);
171     }
172 
173     /** {@inheritDoc} */
174     public ArtifactHandler getArtifactHandler() {
175         return artifact.getArtifactHandler();
176     }
177 
178     /** {@inheritDoc} */
179     public List<String> getDependencyTrail() {
180         return artifact.getDependencyTrail();
181     }
182 
183     /** {@inheritDoc} */
184     public void setDependencyTrail(List<String> dependencyTrail) {
185         artifact.setDependencyTrail(dependencyTrail);
186     }
187 
188     /** {@inheritDoc} */
189     public void setScope(String scope) {
190         artifact.setScope(scope);
191     }
192 
193     /** {@inheritDoc} */
194     public VersionRange getVersionRange() {
195         return artifact.getVersionRange();
196     }
197 
198     /** {@inheritDoc} */
199     public void setVersionRange(VersionRange newRange) {
200         artifact.setVersionRange(newRange);
201     }
202 
203     /** {@inheritDoc} */
204     public void selectVersion(String version) {
205         artifact.selectVersion(version);
206     }
207 
208     /** {@inheritDoc} */
209     public void setGroupId(String groupId) {
210         artifact.setGroupId(groupId);
211     }
212 
213     /** {@inheritDoc} */
214     public void setArtifactId(String artifactId) {
215         artifact.setArtifactId(artifactId);
216     }
217 
218     /** {@inheritDoc} */
219     public boolean isSnapshot() {
220         return artifact.isSnapshot();
221     }
222 
223     /** {@inheritDoc} */
224     public int compareTo(Artifact a) {
225         return artifact.compareTo(a);
226     }
227 
228     /** {@inheritDoc} */
229     public void setResolved(boolean resolved) {
230         artifact.setResolved(resolved);
231     }
232 
233     /** {@inheritDoc} */
234     public boolean isResolved() {
235         return artifact.isResolved();
236     }
237 
238     /** {@inheritDoc} */
239     public void setResolvedVersion(String version) {
240         artifact.setResolvedVersion(version);
241     }
242 
243     /** {@inheritDoc} */
244     public void setArtifactHandler(ArtifactHandler handler) {
245         artifact.setArtifactHandler(handler);
246     }
247 
248     /** {@inheritDoc} */
249     public String toString() {
250         return "active project artifact[artifact: " + artifact + ", project: " + project + "]";
251     }
252 
253     /** {@inheritDoc} */
254     public boolean isRelease() {
255         return artifact.isRelease();
256     }
257 
258     /** {@inheritDoc} */
259     public void setRelease(boolean release) {
260         artifact.setRelease(release);
261     }
262 
263     /** {@inheritDoc} */
264     public List<ArtifactVersion> getAvailableVersions() {
265         return artifact.getAvailableVersions();
266     }
267 
268     /** {@inheritDoc} */
269     public void setAvailableVersions(List<ArtifactVersion> versions) {
270         artifact.setAvailableVersions(versions);
271     }
272 
273     /** {@inheritDoc} */
274     public boolean isOptional() {
275         return artifact.isOptional();
276     }
277 
278     /** {@inheritDoc} */
279     public ArtifactVersion getSelectedVersion() throws OverConstrainedVersionException {
280         return artifact.getSelectedVersion();
281     }
282 
283     /** {@inheritDoc} */
284     public boolean isSelectedVersionKnown() throws OverConstrainedVersionException {
285         return artifact.isSelectedVersionKnown();
286     }
287 
288     /** {@inheritDoc} */
289     public void setOptional(boolean optional) {
290         artifact.setOptional(optional);
291     }
292 
293     /** {@inheritDoc} */
294     public int hashCode() {
295         int result = 17;
296 
297         result = 37 * result + getGroupId().hashCode();
298         result = 37 * result + getArtifactId().hashCode();
299         result = 37 * result + getType().hashCode();
300         if (getVersion() != null) {
301             result = 37 * result + getVersion().hashCode();
302         }
303         result = 37 * result + (getClassifier() != null ? getClassifier().hashCode() : 0);
304 
305         return result;
306     }
307 
308     /** {@inheritDoc} */
309     public boolean equals(Object o) {
310         if (o == this) {
311             return true;
312         }
313 
314         if (!(o instanceof Artifact)) {
315             return false;
316         }
317 
318         Artifact a = (Artifact) o;
319 
320         if (!a.getGroupId().equals(getGroupId())) {
321             return false;
322         } else if (!a.getArtifactId().equals(getArtifactId())) {
323             return false;
324         } else if (!a.getVersion().equals(getVersion())) {
325             return false;
326         } else if (!a.getType().equals(getType())) {
327             return false;
328         } else {
329             return a.getClassifier() == null
330                     ? getClassifier() == null
331                     : a.getClassifier().equals(getClassifier());
332         }
333     }
334 }