3 * Licensed to the Apache Software Foundation (ASF) under one or more
4 * contributor license agreements. See the NOTICE file distributed with
5 * this work for additional information regarding copyright ownership.
6 * The ASF licenses this file to You under the Apache License, Version 2.0
7 * (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org.apache.karaf.tooling.commands;
21 import java.io.FileOutputStream;
22 import java.io.PrintStream;
23 import java.net.MalformedURLException;
25 import java.net.URLClassLoader;
26 import java.util.ArrayList;
27 import java.util.List;
30 import java.util.TreeMap;
31 import java.util.TreeSet;
33 import org.apache.karaf.shell.api.action.Action;
34 import org.apache.karaf.shell.api.action.Command;
35 import org.apache.maven.artifact.DependencyResolutionRequiredException;
36 import org.apache.maven.plugin.AbstractMojo;
37 import org.apache.maven.plugin.MojoExecutionException;
38 import org.apache.maven.plugin.MojoFailureException;
39 import org.apache.maven.plugins.annotations.LifecyclePhase;
40 import org.apache.maven.plugins.annotations.Mojo;
41 import org.apache.maven.plugins.annotations.Parameter;
42 import org.apache.maven.plugins.annotations.ResolutionScope;
43 import org.apache.maven.project.MavenProject;
44 import org.apache.xbean.finder.ClassFinder;
47 * Generates help documentation for Karaf commands
49 @Mojo(name = "commands-generate-help", defaultPhase = LifecyclePhase.GENERATE_RESOURCES,
50 requiresDependencyResolution = ResolutionScope.RUNTIME, inheritByDefault = false, threadSafe = true)
51 public class GenerateHelpMojo extends AbstractMojo {
56 @Parameter(defaultValue = "${project.build.directory}/docbkx/sources")
57 protected File targetFolder;
62 @Parameter(defaultValue = "docbx")
63 protected String format;
66 * The classloader to use for loading the commands.
67 * Can be "project" or "plugin"
69 @Parameter(defaultValue = "project")
70 protected String classLoader;
73 * Includes the --help command output in the generated documentation
75 @Parameter(defaultValue = "true")
76 protected boolean includeHelpOption;
81 @Parameter(defaultValue = "${project}")
82 protected MavenProject project;
84 private static final String FORMAT_CONF = "conf";
85 private static final String FORMAT_DOCBX = "docbx";
86 private static final String FORMAT_ASCIIDOC = "asciidoc";
88 public void execute() throws MojoExecutionException, MojoFailureException {
90 if (!FORMAT_DOCBX.equals(format) && !FORMAT_CONF.equals(format) && !FORMAT_ASCIIDOC.equals(format)) {
91 throw new MojoFailureException("Unsupported format: " + format + ". Supported formats are: asciidoc, docbx, or conf.");
93 if (!targetFolder.exists()) {
94 targetFolder.mkdirs();
97 ClassFinder finder = createFinder(classLoader);
98 List<Class<?>> classes = finder.findAnnotatedClasses(Command.class);
99 if (classes.isEmpty()) {
100 throw new MojoFailureException("No command found");
103 CommandHelpPrinter helpPrinter = null;
104 if (FORMAT_ASCIIDOC.equals(format)) {
105 helpPrinter = new AsciiDoctorCommandHelpPrinter();
107 if (FORMAT_CONF.equals(format)) {
108 helpPrinter = new UserConfCommandHelpPrinter();
110 if (FORMAT_DOCBX.equals(format)) {
111 helpPrinter = new DocBookCommandHelpPrinter();
114 Map<String, Set<String>> commands = new TreeMap<String, Set<String>>();
116 String commandSuffix = null;
117 if (FORMAT_ASCIIDOC.equals(format)) {
118 commandSuffix = "adoc";
120 if (FORMAT_CONF.equals(format)) {
121 commandSuffix = "conf";
123 if (FORMAT_DOCBX.equals(format)) {
124 commandSuffix = "xml";
126 for (Class<?> clazz : classes) {
128 Action action = (Action) clazz.newInstance();
129 Command cmd = clazz.getAnnotation(Command.class);
131 // skip the *-help command
132 if (cmd.scope().equals("*")) continue;
134 File output = new File(targetFolder, cmd.scope() + "-" + cmd.name() + "." + commandSuffix);
135 FileOutputStream outStream = new FileOutputStream(output);
136 PrintStream out = new PrintStream(outStream);
137 helpPrinter.printHelp(action, out, includeHelpOption);
141 Set<String> cmds = commands.get(cmd.scope());
143 cmds = new TreeSet<String>();
144 commands.put(cmd.scope(), cmds);
146 cmds.add(cmd.name());
147 getLog().info("Found command: " + cmd.scope() + ":" + cmd.name());
148 } catch (Exception e) {
149 getLog().warn("Unable to write help for " + clazz.getName(), e);
153 String overViewSuffix = null;
154 if (FORMAT_ASCIIDOC.equals(format)) {
155 overViewSuffix = "adoc";
157 if (FORMAT_CONF.equals(format)) {
158 overViewSuffix = "conf";
160 if (FORMAT_DOCBX.equals(format)) {
161 overViewSuffix = "xml";
163 PrintStream writer = new PrintStream(new FileOutputStream(new File(targetFolder, "commands." + overViewSuffix)));
164 helpPrinter.printOverview(commands, writer);
166 } catch (Exception e) {
167 throw new MojoExecutionException("Error building commands help", e);
171 private ClassFinder createFinder(String classloaderType) throws DependencyResolutionRequiredException, MalformedURLException,
172 Exception, MojoFailureException {
174 if ("project".equals(classloaderType)) {
175 List<URL> urls = new ArrayList<URL>();
176 for (Object object : project.getCompileClasspathElements()) {
177 String path = (String) object;
178 urls.add(new File(path).toURI().toURL());
180 ClassLoader loader = new URLClassLoader(urls.toArray(new URL[urls.size()]), getClass().getClassLoader());
181 finder = new ClassFinder(loader, urls);
182 } else if ("plugin".equals(classLoader)) {
183 finder = new ClassFinder(getClass().getClassLoader());
185 throw new MojoFailureException("classLoader attribute must be 'project' or 'plugin'");