From 2bb31b1bfc9aa48a3ce06bf3588c6995d90f8adc Mon Sep 17 00:00:00 2001 From: Alexander Schwartz Date: Fri, 17 Nov 2023 12:06:16 +0100 Subject: [PATCH] Fix DocsBuildDebugUtil signatures, and ensure it can be called from an IDE Closes #24817 Signed-off-by: Alexander Schwartz Signed-off-by: Alexander Schwartz --- docs/guides/pom.xml | 6 ++- .../keycloak/guides/DocsBuildDebugUtil.java | 39 +++++++++++++++++-- .../keycloak/guides/maven/GuideBuilder.java | 6 +-- .../org/keycloak/guides/maven/GuideMojo.java | 2 +- 4 files changed, 45 insertions(+), 8 deletions(-) diff --git a/docs/guides/pom.xml b/docs/guides/pom.xml index c3fcf4f2ad..3e4496cbdb 100644 --- a/docs/guides/pom.xml +++ b/docs/guides/pom.xml @@ -28,7 +28,8 @@ Keycloak Guides keycloak-guides Keycloak Guides - pom + + jar @@ -40,6 +41,9 @@ + + maven-compiler-plugin + maven-resources-plugin 3.3.0 diff --git a/docs/guides/src/test/java/org/keycloak/guides/DocsBuildDebugUtil.java b/docs/guides/src/test/java/org/keycloak/guides/DocsBuildDebugUtil.java index 1e8eb207dd..777ff87a2d 100644 --- a/docs/guides/src/test/java/org/keycloak/guides/DocsBuildDebugUtil.java +++ b/docs/guides/src/test/java/org/keycloak/guides/DocsBuildDebugUtil.java @@ -2,14 +2,25 @@ package org.keycloak.guides; import freemarker.template.TemplateException; 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.IOException; +import java.util.Properties; 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")); + Properties properties = readPropertiesFromPomXml(); 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")) { @@ -18,10 +29,32 @@ public class DocsBuildDebugUtil { } File targetDir = usrDir.toPath().resolve("target/generated-guides/" + srcDir.getName()).toFile(); 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(); - 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; + } + } diff --git a/docs/maven-plugin/src/main/java/org/keycloak/guides/maven/GuideBuilder.java b/docs/maven-plugin/src/main/java/org/keycloak/guides/maven/GuideBuilder.java index e124a21812..3a624d2fba 100644 --- a/docs/maven-plugin/src/main/java/org/keycloak/guides/maven/GuideBuilder.java +++ b/docs/maven-plugin/src/main/java/org/keycloak/guides/maven/GuideBuilder.java @@ -2,13 +2,13 @@ package org.keycloak.guides.maven; import freemarker.template.TemplateException; import org.apache.maven.plugin.logging.Log; -import org.apache.maven.project.MavenProject; import org.keycloak.common.Version; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; +import java.util.Properties; public class GuideBuilder { @@ -17,7 +17,7 @@ public class GuideBuilder { private final File targetDir; 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.targetDir = targetDir; this.log = log; @@ -25,7 +25,7 @@ public class GuideBuilder { Map globalAttributes = new HashMap<>(); globalAttributes.put("ctx", new Context(srcDir)); globalAttributes.put("version", Version.VERSION); - globalAttributes.put("properties", project.getProperties()); + globalAttributes.put("properties", properties); this.freeMarker = new FreeMarker(srcDir.getParentFile(), globalAttributes); } diff --git a/docs/maven-plugin/src/main/java/org/keycloak/guides/maven/GuideMojo.java b/docs/maven-plugin/src/main/java/org/keycloak/guides/maven/GuideMojo.java index bf5e5c8870..5f1e54c4e0 100644 --- a/docs/maven-plugin/src/main/java/org/keycloak/guides/maven/GuideMojo.java +++ b/docs/maven-plugin/src/main/java/org/keycloak/guides/maven/GuideMojo.java @@ -46,7 +46,7 @@ public class GuideMojo extends AbstractMojo { log.info("Guide dir: " + srcDir.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(); } }