~ubuntu-branches/ubuntu/utopic/build-helper-maven-plugin/utopic

« back to all changes in this revision

Viewing changes to src/main/java/org/codehaus/mojo/buildhelper/RegexPropertyMojo.java

  • Committer: Package Import Robot
  • Author(s): tony mancill, Emmanuel Bourg, tony mancill
  • Date: 2013-06-01 20:19:34 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20130601201934-hzv7zmdcgazfyupp
Tags: 1.7-1
[ Emmanuel Bourg ]
* Team upload.
* New upstream release
* Set the compiler source/target to 1.5
* Updated Standards-Version to 3.9.4 (no changes)
* debian/copyright:
  - Updated the Format URI to 1.0
  - Removed the duplicate Copyright fields

[ tony mancill ]
* debian/control:
  - Add libmaven-invoker-plugin-java to build-depends-indep.
  - Wrap long lines.
  - Update Vcs-Git URL.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.codehaus.mojo.buildhelper;
 
2
 
 
3
/*
 
4
 * The MIT License
 
5
 *
 
6
 * Copyright (c) 2004, The Codehaus
 
7
 *
 
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 
9
 * this software and associated documentation files (the "Software"), to deal in
 
10
 * the Software without restriction, including without limitation the rights to
 
11
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 
12
 * of the Software, and to permit persons to whom the Software is furnished to do
 
13
 * so, subject to the following conditions:
 
14
 *
 
15
 * The above copyright notice and this permission notice shall be included in all
 
16
 * copies or substantial portions of the Software.
 
17
 *
 
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 
24
 * SOFTWARE.
 
25
 */
 
26
 
 
27
import org.apache.maven.plugin.AbstractMojo;
 
28
import org.apache.maven.plugin.MojoExecutionException;
 
29
import org.apache.maven.plugin.MojoFailureException;
 
30
import org.apache.maven.project.MavenProject;
 
31
 
 
32
import java.util.regex.Matcher;
 
33
import java.util.regex.Pattern;
 
34
import java.util.regex.PatternSyntaxException;
 
35
 
 
36
/**
 
37
 * Sets a property by applying a regex replacement rule to a supplied value.
 
38
 *
 
39
 * @author Stephen Connolly
 
40
 * @phase validate
 
41
 * @goal regex-property
 
42
 * @description Sets a property after applying a regex
 
43
 * @since 1.7
 
44
 */
 
45
public class RegexPropertyMojo
 
46
    extends AbstractMojo
 
47
{
 
48
 
 
49
    /**
 
50
     * The Maven project.
 
51
     *
 
52
     * @parameter expression="${project}"
 
53
     * @required
 
54
     * @readonly
 
55
     */
 
56
    private MavenProject project;
 
57
 
 
58
    /**
 
59
     * The property to set.
 
60
     *
 
61
     * @parameter
 
62
     * @required
 
63
     */
 
64
    private String name;
 
65
 
 
66
    /**
 
67
     * The pre-transformation value.
 
68
     *
 
69
     * @parameter
 
70
     * @required
 
71
     */
 
72
    private String value;
 
73
 
 
74
    /**
 
75
     * The regex to replace.
 
76
     *
 
77
     * @parameter
 
78
     * @required
 
79
     */
 
80
    private String regex;
 
81
 
 
82
    /**
 
83
     * The replacement.
 
84
     *
 
85
     * @parameter
 
86
     * @required
 
87
     */
 
88
    private String replacement;
 
89
 
 
90
    /**
 
91
     * Whether to fail if no match is found.
 
92
     *
 
93
     * @parameter default-value="true"
 
94
     */
 
95
    private boolean failIfNoMatch;
 
96
 
 
97
    /**
 
98
     * {@inheritDoc}
 
99
     */
 
100
    public void execute()
 
101
        throws MojoExecutionException, MojoFailureException
 
102
    {
 
103
        Pattern pattern;
 
104
        try
 
105
        {
 
106
            pattern = Pattern.compile( regex );
 
107
        }
 
108
        catch ( PatternSyntaxException e )
 
109
        {
 
110
            throw new MojoExecutionException( e.getMessage(), e );
 
111
        }
 
112
        Matcher matcher = pattern.matcher( value );
 
113
        if ( !matcher.find() )
 
114
        {
 
115
            if ( failIfNoMatch )
 
116
            {
 
117
                throw new MojoFailureException( "No match to regex '" + regex + "' found in '" + value + "'." );
 
118
            }
 
119
            else
 
120
            {
 
121
                getLog().info( "No match to regex '" + regex + "' found in '" + value + "'." );
 
122
            }
 
123
        }
 
124
        else
 
125
        {
 
126
            value = matcher.replaceAll( replacement );
 
127
        }
 
128
        getLog().info( "Setting property '" + name + "' to '" + value + "'." );
 
129
        project.getProperties().setProperty( name, value );
 
130
    }
 
131
 
 
132
}