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.metadata.ArtifactMetadataSource;
25  import org.apache.maven.artifact.repository.ArtifactRepository;
26  import org.apache.maven.artifact.resolver.ArtifactResolver;
27  import org.apache.maven.model.Dependency;
28  import org.apache.maven.plugin.AbstractMojo;
29  import org.apache.maven.plugin.MojoExecutionException;
30  
31  import java.io.File;
32  import java.io.FileOutputStream;
33  import java.io.IOException;
34  import java.util.Iterator;
35  import java.util.LinkedHashSet;
36  import java.util.List;
37  import java.util.Properties;
38  import java.util.Set;
39  
40  /**
41   * Resolves user-specified artifacts transitively. As an additional exercise, the resolution happens in a forked thread
42   * to test access to any shared session state.
43   * 
44   * @goal resolve-transitive
45   * 
46   * @author Benjamin Bentmann
47   */
48  public class ResolveTransitiveMojo
49      extends AbstractMojo
50  {
51  
52      /**
53       * The local repository.
54       * 
55       * @parameter default-value="${localRepository}"
56       * @readonly
57       * @required
58       */
59      private ArtifactRepository localRepository;
60  
61      /**
62       * The remote repositories of the current Maven project.
63       * 
64       * @parameter default-value="${project.remoteArtifactRepositories}"
65       * @readonly
66       * @required
67       */
68      private List remoteRepositories;
69  
70      /**
71       * The artifact resolver.
72       * 
73       * @component
74       */
75      private ArtifactResolver resolver;
76  
77      /**
78       * The artifact factory.
79       * 
80       * @component
81       */
82      private ArtifactFactory factory;
83  
84      /**
85       * The metadata source.
86       * 
87       * @component
88       */
89      private ArtifactMetadataSource metadataSource;
90  
91      /**
92       * The dependencies to resolve.
93       * 
94       * @parameter
95       */
96      private Dependency[] dependencies;
97  
98      /**
99       * The path to a properties file to store the resolved artifact paths in.
100      * 
101      * @parameter
102      */
103     private File propertiesFile;
104 
105     /**
106      * Runs this mojo.
107      * 
108      * @throws MojoExecutionException If the artifacts couldn't be resolved.
109      */
110     public void execute()
111         throws MojoExecutionException
112     {
113         getLog().info( "[MAVEN-CORE-IT-LOG] Resolving artifacts" );
114 
115         ResolverThread thread = new ResolverThread();
116         thread.start();
117         while ( thread.isAlive() )
118         {
119             try
120             {
121                 thread.join();
122             }
123             catch ( InterruptedException e )
124             {
125                 e.printStackTrace();
126             }
127         }
128 
129         if ( thread.error != null )
130         {
131             throw new MojoExecutionException( "Failed to resolve artifacts: " + thread.error.getMessage(), thread.error );
132         }
133 
134         if ( propertiesFile != null )
135         {
136             getLog().info( "[MAVEN-CORE-IT-LOG] Creating properties file " + propertiesFile );
137 
138             try
139             {
140                 propertiesFile.getParentFile().mkdirs();
141 
142                 FileOutputStream fos = new FileOutputStream( propertiesFile );
143                 try
144                 {
145                     thread.props.store( fos, "MAVEN-CORE-IT" );
146                 }
147                 finally
148                 {
149                     fos.close();
150                 }
151             }
152             catch ( IOException e )
153             {
154                 throw new MojoExecutionException( "Failed to create properties file: " + e.getMessage(), e );
155             }
156         }
157     }
158 
159     private String getId( Artifact artifact )
160     {
161         artifact.isSnapshot(); // decouple from MNG-2961
162         return artifact.getId();
163     }
164 
165     class ResolverThread
166         extends Thread
167     {
168 
169         Properties props = new Properties();
170 
171         Exception error;
172 
173         public void run()
174         {
175             if ( dependencies != null )
176             {
177                 try
178                 {
179                     Set artifacts = new LinkedHashSet();
180 
181                     for ( int i = 0; i < dependencies.length; i++ )
182                     {
183                         Dependency dependency = dependencies[i];
184 
185                         Artifact artifact =
186                             factory.createArtifactWithClassifier( dependency.getGroupId(), dependency.getArtifactId(),
187                                                                   dependency.getVersion(), dependency.getType(),
188                                                                   dependency.getClassifier() );
189 
190                         getLog().info( "[MAVEN-CORE-IT-LOG] Resolving " + ResolveTransitiveMojo.this.getId( artifact ) );
191 
192                         artifacts.add( artifact );
193                     }
194 
195                     Artifact origin = factory.createArtifact( "it", "it", "0.1", null, "pom" );
196 
197                     artifacts =
198                         resolver.resolveTransitively( artifacts, origin, remoteRepositories, localRepository,
199                                                       metadataSource ).getArtifacts();
200 
201                     for ( Iterator it = artifacts.iterator(); it.hasNext(); )
202                     {
203                         Artifact artifact = (Artifact) it.next();
204 
205                         if ( artifact.getFile() != null )
206                         {
207                             props.setProperty( ResolveTransitiveMojo.this.getId( artifact ),
208                                                artifact.getFile().getPath() );
209                         }
210 
211                         getLog().info( "[MAVEN-CORE-IT-LOG]   " + artifact.getFile() );
212                     }
213                 }
214                 catch ( Exception e )
215                 {
216                     error = e;
217                 }
218             }
219         }
220     }
221 
222 }