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 <WarneckeMichael@web.de>

* Guides need to see maven's Log class

Signed-off-by: Michael Warnecke <WarneckeMichael@web.de>

---------

Signed-off-by: Michael Warnecke <WarneckeMichael@web.de>
This commit is contained in:
MWarnecke 2024-06-27 09:50:19 +02:00 committed by GitHub
parent fa47d1a416
commit c5fc9f2962
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 125 additions and 3 deletions

View file

@ -37,6 +37,12 @@
<artifactId>keycloak-guides-maven-plugin</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>${maven.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View file

@ -36,6 +36,7 @@
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>${maven.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
@ -47,6 +48,7 @@
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>${maven.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
@ -56,6 +58,25 @@
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</project>

View file

@ -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<Path> {
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;
}
}

View file

@ -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 {
@ -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());

View file

@ -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<String> listDirContent(Path path) throws IOException {
return Files.list(path).map(Path::getFileName).map(Path::toString).sorted().toList();
}
}