GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [MSHARED-1412] Allow to customize Interpolator used by filter by kwin · Pull Request #104 · apache/maven-filtering

Conversation

@kwin

@kwin kwin commented Jun 20, 2024

Copy link
Copy Markdown
Member

Following this checklist to help us incorporate your
contribution quickly and easily:

  • Make sure there is a JIRA issue filed
    for the change (usually before you start working on it). Trivial changes like typos do not
    require a JIRA issue. Your pull request should address just this issue, without
    pulling in other changes.
  • Each commit in the pull request should have a meaningful subject line and body.
  • Format the pull request title like [MSHARED-XXX] - Fixes bug in ApproximateQuantiles,
    where you replace MSHARED-XXX with the appropriate JIRA issue. Best practice
    is to use the JIRA issue title in the pull request title and in the first line of the
    commit message.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Run mvn clean verify -Prun-its to make sure basic checks pass. A more thorough check will
    be performed on your pull request automatically.

If your pull request is about ~20 lines of code you don't need to sign an
Individual Contributor License Agreement if you are unsure
please ask on the developers list.

To make clear that you license your contribution under
the Apache License Version 2.0, January 2004
you have to acknowledge this by using the following check-box.

  • I hereby declare this contribution to be licensed under the Apache License Version 2.0, January 2004

  • In any other case, please file an Apache Individual Contributor License Agreement.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@kwin
kwin requested review from gnodet and slachiewicz June 20, 2024 07:59
@kwin
kwin force-pushed the feature/interpolator-customizer branch from f25600b to b8318ee Compare June 20, 2024 08:01
@gnodet

gnodet commented Jun 20, 2024

Copy link
Copy Markdown
Contributor

@kwin is there an immediate use case for this ? which maven plugin will use it ? just curious...

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@kwin

kwin commented Jun 20, 2024

Copy link
Copy Markdown
Member Author

The use case for me is https://issues.apache.org/jira/browse/JCRVLT-757 (outside the Maven project). But I can imagine that for some advanced use cases also the maven-resources-plugin could leverage that (with automatically registered Sisu components)...

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@olamy

olamy commented Jun 20, 2024

Copy link
Copy Markdown
Member

is there anything you could do with this https://maven.apache.org/plugins/maven-resources-plugin/examples/custom-resource-filters.html ?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@kwin

kwin commented Jun 20, 2024

Copy link
Copy Markdown
Member Author

