1 package io.apicurio.registry.maven;
2
3 import org.apache.maven.plugin.AbstractMojo;
4 import org.apache.maven.plugin.MojoExecutionException;
5 import org.apache.maven.plugin.MojoFailureException;
6 import org.apache.maven.plugins.annotations.Mojo;
7 import org.apache.maven.plugins.annotations.Parameter;
8
9 import java.io.File;
10 import java.io.FileReader;
11 import java.io.FileWriter;
12 import java.io.Reader;
13 import java.util.List;
14 import java.util.Properties;
15
16 @Mojo(name = "merge")
17 public class MergePropertiesMojo extends AbstractMojo {
18
19
20
21
22 @Parameter(required = true)
23 File output;
24
25
26
27
28 @Parameter(required = true)
29 List<File> inputs;
30
31
32
33
34 @Parameter(required = false, defaultValue = "false")
35 Boolean deleteInputs;
36
37
38
39
40 @Override
41 public void execute() throws MojoExecutionException, MojoFailureException {
42 if (output == null || !output.getParentFile().isDirectory() || output.isDirectory()) {
43 throw new MojoExecutionException("Invalid 'output' file.");
44 }
45 if (inputs == null || inputs.isEmpty()) {
46 throw new MojoExecutionException("Invalid 'inputs'. Must be a collection of input files.");
47 }
48
49 Properties mergedProps = new Properties();
50
51 getLog().info("Reading " + inputs.size() + " input files.");
52 for (File input : inputs) {
53 if (!input.isFile()) {
54 throw new MojoExecutionException("Invalid input file: " + input.getAbsolutePath());
55 }
56 Properties inputProps = new Properties();
57 try (Reader reader = new FileReader(input)) {
58 inputProps.load(reader);
59 mergedProps.putAll(inputProps);
60 getLog().info("Read all properties from input file: " + input.getName());
61 } catch (Throwable t) {
62 throw new MojoExecutionException("Failed to load input file: " + input.getAbsolutePath(), t);
63 }
64 if (deleteInputs) {
65 input.delete();
66 getLog().info("Deleted input file: " + input.getName());
67 }
68 }
69
70
71 if (output.isFile()) {
72 output.delete();
73 }
74 try (FileWriter writer = new FileWriter(output)) {
75 mergedProps.store(writer, "Properties merged by 'apicurio-registry-maven-plugin'");
76 getLog().info("Merged properties written to: " + output.getName());
77 } catch (Throwable t) {
78 throw new MojoExecutionException(
79 "Failed to write merged properties to: " + output.getAbsolutePath());
80 }
81 }
82
83 }