View Javadoc

1   package org.apache.maven.plugin.coreit;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.artifact.Artifact;
23  import org.apache.maven.artifact.factory.ArtifactFactory;
24  import org.apache.maven.artifact.repository.ArtifactRepository;
25  import org.apache.maven.artifact.resolver.ArtifactResolver;
26  import org.apache.maven.model.Dependency;
27  import org.apache.maven.plugin.AbstractMojo;
28  import org.apache.maven.plugin.MojoExecutionException;
29  
30  import java.io.File;
31  import java.io.FileOutputStream;
32  import java.io.IOException;
33  import java.util.List;
34  import java.util.Properties;
35  
36  /**
37   * Resolves user-specified artifacts. This mimics in part the Maven Dependency Plugin and the Maven Surefire Plugin.
38   * 
39   * @goal resolve
40   * 
41   * @author Benjamin Bentmann
42   * @version $Id: ResolveMojo.java 995456 2010-09-09 15:20:12Z bentmann $
43   */
44  public class ResolveMojo
45      extends AbstractMojo
46  {
47  
48      /**
49       * The local repository.
50       * 
51       * @parameter default-value="${localRepository}"
52       * @readonly
53       * @required
54       */
55      private ArtifactRepository localRepository;
56  
57      /**
58       * The remote repositories of the current Maven project.
59       * 
60       * @parameter default-value="${project.remoteArtifactRepositories}"
61       * @readonly
62       * @required
63       */
64      private List remoteRepositories;
65  
66      /**
67       * The artifact resolver.
68       * 
69       * @component
70       */
71      private ArtifactResolver resolver;
72  
73      /**
74       * The artifact factory.
75       * 
76       * @component
77       */
78      private ArtifactFactory factory;
79  
80      /**
81       * The dependencies to resolve.
82       * 
83       * @parameter
84       */
85      private Dependency[] dependencies;
86  
87      /**
88       * The path to a properties file to store the resolved artifact paths in.
89       * 
90       * @parameter
91       */
92      private File propertiesFile;
93  
94      /**
95       * Runs this mojo.
96       * 
97       * @throws MojoFailureException If the artifact could not be resolved
98       */
99      public void execute()
100         throws MojoExecutionException
101     {
102         getLog().info( "[MAVEN-CORE-IT-LOG] Resolving artifacts" );
103 
104         Properties props = new Properties();
105 
106         try
107         {
108             if ( dependencies != null )
109             {
110                 for ( int i = 0; i < dependencies.length; i++ )
111                 {
112                     Dependency dependency = dependencies[i];
113 
114                     Artifact artifact =
115                         factory.createArtifactWithClassifier( dependency.getGroupId(), dependency.getArtifactId(),
116                                                               dependency.getVersion(), dependency.getType(),
117                                                               dependency.getClassifier() );
118 
119                     getLog().info( "[MAVEN-CORE-IT-LOG] Resolving " + getId( artifact ) );
120 
121                     resolver.resolve( artifact, remoteRepositories, localRepository );
122 
123                     if ( artifact.getFile() != null )
124                     {
125                         props.setProperty( getId( artifact ), artifact.getFile().getPath() );
126                     }
127 
128                     getLog().info( "[MAVEN-CORE-IT-LOG]   " + artifact.getFile() );
129                 }
130             }
131         }
132         catch ( Exception e )
133         {
134             throw new MojoExecutionException( "Failed to resolve artifacts: " + e.getMessage(), e );
135         }
136 
137         if ( propertiesFile != null )
138         {
139             getLog().info( "[MAVEN-CORE-IT-LOG] Creating properties file " + propertiesFile );
140 
141             try
142             {
143                 propertiesFile.getParentFile().mkdirs();
144 
145                 FileOutputStream fos = new FileOutputStream( propertiesFile );
146                 try
147                 {
148                     props.store( fos, "MAVEN-CORE-IT" );
149                 }
150                 finally
151                 {
152                     fos.close();
153                 }
154             }
155             catch ( IOException e )
156             {
157                 throw new MojoExecutionException( "Failed to create properties file: " + e.getMessage(), e );
158             }
159         }
160     }
161 
162     private String getId( Artifact artifact )
163     {
164         artifact.isSnapshot(); // decouple from MNG-2961
165         return artifact.getId();
166     }
167 
168 }