View Javadoc

1   package org.apache.maven.plugin.dependency.resolvers;
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 java.io.IOException;
23  import java.util.Iterator;
24  
25  import org.apache.maven.artifact.Artifact;
26  import org.apache.maven.plugin.MojoExecutionException;
27  import org.apache.maven.plugin.dependency.AbstractResolveMojo;
28  import org.apache.maven.plugin.dependency.utils.DependencyStatusSets;
29  import org.apache.maven.plugin.dependency.utils.DependencyUtil;
30  import org.apache.maven.plugin.dependency.utils.filters.ResolveFileFilter;
31  import org.apache.maven.plugin.dependency.utils.markers.SourcesFileMarkerHandler;
32  import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter;
33  
34  /**
35   * Goal that resolves the project source dependencies from the repository.
36   * 
37   * @goal sources
38   * @phase generate-sources
39   * @requiresDependencyResolution test
40   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
41   * @version $Id: ResolveDependencySourcesMojo.java 728546 2008-12-21 22:56:51Z bentmann $
42   * @since 2.0-alpha2
43   */
44  public class ResolveDependencySourcesMojo
45      extends AbstractResolveMojo
46  {
47  
48      private static final String SOURCE_TYPE = "java-source";
49  
50      private static final String SOURCE_CLASSIFIER = "sources";
51  
52      /**
53       * Only used to store results for integration test validation
54       */
55      DependencyStatusSets results;
56      
57      /**
58       * Main entry into mojo. Gets the list of dependencies and iterates through
59       * resolving the source jars.
60       * 
61       * @throws MojoExecutionException
62       *             with a message if an error occurs.
63       * 
64       */
65      public void execute()
66          throws MojoExecutionException
67      {
68          this.classifier = SOURCE_CLASSIFIER;
69          this.type = SOURCE_TYPE;
70          // get sets of dependencies
71          results = this.getDependencySets( false );
72          
73          SourcesFileMarkerHandler handler = new SourcesFileMarkerHandler(this.markersDirectory);
74          handler.setResolved( true );
75          
76          Iterator iter = results.getResolvedDependencies().iterator();
77          while (iter.hasNext())
78          {
79              Artifact artifact = (Artifact) iter.next();
80              handler.setArtifact( artifact );
81              handler.setMarker();
82          }
83          
84          handler.setResolved( false );
85          iter = results.getUnResolvedDependencies().iterator();
86          while (iter.hasNext())
87          {
88              Artifact artifact = (Artifact) iter.next();
89              handler.setArtifact( artifact );
90              handler.setMarker();
91          }
92  
93          String output = results.getOutput( outputAbsoluteArtifactFilename, false );
94          try
95          {
96              if ( outputFile == null )
97              {
98                  DependencyUtil.log( output, getLog() );
99              }
100             else
101             {
102                 DependencyUtil.write( output, outputFile, getLog() );
103             }
104         }
105         catch ( IOException e )
106         {
107             throw new MojoExecutionException(e.getMessage(),e);
108         }
109     }
110 
111     protected ArtifactsFilter getMarkedArtifactFilter()
112     {
113         return new ResolveFileFilter( new SourcesFileMarkerHandler( this.markersDirectory ) );
114     }
115 }