1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.googlecode.t7mp;
17
18 import java.io.File;
19 import java.util.List;
20
21 import org.apache.maven.artifact.Artifact;
22 import org.apache.maven.artifact.factory.ArtifactFactory;
23 import org.apache.maven.artifact.repository.ArtifactRepository;
24 import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
25 import org.apache.maven.artifact.resolver.ArtifactResolutionException;
26 import org.apache.maven.artifact.resolver.ArtifactResolver;
27 import org.apache.maven.artifact.versioning.VersionRange;
28 import org.apache.maven.plugin.MojoExecutionException;
29
30 import com.googlecode.t7mp.configuration.Artifacts;
31 import com.googlecode.t7mp.configuration.PluginArtifactResolver;
32 import com.googlecode.t7mp.configuration.ResolutionException;
33
34
35
36
37
38
39 public class MyArtifactResolver implements PluginArtifactResolver {
40
41 private final ArtifactResolver resolver;
42 private final ArtifactFactory factory;
43 private final ArtifactRepository local;
44 private final List<ArtifactRepository> remoteRepositories;
45
46 public MyArtifactResolver(AbstractT7BaseMojo t7BaseMojo) {
47 this.remoteRepositories = t7BaseMojo.getRemoteRepos();
48 this.local = t7BaseMojo.getLocal();
49 this.resolver = t7BaseMojo.getResolver();
50 this.factory = t7BaseMojo.getFactory();
51 }
52
53
54
55
56
57
58
59
60
61
62
63
64
65 public Artifact resolve(String groupId, String artifactId, String version, String classifier, String type, String scope) throws MojoExecutionException {
66 Artifact artifact = factory.createDependencyArtifact(groupId, artifactId, VersionRange.createFromVersion(version), type, classifier,
67 Artifact.SCOPE_COMPILE);
68 try {
69 resolver.resolve(artifact, remoteRepositories, local);
70 } catch (ArtifactResolutionException e) {
71 throw new MojoExecutionException(e.getMessage(), e);
72 } catch (ArtifactNotFoundException e) {
73 throw new MojoExecutionException(e.getMessage(), e);
74 }
75 return artifact;
76 }
77
78 public Artifact resolveJar(String groupId, String artifactId, String version, String classifier) throws MojoExecutionException {
79 return resolve(groupId, artifactId, version, classifier, "jar", Artifact.SCOPE_COMPILE);
80 }
81
82 public Artifact resolveWar(String groupId, String artifactId, String version, String classifier) throws MojoExecutionException {
83 return resolve(groupId, artifactId, version, classifier, "war", Artifact.SCOPE_COMPILE);
84 }
85
86 @Override
87 public File resolveArtifact(String coordinates) throws ResolutionException {
88 AbstractArtifact artifact = Artifacts.fromCoordinates(coordinates);
89 Artifact resolvedArtifact = null;
90 try {
91 resolvedArtifact = resolve(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getClassifier(), artifact.getType(),
92 null);
93 } catch (MojoExecutionException e) {
94 throw new ResolutionException(e.getMessage(), e);
95 }
96
97 return resolvedArtifact.getFile();
98 }
99 }