View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.dependency.testUtils;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.List;
26  
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.artifact.ArtifactUtils;
29  import org.apache.maven.artifact.versioning.VersionRange;
30  import org.apache.maven.plugin.testing.ArtifactStubFactory;
31  import org.apache.maven.plugins.dependency.fromConfiguration.ArtifactItem;
32  import org.codehaus.plexus.archiver.Archiver;
33  import org.codehaus.plexus.archiver.ArchiverException;
34  import org.codehaus.plexus.archiver.manager.ArchiverManager;
35  import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
36  import org.codehaus.plexus.archiver.war.WarArchiver;
37  
38  public class DependencyArtifactStubFactory extends ArtifactStubFactory {
39      private boolean flattenedPath = true;
40      private ArchiverManager archiverManager;
41  
42      public DependencyArtifactStubFactory(File theWorkingDir, boolean theCreateFiles, boolean flattenedPath) {
43          this(theWorkingDir, theCreateFiles);
44          this.flattenedPath = flattenedPath;
45      }
46  
47      /**
48       * @param theWorkingDir {@link File}
49       * @param theCreateFiles true/false.
50       */
51      public DependencyArtifactStubFactory(File theWorkingDir, boolean theCreateFiles) {
52          super(theWorkingDir, theCreateFiles);
53      }
54  
55      public ArtifactItem getArtifactItem(Artifact artifact) {
56          ArtifactItem item = new ArtifactItem(artifact);
57          return item;
58      }
59  
60      public List<ArtifactItem> getArtifactItems(Collection<Artifact> artifacts) {
61          List<ArtifactItem> list = new ArrayList<>();
62          for (Artifact artifact : artifacts) {
63              list.add(getArtifactItem(artifact));
64          }
65          return list;
66      }
67  
68      @Override
69      public Artifact createArtifact(
70              String groupId,
71              String artifactId,
72              VersionRange versionRange,
73              String scope,
74              String type,
75              String classifier,
76              boolean optional)
77              throws IOException {
78          File workingDir = getWorkingDir();
79  
80          if (!flattenedPath) {
81              StringBuilder path = new StringBuilder(128);
82  
83              path.append(groupId.replace('.', '/')).append('/');
84  
85              path.append(artifactId).append('/');
86  
87              path.append(ArtifactUtils.toSnapshotVersion(
88                      versionRange.getRecommendedVersion().toString()));
89  
90              // don't use flatten directories, won't happen at runtime
91              setWorkingDir(new File(workingDir, path.toString()));
92          }
93  
94          Artifact artifact = super.createArtifact(groupId, artifactId, versionRange, scope, type, classifier, optional);
95  
96          setWorkingDir(workingDir);
97  
98          return artifact;
99      }
100 
101     @Override
102     public void setUnpackableFile(ArchiverManager archiverManager) {
103         // it is needed in createUnpackableFile method
104         this.archiverManager = archiverManager;
105         super.setUnpackableFile(archiverManager);
106     }
107 
108     /**
109      * We need override original method which try to set wrong class of logger on Archiver.
110      * <p>
111      * Newer version of Archiver use SLF4J instead of Plexus logger.
112      */
113     @Override
114     public void createUnpackableFile(Artifact artifact, File destFile)
115             throws NoSuchArchiverException, ArchiverException, IOException {
116         Archiver archiver = archiverManager.getArchiver(destFile);
117 
118         archiver.setDestFile(destFile);
119         archiver.addFile(getSrcFile(), getUnpackableFileName(artifact));
120 
121         if (archiver instanceof WarArchiver) {
122             WarArchiver war = (WarArchiver) archiver;
123             // the use of this is counter-intuitive:
124             // http://jira.codehaus.org/browse/PLX-286
125             war.setIgnoreWebxml(false);
126         }
127         archiver.createArchive();
128     }
129 }