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          return new ArtifactItem(artifact);
57      }
58  
59      public List<ArtifactItem> getArtifactItems(Collection<Artifact> artifacts) {
60          List<ArtifactItem> list = new ArrayList<>();
61          for (Artifact artifact : artifacts) {
62              list.add(getArtifactItem(artifact));
63          }
64          return list;
65      }
66  
67      @Override
68      public Artifact createArtifact(
69              String groupId,
70              String artifactId,
71              VersionRange versionRange,
72              String scope,
73              String type,
74              String classifier,
75              boolean optional)
76              throws IOException {
77          File workingDir = getWorkingDir();
78  
79          if (!flattenedPath) {
80              StringBuilder path = new StringBuilder(128);
81  
82              path.append(groupId.replace('.', '/')).append('/');
83  
84              path.append(artifactId).append('/');
85  
86              path.append(ArtifactUtils.toSnapshotVersion(
87                      versionRange.getRecommendedVersion().toString()));
88  
89              // don't use flatten directories, won't happen at runtime
90              setWorkingDir(new File(workingDir, path.toString()));
91          }
92  
93          Artifact artifact = super.createArtifact(groupId, artifactId, versionRange, scope, type, classifier, optional);
94  
95          setWorkingDir(workingDir);
96  
97          return artifact;
98      }
99  
100     @Override
101     public void setUnpackableFile(ArchiverManager archiverManager) {
102         // it is needed in createUnpackableFile method
103         this.archiverManager = archiverManager;
104         super.setUnpackableFile(archiverManager);
105     }
106 
107     /**
108      * We need override original method which try to set wrong class of logger on Archiver.
109      * <p>
110      * Newer version of Archiver use SLF4J instead of Plexus logger.
111      */
112     @Override
113     public void createUnpackableFile(Artifact artifact, File destFile)
114             throws NoSuchArchiverException, ArchiverException, IOException {
115         Archiver archiver = archiverManager.getArchiver(destFile);
116 
117         archiver.setDestFile(destFile);
118         archiver.addFile(getSrcFile(), getUnpackableFileName(artifact));
119 
120         if (archiver instanceof WarArchiver) {
121             WarArchiver war = (WarArchiver) archiver;
122             war.setExpectWebXml(false);
123         }
124         archiver.createArchive();
125     }
126 }