View Javadoc
1   package org.apache.maven.plugins.dependency.testUtils;
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.File;
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.Collection;
26  import java.util.List;
27  
28  import org.apache.maven.artifact.Artifact;
29  import org.apache.maven.artifact.ArtifactUtils;
30  import org.apache.maven.artifact.versioning.VersionRange;
31  import org.apache.maven.plugins.dependency.fromConfiguration.ArtifactItem;
32  import org.apache.maven.plugin.testing.ArtifactStubFactory;
33  
34  public class DependencyArtifactStubFactory
35      extends ArtifactStubFactory
36  {
37      private boolean flattenedPath = true;
38  
39      public DependencyArtifactStubFactory( File theWorkingDir, boolean theCreateFiles, boolean flattenedPath )
40      {
41          this( theWorkingDir, theCreateFiles );
42          this.flattenedPath = flattenedPath;
43      }
44  
45      /**
46       * @param theWorkingDir {@link File}
47       * @param theCreateFiles true/false.
48       */
49      public DependencyArtifactStubFactory( File theWorkingDir, boolean theCreateFiles )
50      {
51          super( theWorkingDir, theCreateFiles );
52      }
53  
54      public ArtifactItem getArtifactItem( Artifact artifact )
55      {
56          ArtifactItem item = new ArtifactItem( artifact );
57          return item;
58      }
59  
60      public List<ArtifactItem> getArtifactItems( Collection<Artifact> artifacts )
61      {
62          List<ArtifactItem> list = new ArrayList<ArtifactItem>();
63          for ( Artifact artifact : artifacts )
64          {
65              list.add( getArtifactItem( artifact ) );
66          }
67          return list;
68      }
69  
70      @Override
71      public Artifact createArtifact( String groupId, String artifactId, VersionRange versionRange, String scope,
72                                      String type, String classifier, boolean optional )
73          throws IOException
74      {
75          File workingDir = getWorkingDir();
76  
77          if ( !flattenedPath )
78          {
79              StringBuilder path = new StringBuilder( 128 );
80  
81              path.append( groupId.replace( '.', '/' ) ).append( '/' );
82  
83              path.append( artifactId ).append( '/' );
84  
85              path.append( ArtifactUtils.toSnapshotVersion( versionRange.getRecommendedVersion().toString() ) );
86  
87              // don't use flatten directories, won't happen at runtime
88              setWorkingDir( new File( workingDir, path.toString() ) );
89          }
90  
91          Artifact artifact =
92              super.createArtifact( groupId, artifactId, versionRange, scope, type, classifier, optional );
93  
94          setWorkingDir( workingDir );
95  
96          return artifact;
97      }
98  }