View Javadoc
1   package org.spf4j.maven.plugin.avro.avscp;
2   
3   import java.io.IOException;
4   import java.nio.file.Files;
5   import java.nio.file.Path;
6   import org.apache.maven.artifact.DefaultArtifact;
7   import org.apache.maven.artifact.handler.DefaultArtifactHandler;
8   import org.apache.maven.plugin.MojoExecutionException;
9   import org.apache.maven.plugin.MojoFailureException;
10  import org.apache.maven.plugin.logging.Log;
11  
12  import org.apache.maven.plugins.annotations.LifecyclePhase;
13  import org.apache.maven.plugins.annotations.Mojo;
14  import org.apache.maven.plugins.annotations.Parameter;
15  import org.apache.maven.shared.model.fileset.FileSet;
16  import org.apache.maven.shared.model.fileset.util.FileSetManager;
17  import org.spf4j.io.compress.Compress;
18  
19  /**
20   * mojo that packages a schema package and avro sources and attaches them as separate artifacts.
21   */
22  @Mojo(name = "avro-package", defaultPhase = LifecyclePhase.PACKAGE, requiresProject = true)
23  public final class SchemaPackageMojo extends SchemaMojoBase {
24  
25    /**
26     * package avsc artifact (a zip/jar containing avro schemas.)
27     */
28    @Parameter(name = "packageAvsc",
29            defaultValue = "true")
30    private boolean packageAvsc = true;
31  
32    /**
33     * package avsc sources artifact (a zip/jar containing avro sources (avdl, avprr, avsc).)
34     */
35    @Parameter(name = "packageAvscSources",
36            defaultValue = "true")
37    private boolean packageAvscSources = true;
38  
39  
40    public String[] getSourceFiles() {
41      FileSetManager fsm = new FileSetManager();
42      FileSet fs = new FileSet();
43      fs.setDirectory(sourceDirectory.getAbsolutePath());
44      fs.addInclude("**/*.avsc");
45      fs.addInclude("**/*.avpr");
46      fs.addInclude("**/*.avdl");
47      fs.setFollowSymlinks(false);
48      return fsm.getIncludedFiles(fs);
49    }
50  
51    /**
52     * {@inheritDoc}
53     * package schemas and sources.s
54     */
55    public void execute() throws MojoExecutionException, MojoFailureException {
56      super.execute();
57      Log logger = this.getLog();
58      Path sourcePath = this.sourceDirectory.toPath();
59      if (packageAvsc) {
60        logger.info("Packaging schemas");
61        Path avsc = target.toPath().resolve(
62                  mavenProject.getArtifactId() + '-' + mavenProject.getVersion()
63                          + '-' + "avsc." + schemaArtifactExtension);
64        try {
65          if (Files.notExists(sourcePath)) {
66            return;
67          }
68          Compress.zip(this.generatedAvscTarget.toPath(), avsc);
69        } catch (IOException ex) {
70          throw new MojoExecutionException("Cannot package schemas and sources from " + this.generatedAvscTarget, ex);
71        }
72        DefaultArtifact schemas = new DefaultArtifact(mavenProject.getGroupId(),
73                mavenProject.getArtifactId(), mavenProject.getVersion(), "compile",
74                schemaArtifactExtension, schemaArtifactClassifier,
75                new DefaultArtifactHandler(schemaArtifactExtension));
76        schemas.setFile(avsc.toFile());
77        logger.debug("Attaching " + schemas  + " from " + avsc);
78        mavenProject.addAttachedArtifact(schemas);
79      }
80      if (packageAvscSources) {
81        logger.info("Packaging schema sources");
82        Path sources = target.toPath().resolve(
83                  mavenProject.getArtifactId() + '-' + mavenProject.getVersion() + '-' + "avroSources.jar");
84        try {
85          Compress.zip(sourcePath, sources);
86        } catch (IOException ex) {
87          throw new MojoExecutionException("Cannot package sources from " + this.sourceDirectory, ex);
88        }
89        DefaultArtifact avroSources = new DefaultArtifact(mavenProject.getGroupId(),
90                mavenProject.getArtifactId(), mavenProject.getVersion(), "compile", "jar", "avroSources",
91                new DefaultArtifactHandler("jar"));
92        avroSources.setFile(sources.toFile());
93        logger.debug("Attaching " + avroSources  + " from " + sources);
94        mavenProject.addAttachedArtifact(avroSources);
95      }
96    }
97  
98    @Override
99    public String toString() {
100     return "SchemaPackageMojo{" + "schemaArtifactClassifier=" + schemaArtifactClassifier + ", "
101           + super.toString() + '}';
102   }
103 
104 }