View Javadoc

1   /**
2    * Copyright (C) 2010-2012 Joerg Bellmann <joerg.bellmann@googlemail.com>
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Uses Maven-API to resolve the Artifacts.
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       * resolves an Artifact from the repositorys.
55       * 
56       * @param groupId - Artifact GroupId
57       * @param artifactId - Artifact Id
58       * @param version - Artifact Version
59       * @param classifier - Artifact Version
60       * @param type - Artifact Type
61       * @param scope - Artifact Scope
62       * @return
63       * @throws MojoExecutionException
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  }