Skip to content

Commit

Permalink
Fix Maven plugin CWD to be project.basedir
Browse files Browse the repository at this point in the history
  • Loading branch information
agentgt committed Dec 29, 2024
1 parent 348e39f commit 1935565
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 13 deletions.
8 changes: 8 additions & 0 deletions doc/overview.html
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,14 @@ <h3 id="maven-plugin">Maven Plugin</h3>
</urls>
<!-- Will replace already set values by default is set to true -->
<override>true</override>
<!--
By default EZKV will use the project.basedir as the CWD.
Maven usually sets project.basedir for CWD for unit tests (surefire) and other plugins.
Set this to true to use the actual system CWD (usually the terminal current working
and not always the project basedir). However realize your build may not be repeatable
if it is set to true.
-->
<realCWD>false</realCWD>
</configuration>
</execution>
</executions>
Expand Down
4 changes: 4 additions & 0 deletions etc/eea/java/nio/file/FileSystem.eea
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class java/nio/file/FileSystem
getPath
(Ljava/lang/String;[Ljava/lang/String;)Ljava/nio/file/Path;
(Ljava/lang/String;[Ljava/lang/String;)L1java/nio/file/Path;
4 changes: 4 additions & 0 deletions etc/eea/java/nio/file/spi/FileSystemProvider.eea
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class java/nio/file/spi/FileSystemProvider
getPath
(Ljava/net/URI;)Ljava/nio/file/Path;
(Ljava/net/URI;)L1java/nio/file/Path;
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected KeyValues load(LoaderContext context, KeyValuesResource resource) thro

@Override
boolean matches(KeyValuesResource resource, KeyValuesEnvironment environment) {
return filePathOrNull(resource.uri(), environment.getFileSystem()) != null;
return isFileURI(resource.uri());
}
},
SYSTEM(KeyValuesResource.SCHEMA_SYSTEM) {
Expand Down Expand Up @@ -163,8 +163,9 @@ protected KeyValuesLoader loader(LoaderContext context, KeyValuesResource resour
protected abstract KeyValues load(LoaderContext context, KeyValuesResource resource) throws IOException;

protected KeyValues load(LoaderContext context, KeyValuesResource resource, Parser parser) throws IOException {
var is = openURI(resource.uri(), context.environment().getResourceStreamLoader(),
context.environment().getFileSystem());
var fileSystem = context.environment().getFileSystem();
var cwd = context.environment().getCWD();
var is = openURI(resource.uri(), context.environment().getResourceStreamLoader(), fileSystem, cwd);
return parser.parse(resource, is);
}

Expand Down Expand Up @@ -215,17 +216,37 @@ static KeyValues profiles(String scheme, LoaderContext context, KeyValuesResourc
return kvs;
}

static @Nullable Path filePathOrNull(URI u, FileSystem fileSystem) {
static boolean isFileURI(URI u) {
if ("file".equals(u.getScheme())) {
return true;
}
if (u.getScheme() == null && u.getPath() != null) {
return true;
}
return false;
}

static @Nullable Path filePathOrNull(URI u, FileSystem fileSystem, @Nullable Path cwd) {
String path;
if ("file".equals(u.getScheme())) {
return fileSystem.provider().getPath(u);
return resolvePath(fileSystem.provider().getPath(u), cwd);
}
else if (u.getScheme() == null && (path = u.getPath()) != null) {
return fileSystem.getPath(path);
return resolvePath(fileSystem.getPath(path), cwd);
}
return null;
}

static Path resolvePath(Path path, @Nullable Path cwd) {
if (cwd == null) {
return path;
}
if (path.isAbsolute()) {
return path;
}
return cwd.resolve(path);
}

static String normalizePath(URI u) {
String p = u.getPath();
if (p == null || p.equals("/")) {
Expand All @@ -237,7 +258,8 @@ else if (p.startsWith("/")) {
return p;
}

static InputStream openURI(URI u, ResourceStreamLoader loader, FileSystem fileSystem) throws IOException {
static InputStream openURI(URI u, ResourceStreamLoader loader, FileSystem fileSystem, @Nullable Path cwd)
throws IOException {
if ("classpath".equals(u.getScheme())) {
String path = u.getPath();
if (path == null) {
Expand All @@ -254,7 +276,7 @@ else if (path.startsWith("/")) {
return stream;
}

var path = filePathOrNull(u, fileSystem);
var path = filePathOrNull(u, fileSystem, cwd);
if (path != null) {
return Files.newInputStream(path);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.Map;
import java.util.Properties;
import java.util.Random;
Expand Down Expand Up @@ -111,6 +112,14 @@ default FileSystem getFileSystem() {
return FileSystems.getDefault();
}

/**
* Gets the current working directory possibly using the passed in filesystem.
* @return cwd or {@code null}
*/
default @Nullable Path getCWD() {
return null;
}

/**
* Retrieves the system class loader. By default, delegates to
* {@link ClassLoader#getSystemClassLoader()}.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.jstach.ezkv.maven;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Properties;
import java.util.Set;
Expand Down Expand Up @@ -81,6 +82,13 @@ public ReadConfigMojo() {
@Parameter(defaultValue = "true")
private boolean override = true;

/**
* Whether to use the real current working directory or use the project
* {@code basedir}.
*/
@Parameter(defaultValue = "false")
private boolean realCWD = false;

/**
* Default scope for test access.
* @param urls The URLs to set for tests.
Expand Down Expand Up @@ -115,6 +123,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
var project = requireInjected(this.project, "project");
var override = this.override;
var originalProperties = project.getProperties();
var path = realCWD ? null : project.getBasedir().toPath().toAbsolutePath();

KeyValuesEnvironment env = new KeyValuesEnvironment() {
@Override
Expand All @@ -126,12 +135,19 @@ public Properties getSystemProperties() {
public KeyValuesEnvironment.Logger getLogger() {
return logger;
}

@Override
public @Nullable Path getCWD() {
return path;
}
};
var loader = KeyValuesSystem.builder()

var system = KeyValuesSystem.builder()
.useServiceLoader() //
.environment(env) //
.build()
.loader();
.build();

var loader = system.loader();

final Variables missingSystemPropeties = new Variables() {
final Set<String> requiredSystemProperties = Collections.singleton("user.home");
Expand All @@ -155,9 +171,14 @@ public KeyValuesEnvironment.Logger getLogger() {
loader.add(url);
}
try {
if (log.isDebugEnabled()) {
var cwd = system.environment().getCWD();
cwd = cwd == null ? system.environment().getFileSystem().getPath(".") : cwd;
log.debug("CWD: " + cwd.toAbsolutePath());
}
var kvs = loader.load().memoize();
if (log.isDebugEnabled()) {
getLog().debug("Found keys: " + kvs);
log.debug("Found keys: " + kvs);
}
// TODO should we do some sort of lock here?
// Technically properties is synchronized
Expand All @@ -169,7 +190,7 @@ public KeyValuesEnvironment.Logger getLogger() {

}
catch (IOException e) {
throw new MojoExecutionException("failed to load config", e);
throw new MojoExecutionException("Failed to load key values as properties", e);
}

}
Expand Down Expand Up @@ -207,4 +228,8 @@ void setOverride(boolean override) {
this.override = override;
}

void setRealCWD(boolean realCWD) {
this.realCWD = realCWD;
}

}

0 comments on commit 1935565

Please sign in to comment.