simonetallevi / maven-replacer-plugin

Automatically exported from code.google.com/p/maven-replacer-plugin
MIT License
0 stars 0 forks source link

Be like Maven? #36

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
N/A

What is the expected output? What do you see instead?
N/A

What version of the product are you using? On what operating system?
1.3.2/Windows

Please provide any additional information below.
Hi,

Firstly, great plugin.
Secondly, I don't need you to make these changes - I already did it for myself 
on my local version, but I just wanted to offer an idea that might be useful to 
others.

Without boring you with the details (long story) I was using your plugin as a 
solution to a problem that I couldn't easily solve with maven filtering.

My idea for an enhancement is a variable that makes it behave more "like maven" 
filtering.
I made this change to my local version of your plugin and it made things much 
easier for me (and made it consistent with my existing knowledge of maven and 
existing projects).

The changes I made (and submit for your consideration):
1. Value/Map file reads "token=value" rather than "token (newline) value".
2. Value/Map file can contain comments if the line starts with a #.
3. Instead of replacing all instances of "token", I only replaced instances 
that are wrapped in "${" "}".

As my project had been using maven filtering, I had things like

Config file:
db.username=${username}

Properties file
# database login
username=database_user

To use your plugin I would first have to remove all the comments and change it 
to "token (newline) value".
This then produces the output file:
db.database_user=database_user

I then would have to add ${ } around my token names (to avoid unintentional 
replaces) - but this was too much for me, so I changed the plugin.

I changed the readline loop in the "contextsForFile" method of 
TokenValueMapFactory to be:

while ((line = reader.readLine()) != null) {
            lineNumber++;

            line = line.trim();
            if (line.length() == 0 || line.startsWith("#")) {
                continue;
            }

            StringTokenizer tok = new StringTokenizer(line, "=");

            if(tok.countTokens() != 2){
                throw new IllegalArgumentException("Parameters must be defined as <key>=<value> on line " + lineNumber);
            }

            String token = "${" + tok.nextToken().trim() + "}";
            String value = tok.nextToken().trim();

            contexts.add(new Replacement(fileUtils, token, value));
        }

(obviously I'm missing the configuration part of this - I changed it to always 
behave in this fashion which is not what you want).

Enjoy,

Marc

Original issue reported on code.google.com by marc.rie...@gmail.com on 6 Sep 2010 at 1:32

GoogleCodeExporter commented 9 years ago
Hi Marc,

Thanks for the suggestions. 
With the ${token} tokens, you may turn off regex matching (since the $ is a 
regex character) to make these tokens match and make your token ${username}.

I used to have my token/value map file use '=' but people were having problems 
matching tokens which contained '=' characters. So I decided to use new lines 
to separate instead (which was a much simpler solution than escaping out).

I can add '#' as a known comment line, but I fear I might run into the same 
issue that I had with the '=' separator. Perhaps I can consider them comments 
if they are the first character. However, I might just give it a go. I could 
just add a parameter to define the separator.

Let me ponder on this for a little while.

Thanks for your feedback,
Steven

Original comment by baker.st...@gmail.com on 6 Sep 2010 at 9:11

GoogleCodeExporter commented 9 years ago
Hi Steven,

Thanks for considering my ideas.
Like I said before, I have already personally modified my own local copy to 
suit me, so I personally am not desperate for these changes! 
Just offering some ideas that helped me.

I fully understand regular expressions and the $ and {} characters weren't 
causing me issues.
The problem was that maven replaces tokens like ${token_name} with values from 
the filter file "token_name=replace_value".
I was trying to use your generic replacement plugin as a "Configuration 
parameter management plugin" which meant I was going to have to go through all 
my filter files and change "token_name" to "${token_name}" to make it do what I 
wanted.

There are a couple of easy tweaks to make the "token=value" thing work with 
escape characters, I tend to use replace on the input string and then split to 
get what I want...
Usually looks like this:

private final static String EQUALS_REPLACEMENT = "_EQUALS_"; //use whatever 
character sequence you like, something people are unlikely to have in their 
files!

line = line.replaceAll("\\\\=", EQUALS_REPLACEMENT); //replace '\=' with some 
"special token" to allow people to escape their inputs
line = line.replaceAll("=", "= "); //replace all actual '=' with '= ' because 
otherwise "a=" is only 1 token and you'll be in trouble
String splitTokens[] = line.split("=");//actual split
if(splitTokens.length != 2){
//fail
}
else{
    token = splitTokens[0].trim();
    value = splitTokens[1].trim();
    /* now replace the special token in the strings with the actual = symbol which is what the inputter requested */
    token = token.replaceAll(EQUALS_REPLACEMENT, "=");
    value = value.replaceAll(EQUALS_REPLACEMENT, "=");
}

As for the comments, maybe you can have a parameter "comments-enabled", default 
= true and if people need to use "#" as the first character they can disable 
comments.

Thanks again for taking the time to consider these suggestions.

Marc

Original comment by marc.rie...@gmail.com on 7 Sep 2010 at 12:16

GoogleCodeExporter commented 9 years ago
Thanks a lot for the sample code, I will give it a go and let you know. :-)

Original comment by baker.st...@gmail.com on 8 Sep 2010 at 3:35

GoogleCodeExporter commented 9 years ago
Thanks a million Marc!

Original comment by laila.fr...@gmail.com on 8 Oct 2010 at 11:12

GoogleCodeExporter commented 9 years ago
Implementation complete.
Will be in next release.

Original comment by baker.st...@gmail.com on 30 Oct 2010 at 2:32

GoogleCodeExporter commented 9 years ago
Released in 1.3.3.

Original comment by baker.st...@gmail.com on 30 Oct 2010 at 3:03

GoogleCodeExporter commented 9 years ago
Just issue clean up.

Original comment by baker.st...@gmail.com on 17 Sep 2012 at 12:42

GoogleCodeExporter commented 9 years ago
make config file unreadable.

Is it possible to specify a separator like new line?

Original comment by reda.abdi on 6 Feb 2013 at 3:04