Fix DocsBuildDebugUtil signatures, and ensure it can be called from an IDE

Closes #24817

Signed-off-by: Alexander Schwartz <aschwart@redhat.com>

Signed-off-by: Alexander Schwartz <aschwart@redhat.com>
This commit is contained in:
Alexander Schwartz 2023-11-17 12:06:16 +01:00 committed by Pedro Igor
parent 1a15cb2803
commit 2bb31b1bfc
4 changed files with 45 additions and 8 deletions

View file

@ -28,7 +28,8 @@
<name>Keycloak Guides</name> <name>Keycloak Guides</name>
<artifactId>keycloak-guides</artifactId> <artifactId>keycloak-guides</artifactId>
<description>Keycloak Guides</description> <description>Keycloak Guides</description>
<packaging>pom</packaging> <!-- although this doesn't provide a JAR, this is necessary to call the DocsBuildDebugUtil class from an IDE for debugging -->
<packaging>jar</packaging>
<dependencies> <dependencies>
<dependency> <dependency>
@ -40,6 +41,9 @@
<build> <build>
<plugins> <plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin> <plugin>
<artifactId>maven-resources-plugin</artifactId> <artifactId>maven-resources-plugin</artifactId>
<version>3.3.0</version> <version>3.3.0</version>

View file

@ -2,14 +2,25 @@ package org.keycloak.guides;
import freemarker.template.TemplateException; import freemarker.template.TemplateException;
import org.keycloak.guides.maven.GuideBuilder; import org.keycloak.guides.maven.GuideBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Properties;
public class DocsBuildDebugUtil { public class DocsBuildDebugUtil {
public static void main(String[] args) throws IOException, TemplateException { public static void main(String[] args) throws IOException, TemplateException, ParserConfigurationException, SAXException {
File usrDir = new File(System.getProperty("user.dir")); File usrDir = new File(System.getProperty("user.dir"));
Properties properties = readPropertiesFromPomXml();
for (File srcDir: usrDir.toPath().resolve("docs/guides").toFile().listFiles(d -> d.isDirectory() && !d.getName().equals("templates"))) { for (File srcDir: usrDir.toPath().resolve("docs/guides").toFile().listFiles(d -> d.isDirectory() && !d.getName().equals("templates"))) {
if (srcDir.getName().equals("target") || srcDir.getName().equals("src")) { if (srcDir.getName().equals("target") || srcDir.getName().equals("src")) {
@ -18,10 +29,32 @@ public class DocsBuildDebugUtil {
} }
File targetDir = usrDir.toPath().resolve("target/generated-guides/" + srcDir.getName()).toFile(); File targetDir = usrDir.toPath().resolve("target/generated-guides/" + srcDir.getName()).toFile();
targetDir.mkdirs(); targetDir.mkdirs();
GuideBuilder builder = new GuideBuilder(srcDir, targetDir, null);
// put here all the entries needed from the parent pom.xml
GuideBuilder builder = new GuideBuilder(srcDir, targetDir, null, properties);
builder.build(); builder.build();
System.out.println("Guides generated to: " + targetDir.getAbsolutePath().toString()); System.out.println("Guides generated to: " + targetDir.getAbsolutePath());
} }
} }
private static Properties readPropertiesFromPomXml() throws ParserConfigurationException, SAXException, IOException {
Properties properties = new Properties();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
// parse pom.xml file - avoid adding Maven as a dependency here
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new File("pom.xml"));
NodeList propertiesXml = doc.getDocumentElement().getElementsByTagName("properties").item(0).getChildNodes();
for(int i = 0; i < propertiesXml.getLength(); ++i) {
Node item = propertiesXml.item(i);
if (!(item instanceof Element)) {
continue;
}
properties.put(item.getNodeName(), item.getTextContent());
}
return properties;
}
} }

View file

@ -2,13 +2,13 @@ package org.keycloak.guides.maven;
import freemarker.template.TemplateException; import freemarker.template.TemplateException;
import org.apache.maven.plugin.logging.Log; import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;
import org.keycloak.common.Version; import org.keycloak.common.Version;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Properties;
public class GuideBuilder { public class GuideBuilder {
@ -17,7 +17,7 @@ public class GuideBuilder {
private final File targetDir; private final File targetDir;
private final Log log; private final Log log;
public GuideBuilder(File srcDir, File targetDir, Log log, MavenProject project) throws IOException { public GuideBuilder(File srcDir, File targetDir, Log log, Properties properties) throws IOException {
this.srcDir = srcDir; this.srcDir = srcDir;
this.targetDir = targetDir; this.targetDir = targetDir;
this.log = log; this.log = log;
@ -25,7 +25,7 @@ public class GuideBuilder {
Map<String, Object> globalAttributes = new HashMap<>(); Map<String, Object> globalAttributes = new HashMap<>();
globalAttributes.put("ctx", new Context(srcDir)); globalAttributes.put("ctx", new Context(srcDir));
globalAttributes.put("version", Version.VERSION); globalAttributes.put("version", Version.VERSION);
globalAttributes.put("properties", project.getProperties()); globalAttributes.put("properties", properties);
this.freeMarker = new FreeMarker(srcDir.getParentFile(), globalAttributes); this.freeMarker = new FreeMarker(srcDir.getParentFile(), globalAttributes);
} }

View file

@ -46,7 +46,7 @@ public class GuideMojo extends AbstractMojo {
log.info("Guide dir: " + srcDir.getAbsolutePath()); log.info("Guide dir: " + srcDir.getAbsolutePath());
log.info("Target dir: " + targetDir.getAbsolutePath()); log.info("Target dir: " + targetDir.getAbsolutePath());
GuideBuilder g = new GuideBuilder(srcDir, targetDir, log, project); GuideBuilder g = new GuideBuilder(srcDir, targetDir, log, project.getProperties());
g.build(); g.build();
} }
} }