View Javadoc
1   package org.apache.maven.plugin.dependency.fromDependencies;
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.utils.DependencyStatusSets;
25  import org.apache.maven.plugin.dependency.utils.DependencyUtil;
26  import org.apache.maven.plugin.dependency.utils.filters.MarkerFileFilter;
27  import org.apache.maven.plugin.dependency.utils.markers.DefaultFileMarkerHandler;
28  import org.apache.maven.plugins.annotations.LifecyclePhase;
29  import org.apache.maven.plugins.annotations.Mojo;
30  import org.apache.maven.plugins.annotations.Parameter;
31  import org.apache.maven.plugins.annotations.ResolutionScope;
32  import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter;
33  
34  import java.io.File;
35  
36  /**
37   * Goal that unpacks the project dependencies from the repository to a defined
38   * location.
39   *
40   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
41   * @version $Id: UnpackDependenciesMojo.html 937155 2015-01-21 21:53:50Z khmarbaise $
42   * @since 1.0
43   */
44  @Mojo( name = "unpack-dependencies", requiresDependencyResolution = ResolutionScope.TEST,
45         defaultPhase = LifecyclePhase.PROCESS_SOURCES, threadSafe = true )
46  public class UnpackDependenciesMojo
47      extends AbstractFromDependenciesMojo
48  {
49      /**
50       * A comma separated list of file patterns to include when unpacking the
51       * artifact.  i.e. <code>**\/*.xml,**\/*.properties</code>
52       * NOTE: Excludes patterns override the includes.
53       * (component code = <code>return isIncluded( name ) AND !isExcluded( name );</code>)
54       *
55       * @since 2.0
56       */
57      @Parameter( property = "mdep.unpack.includes" )
58      private String includes;
59  
60      /**
61       * A comma separated list of file patterns to exclude when unpacking the
62       * artifact.  i.e. <code>**\/*.xml,**\/*.properties</code>
63       * NOTE: Excludes patterns override the includes.
64       * (component code = <code>return isIncluded( name ) AND !isExcluded( name );</code>)
65       *
66       * @since 2.0
67       */
68      @Parameter( property = "mdep.unpack.excludes" )
69      private String excludes;
70  
71      /**
72       * Main entry into mojo. This method gets the dependencies and iterates
73       * through each one passing it to DependencyUtil.unpackFile().
74       *
75       * @throws MojoExecutionException with a message if an error occurs.
76       * @see #getDependencies
77       * @see DependencyUtil#unpackFile(Artifact, File, File, ArchiverManager,
78       *      Log)
79       */
80      protected void doExecute()
81          throws MojoExecutionException
82      {
83          DependencyStatusSets dss = getDependencySets( this.failOnMissingClassifierArtifact );
84  
85          for ( Artifact artifact : dss.getResolvedDependencies() )
86          {
87              File destDir;
88              destDir = DependencyUtil.getFormattedOutputDirectory( useSubDirectoryPerScope, useSubDirectoryPerType,
89                                                                    useSubDirectoryPerArtifact, useRepositoryLayout,
90                                                                    stripVersion, outputDirectory, artifact );
91              unpack( artifact, destDir, getIncludes(), getExcludes() );
92              DefaultFileMarkerHandler handler = new DefaultFileMarkerHandler( artifact, this.markersDirectory );
93              handler.setMarker();
94          }
95  
96          for ( Artifact artifact : dss.getSkippedDependencies() )
97          {
98              getLog().info( artifact.getFile().getName() + " already exists in destination." );
99          }
100     }
101 
102     protected ArtifactsFilter getMarkedArtifactFilter()
103     {
104         return new MarkerFileFilter( this.overWriteReleases, this.overWriteSnapshots, this.overWriteIfNewer,
105                                      new DefaultFileMarkerHandler( this.markersDirectory ) );
106     }
107 
108     /**
109      * @return Returns a comma separated list of excluded items
110      */
111     public String getExcludes()
112     {
113         return DependencyUtil.cleanToBeTokenizedString( this.excludes );
114     }
115 
116     /**
117      * @param excludes A comma separated list of items to exclude
118      *                 i.e. <code>**\/*.xml, **\/*.properties</code>
119      */
120     public void setExcludes( String excludes )
121     {
122         this.excludes = excludes;
123     }
124 
125     /**
126      * @return Returns a comma separated list of included items
127      */
128     public String getIncludes()
129     {
130         return DependencyUtil.cleanToBeTokenizedString( this.includes );
131     }
132 
133     /**
134      * @param includes A comma separated list of items to include
135      *                 i.e. <code>**\/*.xml, **\/*.properties</code>
136      */
137     public void setIncludes( String includes )
138     {
139         this.includes = includes;
140     }
141 }