View Javadoc
1   package io.apicurio.registry.maven;
2   
3   import io.apicurio.registry.rest.client.RegistryClient;
4   import io.vertx.core.Vertx;
5   import org.apache.maven.plugin.MojoExecutionException;
6   import org.apache.maven.plugins.annotations.Mojo;
7   import org.apache.maven.plugins.annotations.Parameter;
8   
9   import java.io.InputStream;
10  import java.nio.file.Files;
11  import java.nio.file.StandardCopyOption;
12  import java.util.List;
13  import java.util.concurrent.ExecutionException;
14  
15  /**
16   * Download artifacts.
17   */
18  @Mojo(name = "download")
19  public class DownloadRegistryMojo extends AbstractRegistryMojo {
20  
21      /**
22       * Set of artifact coordinates to download.
23       */
24      @Parameter(required = true)
25      List<DownloadArtifact> artifacts;
26  
27      /**
28       * Validate the configuration.
29       */
30      protected void validate() throws MojoExecutionException {
31          if (artifacts == null || artifacts.isEmpty()) {
32              getLog().warn("No artifacts are configured for download.");
33          } else {
34              int idx = 0;
35              int errorCount = 0;
36              for (DownloadArtifact artifact : artifacts) {
37                  if (artifact.getGroupId() == null) {
38                      getLog().error(String.format(
39                              "GroupId is required when downloading an artifact.  Missing from artifacts[%d].",
40                              idx));
41                      errorCount++;
42                  }
43                  if (artifact.getArtifactId() == null) {
44                      getLog().error(String.format(
45                              "ArtifactId is required when downloading an artifact.  Missing from artifacts[%s].",
46                              idx));
47                      errorCount++;
48                  }
49                  if (artifact.getFile() == null) {
50                      getLog().error(String.format(
51                              "File is required when downloading an artifact.  Missing from artifacts[%s].",
52                              idx));
53                      errorCount++;
54                  } else {
55                      if (artifact.getFile().exists()) {
56                          if (artifact.getOverwrite() == null || artifact.getOverwrite() == false) {
57                              getLog().error(String.format(
58                                      "File being written already exists.  Use <overwrite>true</overwrite> to replace the destination file: %s",
59                                      artifact.getFile().getPath()));
60                              errorCount++;
61                          }
62                      }
63                  }
64  
65                  idx++;
66              }
67  
68              if (errorCount > 0) {
69                  throw new MojoExecutionException(
70                          "Invalid configuration of the Download Artifact(s) mojo. See the output log for details.");
71              }
72          }
73      }
74  
75      @Override
76      protected void executeInternal() throws MojoExecutionException, ExecutionException, InterruptedException {
77          validate();
78  
79          Vertx vertx = createVertx();
80          RegistryClient registryClient = createClient(vertx);
81  
82          try {
83              int errorCount = 0;
84              if (artifacts != null) {
85                  for (DownloadArtifact artifact : artifacts) {
86                      errorCount += downloadArtifact(registryClient, artifact);
87                  }
88              }
89  
90              if (errorCount > 0) {
91                  throw new MojoExecutionException("Errors while downloading artifacts ...");
92              }
93          } finally {
94              vertx.close();
95          }
96      }
97  
98      private int downloadArtifact(RegistryClient registryClient, DownloadArtifact artifact)
99              throws ExecutionException, InterruptedException {
100         int errorCount = 0;
101         String groupId = artifact.getGroupId();
102         String artifactId = artifact.getArtifactId();
103         String version = artifact.getVersion();
104         if (version == null) {
105             version = "branch=latest";
106         }
107         boolean replaceExisting = artifact.getOverwrite() != null && artifact.getOverwrite();
108 
109         getLog().info(String.format("Downloading artifact [%s] / [%s] (version %s).", groupId, artifactId,
110                 version));
111 
112         try (InputStream content = registryClient.groups().byGroupId(groupId).artifacts()
113                 .byArtifactId(artifactId).versions().byVersionExpression(version).content().get()) {
114 
115             if (!artifact.getFile().getParentFile().exists()) {
116                 artifact.getFile().getParentFile().mkdirs();
117             }
118 
119             if (replaceExisting) {
120                 Files.copy(content, artifact.getFile().toPath(), StandardCopyOption.REPLACE_EXISTING);
121             } else {
122                 Files.copy(content, artifact.getFile().toPath());
123             }
124         } catch (Exception e) {
125             errorCount++;
126             getLog().error(
127                     String.format("Exception while downloading artifact [%s] / [%s]", groupId, artifactId),
128                     e);
129         }
130 
131         getLog().info(String.format("Downloaded artifact [%s] / [%s] to %s.", groupId, artifactId,
132                 artifact.getFile()));
133 
134         if (artifact.getArtifactReferences() != null && !artifact.getArtifactReferences().isEmpty()) {
135             for (DownloadArtifact reference : artifact.getArtifactReferences()) {
136                 errorCount += downloadArtifact(registryClient, reference);
137             }
138         }
139 
140         return errorCount;
141     }
142 
143     public void setArtifacts(List<DownloadArtifact> artifacts) {
144         this.artifacts = artifacts;
145     }
146 }