Author: khmarbaise Date: Thu May 5 20:49:13 2016 New Revision: 1742471 URL: http://svn.apache.org/viewvc?rev=1742471&view=rev Log: [MRESOURCES-99] ${project.baseUri} and ${maven.build.timestamp} are not expanded by resource filtering o Support maven.build.timestamp and project.baseUri which can be used during filtering of resources as in model. o Added MavenBuildTimestamp which is duplicated from maven-model-builder. o Those properties have been added as supplemental properties which solves the issue.
Added: maven/plugins/trunk/maven-resources-plugin/src/it/MRESOURCES-99/ maven/plugins/trunk/maven-resources-plugin/src/it/MRESOURCES-99/pom.xml maven/plugins/trunk/maven-resources-plugin/src/it/MRESOURCES-99/src/ maven/plugins/trunk/maven-resources-plugin/src/it/MRESOURCES-99/src/main/ maven/plugins/trunk/maven-resources-plugin/src/it/MRESOURCES-99/src/main/resources/ maven/plugins/trunk/maven-resources-plugin/src/it/MRESOURCES-99/src/main/resources/SomeResource.txt maven/plugins/trunk/maven-resources-plugin/src/it/MRESOURCES-99/verify.groovy maven/plugins/trunk/maven-resources-plugin/src/main/java/org/apache/maven/plugins/resources/MavenBuildTimestamp.java Modified: maven/plugins/trunk/maven-resources-plugin/src/main/java/org/apache/maven/plugins/resources/ResourcesMojo.java Added: maven/plugins/trunk/maven-resources-plugin/src/it/MRESOURCES-99/pom.xml URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-resources-plugin/src/it/MRESOURCES-99/pom.xml?rev=1742471&view=auto ============================================================================== --- maven/plugins/trunk/maven-resources-plugin/src/it/MRESOURCES-99/pom.xml (added) +++ maven/plugins/trunk/maven-resources-plugin/src/it/MRESOURCES-99/pom.xml Thu May 5 20:49:13 2016 @@ -0,0 +1,49 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> + +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-resources-plugin-it-filter</artifactId> + <packaging>jar</packaging> + <version>1.0-SNAPSHOT</version> + + <properties> + <maven.build.timestamp.format>yyyy-MM-dd'T'HH:mm:ss'Z'</maven.build.timestamp.format> + </properties> + <build> + <resources> + <resource> + <directory>src/main/resources</directory> + <filtering>true</filtering> + </resource> + </resources> + <pluginManagement> + <plugins> + <plugin> + <artifactId>maven-resources-plugin</artifactId> + <version>@project.version@</version> + </plugin> + </plugins> + </pluginManagement> + </build> + +</project> Added: maven/plugins/trunk/maven-resources-plugin/src/it/MRESOURCES-99/src/main/resources/SomeResource.txt URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-resources-plugin/src/it/MRESOURCES-99/src/main/resources/SomeResource.txt?rev=1742471&view=auto ============================================================================== --- maven/plugins/trunk/maven-resources-plugin/src/it/MRESOURCES-99/src/main/resources/SomeResource.txt (added) +++ maven/plugins/trunk/maven-resources-plugin/src/it/MRESOURCES-99/src/main/resources/SomeResource.txt Thu May 5 20:49:13 2016 @@ -0,0 +1,28 @@ +# 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. + +timestamp=${maven.build.timestamp} +at.timestamp=@maven.build.timestamp@ +build.timestamp=${build.timestamp} +project.build.timestamp=${project.build.timestamp} +baseUri=${baseUri} +project.baseUri=${project.baseUri} +pom.baseUri=${pom.baseUri} +at.baseUri=@baseUri@ +groupId=${project.groupId} +basedir=${basedir} +project.basedir=${project.basedir} Added: maven/plugins/trunk/maven-resources-plugin/src/it/MRESOURCES-99/verify.groovy URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-resources-plugin/src/it/MRESOURCES-99/verify.groovy?rev=1742471&view=auto ============================================================================== --- maven/plugins/trunk/maven-resources-plugin/src/it/MRESOURCES-99/verify.groovy (added) +++ maven/plugins/trunk/maven-resources-plugin/src/it/MRESOURCES-99/verify.groovy Thu May 5 20:49:13 2016 @@ -0,0 +1,147 @@ +/* + * 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 java.io.*; + +import java.util.List; +import java.util.Collections; +import java.util.Arrays; + +import org.codehaus.plexus.util.*; + +boolean result = true; + +File target = new File( basedir, "target" ); +if ( !target.exists() || !target.isDirectory() ) +{ + System.err.println( "target folder is missing or not a directory." ); + return false; +} + +File someResource = new File( target, "/classes/SomeResource.txt" ); +if ( !someResource.exists() || someResource.isDirectory() ) +{ + System.err.println( "SomeResource.txt is missing or not a file." ); + return false; +} + +Properties props = new Properties(); + +FileInputStream fis = null; +try +{ + fis = new FileInputStream( someResource ); + props.load( fis ); + fis.close(); + fis = null; +} +catch ( IOException ex ) +{ + System.err.println( "Failure during reading the properties " + someResource.getAbsolutePath() ); + return false; +} +finally +{ + IOUtil.close( fis ); +} + +def keysWhichHaveToExist = [ + "timestamp", + "at.timestamp", + "build.timestamp", + "project.build.timestamp", + "baseUri", + "project.baseUri", + "pom.baseUri", + "at.baseUri", + "groupId", + "basedir", + "project.basedir", +] + + +keysWhichHaveToExist.each { key -> + if (!props.containsKey(key)) { + println "Missing the key '" + key + "'" + return false + } +} + +currentTimestamp = props.get("timestamp") +currentAtTimeStamp = props.get("at.timestamp") +if ( !currentTimestamp.equals (currentAtTimeStamp) + && !currentTimestamp.equals('${maven.build.timestamp}') + && !currentAtTimeStamp.equals('@maven.build.timestamp@') ) { + println 'The ${maven.build.timestamp} has not correctly being replaced.' + return false +} + +buildTimeStamp = props.get("build.timestamp") +if ( !buildTimeStamp.equals ('${build.timestamp}') ) { + println 'The ${build.timestamp} has been replaced.' + return false +} + +currentProjectBuildTimeStamp = props.get('project.build.timestamp') +if ( !currentProjectBuildTimeStamp.equals ('${project.build.timestamp}') ) { + println 'The ${project.build.timestamp} has been replaced.' + return false +} + +currentBaseUri = props.get('baseUri') +if ( !currentBaseUri.equals ('${baseUri}') ) { + println 'The ${baseUri} has been replaced.' + return false +} + +currentProjectBaseUri = props.get('project.baseUri') +if ( currentProjectBaseUri.equals ('${project.baseUri}') ) { + println 'The ${project.baseUri} has not been replaced.' + return false +} + +currentPomBaseUri = props.get('pom.baseUri') +if ( !currentPomBaseUri.equals ('${pom.baseUri}') ) { + println 'The ${pom.baseUri} has been replaced.' + return false +} + +currentAtBaseUri = props.get('at.baseUri') +if ( !currentAtBaseUri.equals ('@baseUri@') ) { + println 'The @baseUri@ has been replaced.' + return false +} + +currentGroupId = props.get('project.groupId') +if ( currentGroupId.equals ('${project.groupId}') ) { + println 'The ${project.groupId} has not been replaced.' + return false +} +currentBaseDir = props.get('basedir') +if ( currentBaseDir.equals ('${basedir}') ) { + println 'The ${basedir} has not been replaced.' + return false +} +currentProjectBaseDir = props.get('project.basedir') +if ( currentBaseDir.equals ('${project.basedir}') ) { + println 'The ${project.basedir} has not been replaced.' + return false +} + +return result; Added: maven/plugins/trunk/maven-resources-plugin/src/main/java/org/apache/maven/plugins/resources/MavenBuildTimestamp.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-resources-plugin/src/main/java/org/apache/maven/plugins/resources/MavenBuildTimestamp.java?rev=1742471&view=auto ============================================================================== --- maven/plugins/trunk/maven-resources-plugin/src/main/java/org/apache/maven/plugins/resources/MavenBuildTimestamp.java (added) +++ maven/plugins/trunk/maven-resources-plugin/src/main/java/org/apache/maven/plugins/resources/MavenBuildTimestamp.java Thu May 5 20:49:13 2016 @@ -0,0 +1,79 @@ +package org.apache.maven.plugins.resources; + +/* + * 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 java.text.SimpleDateFormat; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.Properties; +import java.util.TimeZone; + +/** + * This class is duplicated from maven-model-builder from + * maven core. (See MRESOURCES-99). + * + */ +public class MavenBuildTimestamp +{ + // ISO 8601-compliant timestamp for machine readability + public static final String DEFAULT_BUILD_TIMESTAMP_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'"; + + public static final String BUILD_TIMESTAMP_FORMAT_PROPERTY = "maven.build.timestamp.format"; + + public static final TimeZone DEFAULT_BUILD_TIME_ZONE = TimeZone.getTimeZone( "Etc/UTC" ); + + private String formattedTimestamp; + + public MavenBuildTimestamp() + { + this( new Date() ); + } + + public MavenBuildTimestamp( Date time ) + { + this( time, DEFAULT_BUILD_TIMESTAMP_FORMAT ); + } + + public MavenBuildTimestamp( Date time, Properties properties ) + { + this( time, properties != null ? properties.getProperty( BUILD_TIMESTAMP_FORMAT_PROPERTY ) : null ); + } + + public MavenBuildTimestamp( Date time, String timestampFormat ) + { + if ( timestampFormat == null ) + { + timestampFormat = DEFAULT_BUILD_TIMESTAMP_FORMAT; + } + if ( time == null ) + { + time = new Date(); + } + SimpleDateFormat dateFormat = new SimpleDateFormat( timestampFormat ); + dateFormat.setCalendar( new GregorianCalendar() ); + dateFormat.setTimeZone( DEFAULT_BUILD_TIME_ZONE ); + formattedTimestamp = dateFormat.format( time ); + } + + public String formattedTimestamp() + { + return formattedTimestamp; + } +} Modified: maven/plugins/trunk/maven-resources-plugin/src/main/java/org/apache/maven/plugins/resources/ResourcesMojo.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-resources-plugin/src/main/java/org/apache/maven/plugins/resources/ResourcesMojo.java?rev=1742471&r1=1742470&r2=1742471&view=diff ============================================================================== --- maven/plugins/trunk/maven-resources-plugin/src/main/java/org/apache/maven/plugins/resources/ResourcesMojo.java (original) +++ maven/plugins/trunk/maven-resources-plugin/src/main/java/org/apache/maven/plugins/resources/ResourcesMojo.java Thu May 5 20:49:13 2016 @@ -25,6 +25,7 @@ import java.util.Collection; import java.util.Collections; import java.util.LinkedHashSet; import java.util.List; +import java.util.Properties; import org.apache.maven.execution.MavenSession; import org.apache.maven.model.Resource; @@ -323,6 +324,10 @@ public class ResourcesMojo mavenResourcesExecution.setFilterFilenames( fileNameFiltering ); mavenResourcesExecution.setAddDefaultExcludes( addDefaultExcludes ); + // Handle subject of MRESOURCES-99 + Properties additionalProperties = addSeveralSpecialProperties(); + mavenResourcesExecution.setAdditionalProperties( additionalProperties ); + // if these are NOT set, just use the defaults, which are '${*}' and '@'. mavenResourcesExecution.setDelimiters( delimiters, useDefaultDelimiters ); @@ -341,6 +346,33 @@ public class ResourcesMojo } /** + * This solves https://issues.apache.org/jira/browse/MRESOURCES-99.<br/> + * BUT:<br/> + * This should be done different than defining those properties a second time, cause they have already being defined + * in Maven Model Builder (package org.apache.maven.model.interpolation) via BuildTimestampValueSource. But those + * can't be found in the context which can be got from the maven core.<br/> + * A solution could be to put those values into the context by Maven core so they are accessible everywhere. (I'm + * not sure if this is a good idea). Better ideas are always welcome. + * + * The problem at the moment is that maven core handles usage of properties and replacements in + * the model, but does not the resource filtering which needed some of the properties. + * + * @return the new instance with those properties. + */ + private Properties addSeveralSpecialProperties() + { + String timeStamp = new MavenBuildTimestamp().formattedTimestamp(); + Properties additionalProperties = new Properties(); + additionalProperties.put( "maven.build.timestamp", timeStamp ); + if ( project.getBasedir() != null ) + { + additionalProperties.put( "project.baseUri", project.getBasedir().getAbsoluteFile().toURI().toString() ); + } + + return additionalProperties; + } + + /** * @since 2.5 */ protected void executeUserFilterComponents( MavenResourcesExecution mavenResourcesExecution )