View Javadoc
1   package org.apache.maven.plugins.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.plugins.dependency.utils.DependencyStatusSets;
25  import org.apache.maven.plugins.dependency.utils.DependencyUtil;
26  import org.apache.maven.plugins.dependency.utils.filters.MarkerFileFilter;
27  import org.apache.maven.plugins.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 location.
38   *
39   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
40   * @since 1.0
41   */
42  //CHECKSTYLE_OFF: LineLength
43  @Mojo( name = "unpack-dependencies", requiresDependencyResolution = ResolutionScope.TEST, defaultPhase = LifecyclePhase.PROCESS_SOURCES, threadSafe = true )
44  //CHECKSTYLE_ON: LineLength
45  public class UnpackDependenciesMojo
46      extends AbstractFromDependenciesMojo
47  {
48      /**
49       * A comma separated list of file patterns to include when unpacking the artifact. i.e.
50       * <code>**&#47;*.xml,**&#47;*.properties</code> NOTE: Excludes patterns override the includes. (component code =
51       * <code>return isIncluded( name ) AND !isExcluded( name );</code>)
52       *
53       * @since 2.0
54       */
55      @Parameter( property = "mdep.unpack.includes" )
56      private String includes;
57  
58      /**
59       * A comma separated list of file patterns to exclude when unpacking the artifact. i.e.
60       * <code>**&#47;*.xml,**&#47;*.properties</code> NOTE: Excludes patterns override the includes. (component code =
61       * <code>return isIncluded( name ) AND !isExcluded( name );</code>)
62       *
63       * @since 2.0
64       */
65      @Parameter( property = "mdep.unpack.excludes" )
66      private String excludes;
67  
68      /**
69       * Encoding of artifacts.
70       * 
71       * @since 3.0
72       */
73      @Parameter( property = "mdep.unpack.encoding" )
74      private String encoding;
75  
76      /**
77       * Main entry into mojo. This method gets the dependencies and iterates through each one passing it to
78       * DependencyUtil.unpackFile().
79       *
80       * @throws MojoExecutionException with a message if an error occurs.
81       * @see #getDependencySets(boolean)
82       * @see #unpack(Artifact, File, String)
83       */
84      @Override
85      protected void doExecute()
86          throws MojoExecutionException
87      {
88          DependencyStatusSets dss = getDependencySets( this.failOnMissingClassifierArtifact );
89  
90          for ( Artifact artifact : dss.getResolvedDependencies() )
91          {
92              File destDir;
93              destDir = DependencyUtil.getFormattedOutputDirectory( useSubDirectoryPerScope, useSubDirectoryPerType,
94                                                                    useSubDirectoryPerArtifact, useRepositoryLayout,
95                                                                    stripVersion, outputDirectory, artifact );
96              unpack( artifact, destDir, getIncludes(), getExcludes(), getEncoding() );
97              DefaultFileMarkerHandler handler = new DefaultFileMarkerHandler( artifact, this.markersDirectory );
98              handler.setMarker();
99          }
100 
101         for ( Artifact artifact : dss.getSkippedDependencies() )
102         {
103             getLog().info( artifact.getId() + " already exists in destination." );
104         }
105     }
106 
107     @Override
108     protected ArtifactsFilter getMarkedArtifactFilter()
109     {
110         return new MarkerFileFilter( this.overWriteReleases, this.overWriteSnapshots, this.overWriteIfNewer,
111                                      new DefaultFileMarkerHandler( this.markersDirectory ) );
112     }
113 
114     /**
115      * @return Returns a comma separated list of excluded items
116      */
117     public String getExcludes()
118     {
119         return DependencyUtil.cleanToBeTokenizedString( this.excludes );
120     }
121 
122     /**
123      * @param excludes A comma separated list of items to exclude i.e. <code>**\/*.xml, **\/*.properties</code>
124      */
125     public void setExcludes( String excludes )
126     {
127         this.excludes = excludes;
128     }
129 
130     /**
131      * @return Returns a comma separated list of included items
132      */
133     public String getIncludes()
134     {
135         return DependencyUtil.cleanToBeTokenizedString( this.includes );
136     }
137 
138     /**
139      * @param includes A comma separated list of items to include i.e. <code>**\/*.xml, **\/*.properties</code>
140      */
141     public void setIncludes( String includes )
142     {
143         this.includes = includes;
144     }
145 
146     /**
147      * @param encoding The encoding to set.
148      * @since 3.0
149      */
150     public void setEncoding( String encoding )
151     {
152         this.encoding = encoding;
153     }
154 
155     /**
156      * @return Returns the encoding.
157      * @since 3.0
158      */
159     public String getEncoding()
160     {
161         return this.encoding;
162     }
163 }