[GRADLE-1635] Expand in Copy Task should only replace variables that exist in the scope Created: 23/Jun/11  Updated: 10/Feb/17  Resolved: 10/Feb/17

Status: Resolved
Project: Gradle
Affects Version/s: 1.0-milestone-3
Fix Version/s: None

Type: Bug
Reporter: Jani Assignee: Unassigned
Resolution: Won't Fix Votes: 5

Attachments: File build.gradle     XML File install.xml    


If expand is used in the copy task gradle tries to replace all strings starting with $ with gradle variables. So if a file contains a string starting with $ that is not a string to be replaced the copy task fails.

Here's my example:

1. Build script
task prepareInstaller(type: Copy, dependsOn: jar) {
into "$buildDir/installer"
from "src/main/izpack"
include "."
// get nur für xml
expand(jarName: '$jar.archivePath.name', version: '2.3.1')
from jar.archivePath

{ from configurations.compile }


2. File to be copied with expand:
<pack name="Base" required="yes" id="base">
<description>The base files of the application.</description>
<file src="Licence.txt" targetdir="$INSTALL_PATH" />
<file src="Baralga-Tray.ico" targetdir="$INSTALL_PATH" />
<file src="$jarName" targetdir="$INSTALL_PATH" />
<file src="lib" targetdir="$INSTALL_PATH" />
<file src="data" targetdir="$INSTALL_PATH" />
<include name="lib/**" />

The variable $jarName is expanded correctly yet $INSTALL_PATH is a variable that must not be replaced. Yet gradle fails because it can not find the variable INSTALL_PATH.

By the way:
This works absolutely fine in maven as maven replaces only the the variables it knows of.

Comment by Benjamin Muschko [ 27/Sep/11 ]

Jani, did you find a workaround for this in the meantime? I am running into the same issue.

Comment by Jani [ 27/Sep/11 ]


well sort of: I ended up adding all variables that gradle wants to replace as variables. So I added a variable INSTALL_PATH having the value $INSTALL_PATH and so on. This makes the build file quite ugly if there are many.


Comment by Benjamin Muschko [ 01/Oct/11 ]

I had a look at the Gradle source code. The expand method for the Copy task type uses groovy.text.SimpleTemplateEngine which replaces ${..} as well as $.. by default. This is usually not what people want for regular templates. All too often this is used for shell scripts that use environment variables itself.

Jani, thanks for your reply. I am not sure if that helps you but I used the Ant filter ExpandProperties instead:

ant.property(name: 'jarName', value: jar.archivePath.name)
filter(org.apache.tools.ant.filters.ExpandProperties, project: ant.antProject)

This requires using ${..} as replacement tokens. The problem is that you cannot passing tokens. You'll have to define them as Ant properties in your project which is not great.

Comment by Benjamin Muschko [ 11/Oct/11 ]

Sent pull request that provides an expand method for using an implementation of groovy.text.TemplateEngine.

Comment by Benjamin Muschko [ 15/Nov/16 ]

As announced on the Gradle blog we are planning to completely migrate issues from JIRA to GitHub.

We intend to prioritize issues that are actionable and impactful while working more closely with the community. Many of our JIRA issues are inactionable or irrelevant. We would like to request your help to ensure we can appropriately prioritize JIRA issues you’ve contributed to.

Please confirm that you still advocate for your JIRA issue before December 10th, 2016 by:

  • Checking that your issues contain requisite context, impact, behaviors, and examples as described in our published guidelines.
  • Leave a comment on the JIRA issue or open a new GitHub issue confirming that the above is complete.

We look forward to collaborating with you more closely on GitHub. Thank you for your contribution to Gradle!

Comment by Benjamin Muschko [ 10/Feb/17 ]

Thanks again for reporting this issue. We haven't heard back from you after our inquiry from November 15th. We are closing this issue now. Please create an issue on GitHub if you still feel passionate about getting it resolved.

Generated at Wed Jun 30 12:00:33 CDT 2021 using Jira 8.4.2#804003-sha1:d21414fc212e3af190e92c2d2ac41299b89402cf.