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.plugin.MojoExecutionException;
23  import org.apache.maven.plugin.dependency.utils.DependencyStatusSets;
24  import org.apache.maven.plugin.dependency.utils.DependencyUtil;
25  import org.apache.maven.plugin.dependency.utils.filters.ResolveFileFilter;
26  import org.apache.maven.plugin.dependency.utils.markers.SourcesFileMarkerHandler;
27  import org.apache.maven.plugins.annotations.LifecyclePhase;
28  import org.apache.maven.plugins.annotations.Mojo;
29  import org.apache.maven.plugins.annotations.Parameter;
30  import org.apache.maven.plugins.annotations.ResolutionScope;
31  import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter;
32  
33  import java.io.IOException;
34  
35  /**
36   * Goal that resolves the project dependencies from the repository.
37   *
38   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
39   * @version $Id: ResolveDependenciesMojo.html 937155 2015-01-21 21:53:50Z khmarbaise $
40   * @since 2.0
41   */
42  @Mojo( name = "resolve", requiresDependencyResolution = ResolutionScope.TEST,
43         defaultPhase = LifecyclePhase.GENERATE_SOURCES, threadSafe = true )
44  public class ResolveDependenciesMojo
45      extends AbstractResolveMojo
46  {
47  
48      /**
49       * If we should display the scope when resolving
50       *
51       * @since 2.0-alpha-2
52       */
53      @Parameter( property = "mdep.outputScope", defaultValue = "true" )
54      protected boolean outputScope;
55  
56      /**
57       * Only used to store results for integration test validation
58       */
59      DependencyStatusSets results;
60  
61      /**
62       * Sort the output list of resolved artifacts alphabetically.
63       * The default ordering matches the classpath order.
64       * 
65       * @since 2.8
66       */
67      @Parameter( property = "sort", defaultValue = "false" )
68      boolean sort;
69  
70      /**
71       * Include parent poms in the dependency resolution list.
72       * 
73       * @since 2.8
74       */
75      @Parameter( property = "includeParents", defaultValue = "false" )
76      boolean includeParents;
77  
78      /**
79       * Main entry into mojo. Gets the list of dependencies and iterates through displaying the resolved version.
80       *
81       * @throws MojoExecutionException with a message if an error occurs.
82       */
83      protected void doExecute()
84          throws MojoExecutionException
85      {
86          // get sets of dependencies
87          results = this.getDependencySets( false, includeParents );
88  
89          String output = results.getOutput( outputAbsoluteArtifactFilename, outputScope, sort );
90          try
91          {
92              if ( outputFile == null )
93              {
94                  DependencyUtil.log( output, getLog() );
95              }
96              else
97              {
98                  DependencyUtil.write( output, outputFile, appendOutput, getLog() );
99              }
100         }
101         catch ( IOException e )
102         {
103             throw new MojoExecutionException( e.getMessage(), e );
104         }
105     }
106 
107     /**
108      * @return Returns the results.
109      */
110     public DependencyStatusSets getResults()
111     {
112         return this.results;
113     }
114 
115     protected ArtifactsFilter getMarkedArtifactFilter()
116     {
117         return new ResolveFileFilter( new SourcesFileMarkerHandler( this.markersDirectory ) );
118     }
119 }