~ubuntu-branches/ubuntu/trusty/build-helper-maven-plugin/trusty-proposed

« back to all changes in this revision

Viewing changes to src/main/java/org/codehaus/mojo/buildhelper/ReleasedVersionMojo.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 java.util.List;
 
28
import java.util.Properties;
 
29
 
 
30
import org.apache.maven.artifact.ArtifactUtils;
 
31
import org.apache.maven.artifact.factory.ArtifactFactory;
 
32
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
 
33
import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
 
34
import org.apache.maven.artifact.repository.ArtifactRepository;
 
35
import org.apache.maven.artifact.versioning.ArtifactVersion;
 
36
import org.apache.maven.plugin.AbstractMojo;
 
37
import org.apache.maven.project.MavenProject;
 
38
 
 
39
/**
 
40
 * Resolve the latest released version of this project.
 
41
 * This mojo sets the following properties:
 
42
 * 
 
43
 * <pre>
 
44
 *   [propertyPrefix].version
 
45
 *   [propertyPrefix].majorVersion
 
46
 *   [propertyPrefix].minorVersion
 
47
 *   [propertyPrefix].incrementalVersion   
 
48
 * </pre>
 
49
 * 
 
50
 * Where the propertyPrefix is the string set in the mojo parameter.
 
51
 * 
 
52
 * @author Robert Scholte
 
53
 * @goal released-version
 
54
 * @phase validate
 
55
 * @since 1.6
 
56
 * @threadSafe
 
57
 */
 
58
public class ReleasedVersionMojo
 
59
    extends AbstractMojo
 
60
{
 
61
 
 
62
    /**
 
63
     * The Maven Project.
 
64
     * 
 
65
     * @parameter expression="${project}"
 
66
     * @required
 
67
     * @readonly
 
68
     */
 
69
    private MavenProject project;
 
70
 
 
71
    /**
 
72
     * The artifact metadata source to use.
 
73
     * 
 
74
     * @component
 
75
     * @required
 
76
     * @readonly
 
77
     */
 
78
    private ArtifactMetadataSource artifactMetadataSource;
 
79
 
 
80
    /**
 
81
     * @component
 
82
     * @required
 
83
     * @readonly
 
84
     */
 
85
    private ArtifactFactory artifactFactory;
 
86
 
 
87
    /**
 
88
     * @parameter expression="${localRepository}"
 
89
     * @readonly
 
90
     * @required
 
91
     */
 
92
    private ArtifactRepository localRepository;
 
93
 
 
94
    /**
 
95
     * @parameter expression="${project.remoteArtifactRepositories}"
 
96
     * @readonly
 
97
     * @required
 
98
     */
 
99
    private List<ArtifactRepository> remoteArtifactRepositories;
 
100
 
 
101
    /**
 
102
     * Prefix string to use for the set of version properties.
 
103
     * 
 
104
     * @parameter default-value="releasedVersion"
 
105
     */
 
106
    private String propertyPrefix;
 
107
 
 
108
    @SuppressWarnings( "unchecked" )
 
109
    public void execute()
 
110
    {
 
111
        org.apache.maven.artifact.Artifact artifact =
 
112
            artifactFactory.createArtifact( project.getGroupId(), project.getArtifactId(), "", "", "" );
 
113
        try
 
114
        {
 
115
            ArtifactVersion releasedVersion = null;
 
116
            List<ArtifactVersion> versions = artifactMetadataSource
 
117
                .retrieveAvailableVersions( artifact, localRepository, remoteArtifactRepositories );
 
118
            for ( ArtifactVersion version : versions )
 
119
            {
 
120
                if ( !ArtifactUtils.isSnapshot( version.toString() )
 
121
                    && ( releasedVersion == null || version.compareTo( releasedVersion ) == 1 ) )
 
122
                {
 
123
                    releasedVersion = version;
 
124
                }
 
125
            }
 
126
            if ( releasedVersion != null )
 
127
            {
 
128
                String releasedVersionValue;
 
129
 
 
130
                // Use ArtifactVersion.toString(), the major, minor and incrementalVersion return all an int.
 
131
                // This would not always reflect the expected version.
 
132
                int dashIndex = releasedVersion.toString().indexOf( '-' );
 
133
                if ( dashIndex >= 0 )
 
134
                {
 
135
                    releasedVersionValue = releasedVersion.toString().substring( 0, dashIndex );
 
136
                }
 
137
                else
 
138
                {
 
139
                    releasedVersionValue = releasedVersion.toString();
 
140
                }
 
141
                if ( getLog().isDebugEnabled() )
 
142
                {
 
143
                    getLog().debug( propertyPrefix + ".version = " + releasedVersionValue );
 
144
                }
 
145
                Properties props = project.getProperties(); 
 
146
                props.setProperty( propertyPrefix + ".version", 
 
147
                                   releasedVersionValue );
 
148
                props.setProperty( propertyPrefix + ".majorVersion", 
 
149
                                   Integer.toString( releasedVersion.getMajorVersion() ) );
 
150
                props.setProperty( propertyPrefix + ".minorVersion", 
 
151
                                   Integer.toString( releasedVersion.getMinorVersion() ) );
 
152
                props.setProperty( propertyPrefix + ".incrementalVersion",
 
153
                                   Integer.toString( releasedVersion.getIncrementalVersion() ) );
 
154
 
 
155
            }
 
156
 
 
157
        }
 
158
        catch ( ArtifactMetadataRetrievalException e )
 
159
        {
 
160
            if ( getLog().isWarnEnabled() )
 
161
            {
 
162
                getLog().warn( "Failed to retrieve artifacts metadata, cannot resolve the released version" );
 
163
            }
 
164
        }
 
165
    }
 
166
}