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.manager.WagonManager;
23  import org.apache.maven.plugin.AbstractMojo;
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugin.MojoFailureException;
26  import org.apache.maven.wagon.Wagon;
27  import org.apache.maven.wagon.repository.Repository;
28  
29  import java.io.File;
30  import java.io.FileOutputStream;
31  import java.io.IOException;
32  import java.io.OutputStream;
33  import java.util.Properties;
34  
35  /**
36   * Loads resources from a class loader used to load a wagon provider. The wagon is merely used to access the extension
37   * class loader it came from which is otherwise not accessible to a plugin.
38   *
39   * @goal lookup-wagon
40   * @phase validate
41   *
42   * @author Benjamin Bentmann
43   *
44   */
45  public class LookupWagonMojo
46      extends AbstractMojo
47  {
48  
49      /**
50       * The Wagon manager used to retrieve wagon providers.
51       *
52       * @component
53       */
54      private WagonManager wagonManager;
55  
56      /**
57       * The path to the properties file used to track the results of the wagon lookups.
58       *
59       * @parameter property="wagon.outputFile"
60       */
61      private File outputFile;
62  
63      /**
64       * The URLs for which to look up wagons.
65       *
66       * @parameter
67       */
68      private String[] urls;
69  
70      /**
71       * Runs this mojo.
72       *
73       * @throws MojoFailureException If the attached file has not been set.
74       */
75      public void execute()
76          throws MojoExecutionException, MojoFailureException
77      {
78          Properties loaderProperties = new Properties();
79  
80          if ( urls != null )
81          {
82              for ( int i = 0; i < urls.length; i++ )
83              {
84                  String url = urls[i];
85                  getLog().info( "[MAVEN-CORE-IT-LOG] Looking up wagon for URL " + url );
86  
87                  try
88                  {
89                      Repository repo = new Repository( "repo-" + i, url );
90                      Wagon wagon = wagonManager.getWagon( repo );
91                      getLog().info( "[MAVEN-CORE-IT-LOG]   " + wagon );
92  
93                      loaderProperties.setProperty( url + ".hash", Integer.toString( System.identityHashCode( wagon ) ) );
94                      loaderProperties.setProperty( url + ".class", wagon.getClass().getName() );
95                  }
96                  catch ( Exception e )
97                  {
98                      getLog().warn( "[MAVEN-CORE-IT-LOG] Failed to lookp up wagon for URL " + url, e );
99                  }
100             }
101         }
102 
103         getLog().info( "[MAVEN-CORE-IT-LOG] Creating output file " + outputFile );
104 
105         OutputStream out = null;
106         try
107         {
108             outputFile.getParentFile().mkdirs();
109             out = new FileOutputStream( outputFile );
110             loaderProperties.store( out, "MAVEN-CORE-IT-LOG" );
111         }
112         catch ( IOException e )
113         {
114             throw new MojoExecutionException( "Output file could not be created: " + outputFile, e );
115         }
116         finally
117         {
118             if ( out != null )
119             {
120                 try
121                 {
122                     out.close();
123                 }
124                 catch ( IOException e )
125                 {
126                     // just ignore
127                 }
128             }
129         }
130 
131         getLog().info( "[MAVEN-CORE-IT-LOG] Created output file " + outputFile );
132     }
133 
134 }