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