diff --git a/pom.xml b/pom.xml index 12434295..c0dff56a 100644 --- a/pom.xml +++ b/pom.xml @@ -63,6 +63,11 @@ Graham Leggett + + @ImadBL + Imad BELMOUJAHID + belmoujahid.i@gmail.com + @@ -95,6 +100,11 @@ maven-shared-utils 3.3.4 + + com.fasterxml.jackson.core + jackson-databind + 2.2.2 + org.codehaus.plexus plexus-utils diff --git a/src/main/java/org/apache/maven/shared/filtering/AbstractMavenFilteringRequest.java b/src/main/java/org/apache/maven/shared/filtering/AbstractMavenFilteringRequest.java index 0b10ab81..5afacebb 100644 --- a/src/main/java/org/apache/maven/shared/filtering/AbstractMavenFilteringRequest.java +++ b/src/main/java/org/apache/maven/shared/filtering/AbstractMavenFilteringRequest.java @@ -28,6 +28,7 @@ import org.apache.maven.project.MavenProject; /** + * @author Imad BELMOUJAHID @ImadBL + */ class BaseFilter extends AbstractLogEnabled implements DefaultFilterInfo @@ -120,7 +123,7 @@ public List 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 ); @@ -138,7 +141,7 @@ public List getDefaultFilterWrappers( final AbstractMav buildFilters.removeAll( request.getFileFilters() ); } - loadProperties( filterProperties, basedir, buildFilters, baseProps ); + loadProperties( filterProperties, basedir, buildFilters, baseProps, request.getRootNode() ); } // Project properties @@ -182,10 +185,11 @@ public List 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 propertiesFilePaths, - Properties baseProps ) + Properties baseProps, String rootNode ) throws MavenFilteringException { if ( propertiesFilePaths != null ) @@ -203,7 +207,8 @@ void loadProperties( Properties filterProperties, File basedir, List 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 ); } diff --git a/src/main/java/org/apache/maven/shared/filtering/MavenResourcesExecution.java b/src/main/java/org/apache/maven/shared/filtering/MavenResourcesExecution.java index 449e405b..287db33a 100644 --- a/src/main/java/org/apache/maven/shared/filtering/MavenResourcesExecution.java +++ b/src/main/java/org/apache/maven/shared/filtering/MavenResourcesExecution.java @@ -34,7 +34,7 @@ /** * A bean to configure a resources filtering execution. - * + * @author Imad BELMOUJAHID @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 e + : getPropertiesFromString( IOUtils.toString( reader ), useThisRoot ).entrySet( ) ) + { + + this.properties.put ( e.getKey (), e.getValue () ); + } + } + } + + /** + * + * @param json + * @param useThisRoot + * @return + * @throws JsonProcessingException + */ + private HashMap getPropertiesFromString( String json, String useThisRoot ) + throws IOException + { + String str = ""; + HashMap hashMap = new HashMap<>(); + ObjectMapper mapper = new ObjectMapper(); + List 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 result, String str, + JsonNode jsonNode, List keys, String useThisRoot ) + { + if ( jsonNode.isObject() ) + { + Iterator 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() ); + } + + } + } +} \ No newline at end of file diff --git a/src/main/java/org/apache/maven/shared/filtering/PropertyUtils.java b/src/main/java/org/apache/maven/shared/filtering/PropertyUtils.java index b37f7de4..00e4c86e 100644 --- a/src/main/java/org/apache/maven/shared/filtering/PropertyUtils.java +++ b/src/main/java/org/apache/maven/shared/filtering/PropertyUtils.java @@ -32,6 +32,7 @@ import org.codehaus.plexus.logging.Logger; /** + * @author Kenney Westerhof * @author William Ferguson */ @@ -54,13 +55,14 @@ private PropertyUtils() * * @param propFile The property file to load. * @param baseProps Properties containing the initial values to substitute into the properties file. + * @param rootNode I have added this parameter for new feature with JSON files ### MRESOURCES-284 ### * @return Properties object containing the properties in the file with their values fully resolved. * @throws IOException if profile does not exist, or cannot be read. */ - public static Properties loadPropertyFile( File propFile, Properties baseProps ) + public static Properties loadPropertyFile( File propFile, Properties baseProps, String rootNode ) throws IOException { - return loadPropertyFile( propFile, baseProps, null ); + return loadPropertyFile( propFile, baseProps, null, rootNode ); } /** @@ -73,12 +75,14 @@ public static Properties loadPropertyFile( File propFile, Properties baseProps ) * @param propFile The property file to load. * @param baseProps Properties containing the initial values to substitute into the properties file. * @param logger Logger instance + * @param rootNode I have added this parameter for new feature with JSON files ### MRESOURCES-284 ### * @return Properties object containing the properties in the file with their values fully resolved. * @throws IOException if profile does not exist, or cannot be read. * * @since 3.1.2 */ - public static Properties loadPropertyFile( File propFile, Properties baseProps, Logger logger ) + public static Properties loadPropertyFile( File propFile, Properties baseProps, Logger logger, + String rootNode ) throws IOException { if ( !propFile.exists() ) @@ -90,7 +94,17 @@ public static Properties loadPropertyFile( File propFile, Properties baseProps, try ( InputStream inStream = Files.newInputStream( propFile.toPath() ) ) { - fileProps.load( inStream ); + // I added this control to support JSON files + if ( propFile.getName().endsWith( ".json" ) ) + { + PropertiesJson propertiesJson = new PropertiesJson(); + propertiesJson.load( propFile, rootNode ); + fileProps.putAll( propertiesJson.getProperties() ); + } + else + { + fileProps.load( inStream ); + } } final Properties combinedProps = new Properties(); @@ -122,13 +136,15 @@ public static Properties loadPropertyFile( File propFile, Properties baseProps, * @param propfile The property file to load * @param fail whether to throw an exception when the file cannot be loaded or to return null * @param useSystemProps whether to incorporate System.getProperties settings into the returned Properties object. + * @param rootNode I have added this parameter for new feature with JSON files ### MRESOURCES-284 ### * @return the loaded and fully resolved Properties object * @throws IOException if profile does not exist, or cannot be read. */ - public static Properties loadPropertyFile( File propfile, boolean fail, boolean useSystemProps ) + public static Properties loadPropertyFile( File propfile, boolean fail, boolean useSystemProps, + String rootNode ) throws IOException { - return loadPropertyFile( propfile, fail, useSystemProps, null ); + return loadPropertyFile( propfile, fail, useSystemProps, null, rootNode ); } /** @@ -138,12 +154,14 @@ public static Properties loadPropertyFile( File propfile, boolean fail, boolean * @param fail whether to throw an exception when the file cannot be loaded or to return null * @param useSystemProps whether to incorporate System.getProperties settings into the returned Properties object. * @param logger Logger instance + * @param rootNode I have added this parameter for new feature with JSON files ### MRESOURCES-284 ### * @return the loaded and fully resolved Properties object * @throws IOException if profile does not exist, or cannot be read. * * @since 3.1.2 */ - public static Properties loadPropertyFile( File propfile, boolean fail, boolean useSystemProps, Logger logger ) + public static Properties loadPropertyFile( File propfile, boolean fail, boolean useSystemProps, Logger logger, + String rootNode ) throws IOException { @@ -157,7 +175,17 @@ public static Properties loadPropertyFile( File propfile, boolean fail, boolean final Properties resolvedProps = new Properties(); try { - resolvedProps.putAll( loadPropertyFile( propfile, baseProps, logger ) ); + // I added this control to support JSON files + if ( propfile.getName().endsWith( ".json" ) ) + { + PropertiesJson propertiesJson = new PropertiesJson(); + propertiesJson.load( propfile, rootNode ); + resolvedProps.putAll( propertiesJson.getProperties() ); + } + else + { + resolvedProps.putAll( loadPropertyFile( propfile, baseProps, logger, null ) ); + } } catch ( FileNotFoundException e ) { diff --git a/src/test/java/org/apache/maven/shared/filtering/DefaultMavenFileFilterTest.java b/src/test/java/org/apache/maven/shared/filtering/DefaultMavenFileFilterTest.java index 249aa790..60230267 100644 --- a/src/test/java/org/apache/maven/shared/filtering/DefaultMavenFileFilterTest.java +++ b/src/test/java/org/apache/maven/shared/filtering/DefaultMavenFileFilterTest.java @@ -37,7 +37,7 @@ /** * @author Olivier Lamy - * + * @author