The problem is I want the default filter behaviour, which is currently not reusable in custom filter (i.e. all the code from

maven-filtering/src/main/java/org/apache/maven/shared/filtering/BaseFilter.java

Lines 74 to 181 in 9d528d0

public List<FilterWrapper> getDefaultFilterWrappers(final AbstractMavenFilteringRequest request)
throws MavenFilteringException {
// backup values
boolean supportMultiLineFiltering = request.isSupportMultiLineFiltering();
request.setSupportMultiLineFiltering(supportMultiLineFiltering);
// Here we build some properties which will be used to read some properties files
// to interpolate the expression ${ } in this properties file
// Take a copy of filterProperties to ensure that evaluated filterTokens are not propagated
// to subsequent filter files. Note: this replicates current behaviour and seems to make sense.
final Properties baseProps = new Properties();
// Project properties
if (request.getMavenProject() != null) {
baseProps.putAll(
request.getMavenProject().getProperties() == null
? Collections.emptyMap()
: request.getMavenProject().getProperties());
}
// TODO this is NPE free but do we consider this as normal
// or do we have to throw an MavenFilteringException with mavenSession cannot be null
//
// khmarbaise: 2016-05-21:
// If we throw an MavenFilteringException tests will fail which is
// caused by for example:
// void copyFile( File from, final File to, boolean filtering, List<FileUtils.FilterWrapper> filterWrappers,
// String encoding )
// in MavenFileFilter interface where no MavenSession is given.
// So changing here to throw a MavenFilteringException would make
// it necessary to change the interface or we need to find a better solution.
//
if (request.getMavenSession() != null) {
// User properties have precedence over system properties
putAll(baseProps, request.getMavenSession().getSystemProperties());
putAll(baseProps, request.getMavenSession().getUserProperties());
}
// now we build properties to use for resources interpolation
final Properties filterProperties = new Properties();
File basedir =
request.getMavenProject() != null ? request.getMavenProject().getBasedir() : new File(".");
loadProperties(filterProperties, basedir, request.getFileFilters(), baseProps);
if (filterProperties.isEmpty()) {
putAll(filterProperties, baseProps);
}
if (request.getMavenProject() != null) {
if (request.isInjectProjectBuildFilters()) {
List<String> buildFilters =
new ArrayList<>(request.getMavenProject().getBuild().getFilters());
// JDK-8015656: (coll) unexpected NPE from removeAll
if (request.getFileFilters() != null) {
buildFilters.removeAll(request.getFileFilters());
}
loadProperties(filterProperties, basedir, buildFilters, baseProps);
}
// Project properties
filterProperties.putAll(
request.getMavenProject().getProperties() == null
? Collections.emptyMap()
: request.getMavenProject().getProperties());
}
if (request.getMavenSession() != null) {
// User properties have precedence over system properties
putAll(filterProperties, request.getMavenSession().getSystemProperties());
putAll(filterProperties, request.getMavenSession().getUserProperties());
}
if (request.getAdditionalProperties() != null) {
// additional properties wins
putAll(filterProperties, request.getAdditionalProperties());
}
List<FilterWrapper> defaultFilterWrappers =
new ArrayList<>(request.getDelimiters().size() + 1);
if (getLogger().isDebugEnabled()) {
getLogger().debug("properties used:");
for (String s : new TreeSet<>(filterProperties.stringPropertyNames())) {
getLogger().debug(s + ": " + filterProperties.getProperty(s));
}
}
final ValueSource propertiesValueSource = new PropertiesBasedValueSource(filterProperties);
FilterWrapper wrapper = new Wrapper(
request.getDelimiters(),
request.getMavenProject(),
request.getMavenSession(),
propertiesValueSource,
request.getProjectStartExpressions(),
request.getEscapeString(),
request.isEscapeWindowsPaths(),
request.isSupportMultiLineFiltering());
defaultFilterWrappers.add(wrapper);
return defaultFilterWrappers;
}
and

maven-filtering/src/main/java/org/apache/maven/shared/filtering/BaseFilter.java

Lines 217 to 323 in 9d528d0

private static final class Wrapper extends FilterWrapper {
private LinkedHashSet<String> delimiters;
private MavenProject project;
private ValueSource propertiesValueSource;
private List<String> projectStartExpressions;
private String escapeString;
private boolean escapeWindowsPaths;
private final MavenSession mavenSession;
private boolean supportMultiLineFiltering;
Wrapper(
LinkedHashSet<String> delimiters,
MavenProject project,
MavenSession mavenSession,
ValueSource propertiesValueSource,
List<String> projectStartExpressions,
String escapeString,
boolean escapeWindowsPaths,
boolean supportMultiLineFiltering) {
super();
this.delimiters = delimiters;
this.project = project;
this.mavenSession = mavenSession;
this.propertiesValueSource = propertiesValueSource;
this.projectStartExpressions = projectStartExpressions;
this.escapeString = escapeString;
this.escapeWindowsPaths = escapeWindowsPaths;
this.supportMultiLineFiltering = supportMultiLineFiltering;
}
@Override
public Reader getReader(Reader reader) {
Interpolator interpolator = createInterpolator(
delimiters,
projectStartExpressions,
propertiesValueSource,
project,
mavenSession,
escapeString,
escapeWindowsPaths);
MultiDelimiterInterpolatorFilterReaderLineEnding filterReader =
new MultiDelimiterInterpolatorFilterReaderLineEnding(
reader, interpolator, supportMultiLineFiltering);
final RecursionInterceptor ri;
if (projectStartExpressions != null && !projectStartExpressions.isEmpty()) {
ri = new PrefixAwareRecursionInterceptor(projectStartExpressions, true);
} else {
ri = new SimpleRecursionInterceptor();
}
filterReader.setRecursionInterceptor(ri);
filterReader.setDelimiterSpecs(delimiters);
filterReader.setInterpolateWithPrefixPattern(false);
filterReader.setEscapeString(escapeString);
return filterReader;
}
}
private static Interpolator createInterpolator(
LinkedHashSet<String> delimiters,
List<String> projectStartExpressions,
ValueSource propertiesValueSource,
MavenProject project,
MavenSession mavenSession,
String escapeString,
boolean escapeWindowsPaths) {
MultiDelimiterStringSearchInterpolator interpolator = new MultiDelimiterStringSearchInterpolator();
interpolator.setDelimiterSpecs(delimiters);
interpolator.addValueSource(propertiesValueSource);
if (project != null) {
interpolator.addValueSource(new PrefixedObjectValueSource(projectStartExpressions, project, true));
}
if (mavenSession != null) {
interpolator.addValueSource(new PrefixedObjectValueSource("session", mavenSession));
final Settings settings = mavenSession.getSettings();
if (settings != null) {
interpolator.addValueSource(new PrefixedObjectValueSource("settings", settings));
interpolator.addValueSource(
new SingleResponseValueSource("localRepository", settings.getLocalRepository()));
}
}
interpolator.setEscapeString(escapeString);
if (escapeWindowsPaths) {
interpolator.addPostProcessor((expression, value) ->
(value instanceof String) ? FilteringUtils.escapeWindowsPath((String) value) : value);
}
return interpolator;
}
}
would need to be duplicated for a custom filter). The interpolator customizer allows to leverage the default filterwhile still extending interpolation features of it.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@kwin

kwin commented Jul 16, 2024

Copy link
Copy Markdown
Member Author

If there is no further input I will merge at the end of the week.

olamy reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@olamy olamy added the enhancement New feature or request label Jul 17, 2024
slachiewicz
slachiewicz approved these changes Jul 28, 2024
View reviewed changes
@kwin
[MSHARED-1412] Allow to customize Interpolator used by filter
9abcf84
@kwin
kwin force-pushed the feature/interpolator-customizer branch from b8318ee to 9abcf84 Compare August 5, 2024 15:21
@kwin
kwin merged commit 0880aea into master Aug 5, 2024
kwin added a commit that referenced this pull request Aug 5, 2024
@kwin
[MSHARED-1412] Allow to customize Interpolator used by filter (#104)
035d1d3
Backport to 3.x
kwin added a commit that referenced this pull request Aug 15, 2024
@kwin
[MSHARED-1412] Allow to customize Interpolator used by filter (#104) (#…
bc0d81b
…114)

Backport to 3.x
@jira-importer

jira-importer commented Jun 17, 2025

Copy link
Copy Markdown

Resolve #263

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@slachiewicz slachiewicz slachiewicz approved these changes
@gnodet gnodet Awaiting requested review from gnodet

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

enhancement New feature or request

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@kwin @gnodet @olamy @jira-importer @slachiewicz
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.