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.repository.Repository;
27  
28  import java.io.File;
29  import java.io.FileOutputStream;
30  import java.io.IOException;
31  import java.io.OutputStream;
32  import java.net.URL;
33  import java.util.Collections;
34  import java.util.List;
35  import java.util.Properties;
36  
37  /**
38   * Loads resources from a class loader used to load a wagon provider. The wagon is merely used to access the extension
39   * class loader it came from which is otherwise not accessible to a plugin.
40   *
41   * @author Benjamin Bentmann
42   *
43   * @goal load-resource
44   * @phase validate
45   */
46  public class LoadResourceMojo
47      extends AbstractMojo
48  {
49  
50      /**
51       * The Wagon manager used to retrieve wagon providers.
52       *
53       * @component
54       */
55      private WagonManager wagonManager;
56  
57      /**
58       * The path to the properties file used to track the results of the resource loading via the wagon's class loader.
59       *
60       * @parameter property="wagon.wagonClassLoaderOutput"
61       */
62      private File wagonClassLoaderOutput;
63  
64      /**
65       * The role hint for the wagon provider to load. The class loader of this provider will be used to load the
66       * resources.
67       *
68       * @parameter property="wagon.wagonProtocol"
69       */
70      private String wagonProtocol;
71  
72      /**
73       * The repository to load the wagon for, if applicable.
74       *
75       * @parameter property="wagon.repositoryId"
76       */
77      private String repositoryId;
78  
79      /**
80       * The set of resources to load. For each specified absolute resource path <code>ARP</code> that was successfully
81       * loaded, the generated properties files will contain a key named <code>ARP</code> whose value gives the URL to the
82       * resource. In addition, the keys <code>ARP.count</code>, <code>ARP.0</code>, <code>ARP.1</code> etc. will
83       * enumerate all URLs matching the resource name.
84       *
85       * @parameter
86       */
87      private String[] resourcePaths;
88  
89      /**
90       * Runs this mojo.
91       *
92       * @throws MojoFailureException If the attached file has not been set.
93       */
94      public void execute()
95          throws MojoExecutionException, MojoFailureException
96      {
97          getLog().info( "[MAVEN-CORE-IT-LOG] Looking up wagon for protocol " + wagonProtocol );
98  
99          Object wagon;
100         try
101         {
102             if ( repositoryId != null )
103             {
104                 wagon = wagonManager.getWagon( new Repository( repositoryId, wagonProtocol + "://host/path" ) );
105             }
106             else
107             {
108                 wagon = wagonManager.getWagon( wagonProtocol );
109             }
110         }
111         catch ( Exception e )
112         {
113             throw new MojoExecutionException( "Failed to load wagon for protocol " + wagonProtocol, e );
114         }
115 
116         ClassLoader classLoader = wagon.getClass().getClassLoader();
117 
118         getLog().info( "[MAVEN-CORE-IT-LOG] Using class loader " + classLoader );
119 
120         Properties loaderProperties = new Properties();
121         loaderProperties.setProperty( "wagon.class", wagon.getClass().getName() );
122 
123         if ( resourcePaths != null )
124         {
125             for ( String path : resourcePaths )
126             {
127                 getLog().info( "[MAVEN-CORE-IT-LOG] Loading resource " + path );
128 
129                 URL url = classLoader.getResource( path );
130                 getLog().info( "[MAVEN-CORE-IT-LOG]   Loaded resource from " + url );
131                 if ( url != null )
132                 {
133                     loaderProperties.setProperty( path, url.toString() );
134                 }
135 
136                 try
137                 {
138                     List urls = Collections.list( classLoader.getResources( path ) );
139                     loaderProperties.setProperty( path + ".count", "" + urls.size() );
140                     for ( int j = 0; j < urls.size(); j++ )
141                     {
142                         loaderProperties.setProperty( path + "." + j, urls.get( j ).toString() );
143                     }
144                 }
145                 catch ( IOException e )
146                 {
147                     throw new MojoExecutionException( "Resources could not be enumerated: " + path, e );
148                 }
149             }
150         }
151 
152         getLog().info( "[MAVEN-CORE-IT-LOG] Creating output file " + wagonClassLoaderOutput );
153 
154         OutputStream out = null;
155         try
156         {
157             wagonClassLoaderOutput.getParentFile().mkdirs();
158             out = new FileOutputStream( wagonClassLoaderOutput );
159             loaderProperties.store( out, "MAVEN-CORE-IT-LOG" );
160         }
161         catch ( IOException e )
162         {
163             throw new MojoExecutionException( "Output file could not be created: " + wagonClassLoaderOutput, e );
164         }
165         finally
166         {
167             if ( out != null )
168             {
169                 try
170                 {
171                     out.close();
172                 }
173                 catch ( IOException e )
174                 {
175                     // just ignore
176                 }
177             }
178         }
179 
180         getLog().info( "[MAVEN-CORE-IT-LOG] Created output file " + wagonClassLoaderOutput );
181     }
182 
183 }