1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.dependency.utils;
20
21 import javax.inject.Inject;
22 import javax.inject.Named;
23 import javax.inject.Singleton;
24
25 import java.io.File;
26 import java.io.IOException;
27
28 import org.apache.maven.artifact.Artifact;
29 import org.apache.maven.plugin.MojoExecutionException;
30 import org.codehaus.plexus.util.FileUtils;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.sonatype.plexus.build.incremental.BuildContext;
34
35
36
37
38
39
40 @Named
41 @Singleton
42 public class CopyUtil {
43
44 private final BuildContext buildContext;
45
46 private final Logger logger = LoggerFactory.getLogger(CopyUtil.class);
47
48 @Inject
49 public CopyUtil(BuildContext buildContext) {
50 this.buildContext = buildContext;
51 }
52
53
54
55
56
57
58
59
60
61
62 public void copyArtifactFile(Artifact sourceArtifact, File destination) throws IOException, MojoExecutionException {
63 File source = sourceArtifact.getFile();
64 if (source.isDirectory()) {
65
66 throw new MojoExecutionException("Artifact '" + sourceArtifact
67 + "' has not been packaged yet (is a directory). When used on reactor artifact, "
68 + "copy should be executed after packaging: see MDEP-187.");
69 }
70 logger.debug("Copying artifact '{}' ({}) to {}", sourceArtifact.getId(), sourceArtifact.getFile(), destination);
71 FileUtils.copyFile(source, destination);
72 buildContext.refresh(destination);
73 }
74
75
76
77
78
79
80
81
82
83 public void copyFile(File source, File destination) throws IOException {
84 logger.debug("Copying file '{}' to {}", source, destination);
85 FileUtils.copyFile(source, destination);
86 buildContext.refresh(destination);
87 }
88 }