From c5fc9f2962392a83d65d8cca0fe94e63478b0e69 Mon Sep 17 00:00:00 2001 From: MWarnecke Date: Thu, 27 Jun 2024 09:50:19 +0200 Subject: [PATCH] Use provided scope for maven-plugin dependencies * use provided scope for maven dependencies As the maven-plugin-plugin suggests, dependencies to the maven runtime should be in provided scope. This gets rid of the according warning which was written during build. Before Maven 3.9, plexus-utils was injected in the classpath at runtime. As of Maven 3.9 this is not the case anymore which broke the plugin due to a usage of said dependency. The only usage is replaced by a visitor to copy files. Closes #30542 Signed-off-by: Michael Warnecke * Guides need to see maven's Log class Signed-off-by: Michael Warnecke --------- Signed-off-by: Michael Warnecke --- docs/guides/pom.xml | 6 ++ docs/maven-plugin/pom.xml | 21 +++++++ .../guides/maven/DirectoryCopyVisitor.java | 39 +++++++++++++ .../org/keycloak/guides/maven/GuideMojo.java | 7 ++- .../maven/DirectoryCopyVisitorTest.java | 55 +++++++++++++++++++ 5 files changed, 125 insertions(+), 3 deletions(-) create mode 100644 docs/maven-plugin/src/main/java/org/keycloak/guides/maven/DirectoryCopyVisitor.java create mode 100644 docs/maven-plugin/src/test/java/org/keycloak/guides/maven/DirectoryCopyVisitorTest.java diff --git a/docs/guides/pom.xml b/docs/guides/pom.xml index ee41550a9b..a8b6d203ed 100644 --- a/docs/guides/pom.xml +++ b/docs/guides/pom.xml @@ -37,6 +37,12 @@ keycloak-guides-maven-plugin ${project.version} + + org.apache.maven + maven-plugin-api + ${maven.version} + test + diff --git a/docs/maven-plugin/pom.xml b/docs/maven-plugin/pom.xml index 67925550f0..e2953f6f89 100644 --- a/docs/maven-plugin/pom.xml +++ b/docs/maven-plugin/pom.xml @@ -36,6 +36,7 @@ org.apache.maven maven-plugin-api ${maven.version} + provided org.apache.maven.plugin-tools @@ -47,6 +48,7 @@ org.apache.maven maven-core ${maven.version} + provided org.keycloak @@ -56,6 +58,25 @@ org.freemarker freemarker + + org.junit.jupiter + junit-jupiter-api + test + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + org.jboss.logmanager.LogManager + + + + + + diff --git a/docs/maven-plugin/src/main/java/org/keycloak/guides/maven/DirectoryCopyVisitor.java b/docs/maven-plugin/src/main/java/org/keycloak/guides/maven/DirectoryCopyVisitor.java new file mode 100644 index 0000000000..4811c5915f --- /dev/null +++ b/docs/maven-plugin/src/main/java/org/keycloak/guides/maven/DirectoryCopyVisitor.java @@ -0,0 +1,39 @@ +package org.keycloak.guides.maven; + +import java.io.IOException; +import java.nio.file.FileVisitResult; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.StandardCopyOption; +import java.nio.file.attribute.BasicFileAttributes; + +public class DirectoryCopyVisitor extends SimpleFileVisitor { + + private Path targetDir; + private Path sourceDir; + + public DirectoryCopyVisitor(Path targetDir) { + this.targetDir = targetDir; + } + + @Override + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { + if (sourceDir == null) { + sourceDir = dir; + } else { + Path relativePath = sourceDir.relativize(dir); + Files.createDirectories(targetDir.resolve(relativePath)); + } + + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + Path relativePath = sourceDir.relativize(file); + Files.copy(file, targetDir.resolve(relativePath), StandardCopyOption.REPLACE_EXISTING); + return FileVisitResult.CONTINUE; + } + +} 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 203b52f745..14172fdb37 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 @@ -7,9 +7,9 @@ import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.project.MavenProject; -import org.codehaus.plexus.util.FileUtils; import java.io.File; +import java.nio.file.Files; @Mojo(name = "keycloak-guide", defaultPhase = LifecyclePhase.GENERATE_SOURCES, threadSafe = true) public class GuideMojo extends AbstractMojo { @@ -29,7 +29,7 @@ public class GuideMojo extends AbstractMojo { Log log = getLog(); File topDir = new File(sourceDir); - for (File srcDir: topDir.listFiles(d -> d.isDirectory() && !d.getName().equals("templates"))) { + for (File srcDir : topDir.listFiles(d -> d.isDirectory() && !d.getName().equals("templates"))) { if (srcDir.getName().equals("target") || srcDir.getName().equals("src")) { // those are standard maven folders, ignore them continue; @@ -41,7 +41,8 @@ public class GuideMojo extends AbstractMojo { } if (srcDir.getName().equals("images")) { - FileUtils.copyDirectoryStructure(srcDir, targetDir); + log.info("Copy files from " + srcDir + " to " + targetDir); + Files.walkFileTree(srcDir.toPath(), new DirectoryCopyVisitor(targetDir.toPath())); } else { log.info("Guide dir: " + srcDir.getAbsolutePath()); log.info("Target dir: " + targetDir.getAbsolutePath()); diff --git a/docs/maven-plugin/src/test/java/org/keycloak/guides/maven/DirectoryCopyVisitorTest.java b/docs/maven-plugin/src/test/java/org/keycloak/guides/maven/DirectoryCopyVisitorTest.java new file mode 100644 index 0000000000..e3025d40c5 --- /dev/null +++ b/docs/maven-plugin/src/test/java/org/keycloak/guides/maven/DirectoryCopyVisitorTest.java @@ -0,0 +1,55 @@ +package org.keycloak.guides.maven; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +class DirectoryCopyVisitorTest { + + @TempDir + Path temp; + + private Path srcDir; + private Path targetDir; + + @BeforeEach + void setUpDirectories() throws IOException { + srcDir = temp.resolve("source"); + targetDir = temp.resolve("target"); + Files.createDirectories(srcDir); + Files.createDirectories(targetDir); + } + + @Test + void copyDirectoriesMultipleLevels() throws IOException { + Path level1 = srcDir.resolve("level1"); + Path level2a = level1.resolve("level2a"); + Path level2b = level1.resolve("level2b"); + Path level3 = level2a.resolve("level3"); + Files.createDirectories(level3); + Files.createDirectories(level2b); + Files.createFile(srcDir.resolve("rootfile")); + Files.createFile(level1.resolve("l1file")); + Files.createFile(level2b.resolve("l2filea")); + Files.createFile(level2b.resolve("l2fileb")); + + Files.walkFileTree(srcDir, new DirectoryCopyVisitor(targetDir)); + + assertEquals(List.of("level1", "rootfile"), listDirContent(targetDir)); + assertEquals(List.of("l1file", "level2a", "level2b"), listDirContent(targetDir.resolve("level1"))); + assertEquals(List.of("level3"), listDirContent(targetDir.resolve("level1").resolve("level2a"))); + assertEquals(List.of(), listDirContent(targetDir.resolve("level1").resolve("level2a").resolve("level3"))); + assertEquals(List.of("l2filea", "l2fileb"), listDirContent(targetDir.resolve("level1").resolve("level2b"))); + } + + private List listDirContent(Path path) throws IOException { + return Files.list(path).map(Path::getFileName).map(Path::toString).sorted().toList(); + } +}