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.report.projectinfo.stubs;
20  
21  import java.io.File;
22  import java.io.IOException;
23  
24  import org.apache.maven.artifact.Artifact;
25  import org.apache.maven.artifact.ArtifactUtils;
26  import org.apache.maven.artifact.versioning.VersionRange;
27  import org.apache.maven.plugin.testing.ArtifactStubFactory;
28  
29  public class DependencyArtifactStubFactory extends ArtifactStubFactory {
30      private boolean flattenedPath = true;
31  
32      public DependencyArtifactStubFactory(File theWorkingDir, boolean theCreateFiles, boolean flattenedPath) {
33          this(theWorkingDir, theCreateFiles);
34          this.flattenedPath = flattenedPath;
35      }
36  
37      public DependencyArtifactStubFactory(File theWorkingDir, boolean theCreateFiles) {
38          super(theWorkingDir, theCreateFiles);
39      }
40  
41      @Override
42      public Artifact createArtifact(
43              String groupId,
44              String artifactId,
45              VersionRange versionRange,
46              String scope,
47              String type,
48              String classifier,
49              boolean optional)
50              throws IOException {
51          File workingDir = getWorkingDir();
52  
53          if (!flattenedPath) {
54              // don't use flatten directories, won't happen at runtime
55              String path = groupId.replace('.', '/')
56                      + '/'
57                      + artifactId
58                      + '/'
59                      + ArtifactUtils.toSnapshotVersion(
60                              versionRange.getRecommendedVersion().toString());
61              setWorkingDir(new File(workingDir, path));
62          }
63  
64          Artifact artifact = super.createArtifact(groupId, artifactId, versionRange, scope, type, classifier, optional);
65  
66          setWorkingDir(workingDir);
67  
68          return artifact;
69      }
70  }