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.plugin.testing.ArtifactStubFactory;
32  import org.apache.maven.plugins.dependency.fromConfiguration.ArtifactItem;
33  import org.codehaus.plexus.archiver.Archiver;
34  import org.codehaus.plexus.archiver.ArchiverException;
35  import org.codehaus.plexus.archiver.manager.ArchiverManager;
36  import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
37  import org.codehaus.plexus.archiver.war.WarArchiver;
38  
39  public class DependencyArtifactStubFactory
40      extends ArtifactStubFactory
41  {
42      private boolean flattenedPath = true;
43      private ArchiverManager archiverManager;
44  
45      public DependencyArtifactStubFactory( File theWorkingDir, boolean theCreateFiles, boolean flattenedPath )
46      {
47          this( theWorkingDir, theCreateFiles );
48          this.flattenedPath = flattenedPath;
49      }
50  
51      /**
52       * @param theWorkingDir {@link File}
53       * @param theCreateFiles true/false.
54       */
55      public DependencyArtifactStubFactory( File theWorkingDir, boolean theCreateFiles )
56      {
57          super( theWorkingDir, theCreateFiles );
58      }
59  
60      public ArtifactItem getArtifactItem( Artifact artifact )
61      {
62          ArtifactItem item = new ArtifactItem( artifact );
63          return item;
64      }
65  
66      public List<ArtifactItem> getArtifactItems( Collection<Artifact> artifacts )
67      {
68          List<ArtifactItem> list = new ArrayList<>();
69          for ( Artifact artifact : artifacts )
70          {
71              list.add( getArtifactItem( artifact ) );
72          }
73          return list;
74      }
75  
76      @Override
77      public Artifact createArtifact( String groupId, String artifactId, VersionRange versionRange, String scope,
78                                      String type, String classifier, boolean optional )
79          throws IOException
80      {
81          File workingDir = getWorkingDir();
82  
83          if ( !flattenedPath )
84          {
85              StringBuilder path = new StringBuilder( 128 );
86  
87              path.append( groupId.replace( '.', '/' ) ).append( '/' );
88  
89              path.append( artifactId ).append( '/' );
90  
91              path.append( ArtifactUtils.toSnapshotVersion( versionRange.getRecommendedVersion().toString() ) );
92  
93              // don't use flatten directories, won't happen at runtime
94              setWorkingDir( new File( workingDir, path.toString() ) );
95          }
96  
97          Artifact artifact =
98              super.createArtifact( groupId, artifactId, versionRange, scope, type, classifier, optional );
99  
100         setWorkingDir( workingDir );
101 
102         return artifact;
103     }
104 
105 
106     @Override
107     public void setUnpackableFile( ArchiverManager archiverManager )
108     {
109         // it is needed in createUnpackableFile method
110         this.archiverManager = archiverManager;
111         super.setUnpackableFile( archiverManager );
112     }
113 
114     /**
115      * We need override original method which try to set wrong class of logger on Archiver.
116      * <p>
117      * Newer version of Archiver use SLF4J instead of Plexus logger.
118      */
119     @Override
120     public void createUnpackableFile( Artifact artifact, File destFile )
121         throws NoSuchArchiverException, ArchiverException, IOException
122     {
123         Archiver archiver = archiverManager.getArchiver( destFile );
124 
125         archiver.setDestFile( destFile );
126         archiver.addFile( getSrcFile(), getUnpackableFileName( artifact ) );
127 
128         if ( archiver instanceof WarArchiver )
129         {
130             WarArchiver war = (WarArchiver) archiver;
131             // the use of this is counter-intuitive:
132             // http://jira.codehaus.org/browse/PLX-286
133             war.setIgnoreWebxml( false );
134         }
135         archiver.createArchive();
136     }
137 }