1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.forgerock.doc.maven.pre;
18
19 import freemarker.template.Configuration;
20 import freemarker.template.Template;
21 import freemarker.template.TemplateException;
22 import freemarker.template.TemplateExceptionHandler;
23 import org.apache.commons.io.FileUtils;
24 import org.apache.maven.plugin.MojoExecutionException;
25 import org.forgerock.doc.maven.AbstractDocbkxMojo;
26 import org.twdata.maven.mojoexecutor.MojoExecutor;
27
28 import java.io.File;
29 import java.io.FileOutputStream;
30 import java.io.IOException;
31 import java.io.OutputStreamWriter;
32 import java.io.Writer;
33 import java.util.HashMap;
34 import java.util.Map;
35
36
37
38
39 public class ArtifactBuilder {
40
41
42
43
44 private AbstractDocbkxMojo m;
45
46
47
48
49 private final Executor executor;
50
51
52
53
54
55 private Configuration configuration;
56
57
58
59
60
61
62 public ArtifactBuilder(final AbstractDocbkxMojo mojo) {
63 m = mojo;
64 this.executor = new Executor();
65
66
67 configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
68 configuration.setClassForTemplateLoading(ArtifactBuilder.class, "/templates");
69 configuration.setDefaultEncoding("UTF-8");
70 configuration.setTemplateExceptionHandler(TemplateExceptionHandler.DEBUG_HANDLER);
71 }
72
73
74
75
76
77
78 private String getDescriptorPath(File descriptorFile) throws IOException, TemplateException {
79 Template template = configuration.getTemplate("artifact.ftl");
80
81 FileOutputStream out = FileUtils.openOutputStream(descriptorFile);
82
83 Writer writer = new OutputStreamWriter(out);
84 template.process(getModel(), writer);
85 writer.close();
86 out.close();
87
88 return descriptorFile.getPath();
89 }
90
91
92
93
94
95
96 private Map<String, String> getModel() {
97 Map<String, String> map = new HashMap<String, String>();
98 map.put("modifiedSourcesDirectory", m.path(m.getDocbkxModifiableSourcesDirectory()));
99 return map;
100 }
101
102
103
104
105
106
107 public void execute() throws MojoExecutionException {
108 executor.build();
109 }
110
111
112
113
114 class Executor extends MojoExecutor {
115
116
117
118
119
120
121 public void build() throws MojoExecutionException {
122
123 File descriptorFile;
124 String descriptorPath;
125 try {
126 descriptorFile = File.createTempFile("descriptor", "xml");
127 descriptorPath = getDescriptorPath(descriptorFile);
128 } catch (Exception e) {
129 throw new MojoExecutionException("Failed to get assembly descriptor", e);
130 }
131
132 executeMojo(
133 plugin(
134 groupId("org.apache.maven.plugins"),
135 artifactId("maven-assembly-plugin"),
136 version(m.getMavenAssemblyVersion())),
137 goal("single"),
138 configuration(
139 element("descriptors",
140 element("descriptor", descriptorPath))),
141 executionEnvironment(m.getProject(), m.getSession(), m.getPluginManager()));
142
143 if (descriptorFile.exists()) {
144 descriptorFile.delete();
145 }
146 }
147 }
148 }