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