Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@
<contributor>
<name>Graham Leggett</name>
</contributor>
<contributor>
<url>@ImadBL</url>
<name>Imad BELMOUJAHID</name>
<email>belmoujahid.i@gmail.com</email>
</contributor>
</contributors>

<dependencies>
Expand Down Expand Up @@ -95,6 +100,11 @@
<artifactId>maven-shared-utils</artifactId>
<version>3.3.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@
import org.apache.maven.project.MavenProject;

/**
* @author <a href="mailto:BELMOUJAHID.I@Gmail.Com>Imad BELMOUJAHID</a> @ImadBL
* @since 1.0-beta-3
*/
public class AbstractMavenFilteringRequest
{

private MavenProject mavenProject;

private String rootNode;

private List<String> filters;

private boolean escapeWindowsPaths = true;
Expand Down Expand Up @@ -172,6 +175,24 @@ public void setFileFilters( List<String> paramfilters )
setFilters( paramfilters );
}

/**
* get RootNode
* @return String using with json file
*/
public String getRootNode()
{
return rootNode;
}

/**
* set RootNode
* @param rootNode String using with json file
*/
public void setRootNode( String rootNode )
{
this.rootNode = rootNode;
}

/**
* @since 1.0-beta-3
* @return true if escape is activated false otherwise.
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/org/apache/maven/shared/filtering/BaseFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
import org.codehaus.plexus.interpolation.multi.MultiDelimiterStringSearchInterpolator;
import org.codehaus.plexus.logging.AbstractLogEnabled;

/**
* @author <a href="mailto:BELMOUJAHID.I@Gmail.Com>Imad BELMOUJAHID</a> @ImadBL
*/
class BaseFilter
extends AbstractLogEnabled
implements DefaultFilterInfo
Expand Down Expand Up @@ -120,7 +123,7 @@ public List<FileUtils.FilterWrapper> getDefaultFilterWrappers( final AbstractMav

File basedir = request.getMavenProject() != null ? request.getMavenProject().getBasedir() : new File( "." );

loadProperties( filterProperties, basedir, request.getFileFilters(), baseProps );
loadProperties( filterProperties, basedir, request.getFileFilters(), baseProps, request.getRootNode() );
if ( filterProperties.size() < 1 )
{
filterProperties.putAll( baseProps );
Expand All @@ -138,7 +141,7 @@ public List<FileUtils.FilterWrapper> getDefaultFilterWrappers( final AbstractMav
buildFilters.removeAll( request.getFileFilters() );
}

loadProperties( filterProperties, basedir, buildFilters, baseProps );
loadProperties( filterProperties, basedir, buildFilters, baseProps, request.getRootNode() );
}

// Project properties
Expand Down Expand Up @@ -182,10 +185,11 @@ public List<FileUtils.FilterWrapper> getDefaultFilterWrappers( final AbstractMav
}

/**
* UseRootThisNode this parameter is null (used only for JSON files) for more information ### MRESOURCES-284 ###
* default visibility only for testing reason !
*/
void loadProperties( Properties filterProperties, File basedir, List<String> propertiesFilePaths,
Properties baseProps )
Properties baseProps, String rootNode )
throws MavenFilteringException
{
if ( propertiesFilePaths != null )
Expand All @@ -203,7 +207,8 @@ void loadProperties( Properties filterProperties, File basedir, List<String> pro
try
{
File propFile = FileUtils.resolveFile( basedir, filterFile );
Properties properties = PropertyUtils.loadPropertyFile( propFile, workProperties, getLogger() );
Properties properties = PropertyUtils.loadPropertyFile( propFile, workProperties, getLogger(),
rootNode );
filterProperties.putAll( properties );
workProperties.putAll( properties );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

/**
* A bean to configure a resources filtering execution.
*
* @author <a href="mailto:BELMOUJAHID.I@Gmail.Com>Imad BELMOUJAHID</a> @ImadBL
* @author Olivier Lamy
*/
public class MavenResourcesExecution
Expand Down Expand Up @@ -118,6 +118,11 @@ public class MavenResourcesExecution
*/
private boolean flatten = false;

/**
* rootNode
*/
private String rootNode;

/**
* Do nothing.
*/
Expand All @@ -126,6 +131,32 @@ public MavenResourcesExecution()
// no op
}

/**
*
* @param resources The list of resources.
* @param outputDirectory The output directory.
* @param mavenProject The maven project.
* @param encoding The given encoding.
* @param fileFilters The file filters.
* @param nonFilteredFileExtensions The extensions which should not being filtered.
* @param mavenSession The maven session.
* @param rootNode param used for json files
*/
public MavenResourcesExecution( List<Resource> resources, File outputDirectory, MavenProject mavenProject,
String encoding, List<String> fileFilters, List<String> nonFilteredFileExtensions,
MavenSession mavenSession, String rootNode )
{
super( mavenProject, fileFilters, mavenSession );
this.encoding = encoding;
this.resources = resources;
this.outputDirectory = outputDirectory;
this.nonFilteredFileExtensions = nonFilteredFileExtensions;
this.useDefaultFilterWrappers = true;
this.addDefaultExcludes = true;
this.resourcesBaseDirectory = mavenProject.getBasedir();
this.rootNode = rootNode;
}

/**
* As we use a Maven project <code>useDefaultFilterWrappers</code> will be set to <code>true</code>. The
* {@code useDefaultExcludes} is set to {@code true}.
Expand Down Expand Up @@ -320,6 +351,24 @@ public Reader getReader( Reader reader )
} );
}

/**
* get RootNode
* @return
*/
public String getRootNode()
{
return rootNode;
}

/**
* set rootNode
* @param rootNode
*/
public void setRootNode( String rootNode )
{
this.rootNode = rootNode;
}

/**
* @return The resource base directory.
*/
Expand Down
176 changes: 176 additions & 0 deletions src/main/java/org/apache/maven/shared/filtering/PropertiesJson.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package org.apache.maven.shared.filtering;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Properties;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;

/**
* I added this class to support JSON files MRESOURCES-284
* @author <a href="mailto:belmoujahid.i@gmail.com">Imad BELMOUJAHID</a> @ImadBL
*/
public class PropertiesJson
{
/**
* properties
*/
private final Properties properties;

/**
* default constructor
*/
public PropertiesJson ()
{
properties = new Properties ();
}

/**
*
* @return properties
*/
public Properties getProperties ()
{
return properties;
}

/**
*
* @param fr
* @param useThisRoot
* @throws IOException
*/
public void load ( File fr, String useThisRoot )
throws IOException
{
try ( FileReader reader = new FileReader ( fr ) )
{
for ( Map.Entry<String, Object> e
: getPropertiesFromString( IOUtils.toString( reader ), useThisRoot ).entrySet( ) )
{

this.properties.put ( e.getKey (), e.getValue () );
}
}
}

/**
*
* @param json
* @param useThisRoot
* @return
* @throws JsonProcessingException
*/
private HashMap<String, Object> getPropertiesFromString( String json, String useThisRoot )
throws IOException
{
String str = "";
HashMap<String, Object> hashMap = new HashMap<>();
ObjectMapper mapper = new ObjectMapper();
List<String> keys = new ArrayList<>();
JsonNode jsonNode = mapper.readTree( json );
getPropertiesFromJsonNode( hashMap, str, jsonNode, keys, useThisRoot );
return hashMap;
}

/**
*
* @param result
* @param str
* @param jsonNode
* @param keys
* @param useThisRoot
*/
private void getPropertiesFromJsonNode ( HashMap<String, Object> result, String str,
JsonNode jsonNode, List<String> keys, String useThisRoot )
{
if ( jsonNode.isObject() )
{
Iterator<String> fieldNames = jsonNode.fieldNames();
while ( fieldNames.hasNext() )
{
String fieldName = fieldNames.next();
keys.add ( fieldName );
if ( jsonNode.get( fieldName ).isObject() )
{
String r;
if ( str != null && str.isEmpty () )
{
r = str.concat( fieldName );
}
else
{
r = str.concat( "." ).concat( fieldName );
}
getPropertiesFromJsonNode ( result, r, jsonNode.get( fieldName ), keys, useThisRoot );
}
else
{
String r;
if ( str != null && str.isEmpty() )
{
r = str.concat( fieldName );
}
else
{
r = str.concat( "." ).concat( fieldName );
}
getPropertiesFromJsonNode ( result, r, jsonNode.get( fieldName ), keys, useThisRoot );
}
}
}
else if ( jsonNode.isArray () )
{
ArrayNode arrayField = ( ArrayNode ) jsonNode;
for ( JsonNode node : arrayField )
{
getPropertiesFromJsonNode ( result, str, node, keys, useThisRoot ) ;
}
}
else
{

if ( null != useThisRoot && !useThisRoot.isEmpty() )
{
if ( str.startsWith( useThisRoot + "." ) )
{
result.put ( str.replaceFirst( "^[a-zA-Z1-9]*.", "" ), jsonNode.textValue( ) );
}
}
else
{
result.put ( str, jsonNode.textValue() );
}

}
}
}
Loading