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