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.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   * Provide a copyFile method in one place.
37   *
38   * @since 3.7.0
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       * Copies the artifact (file)
55       *
56       * @param sourceArtifact the artifact (file) to copy
57       * @param destination file name of destination file
58       * @throws IOException if copy has failed
59       * @throws MojoExecutionException if artifact file is a directory (which has not been packaged yet)
60       * @since 3.7.0
61       */
62      public void copyArtifactFile(Artifact sourceArtifact, File destination) throws IOException, MojoExecutionException {
63          File source = sourceArtifact.getFile();
64          if (source.isDirectory()) {
65              // usual case is a future jar packaging, but there are special cases: classifier and other packaging
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       * Copies a file to a destination and refreshes the build context for the new file.
77       *
78       * @param source the source file to copy
79       * @param destination the destination file
80       * @throws IOException if copy has failed
81       * @since 3.2.0
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  }