1
package org.codehaus.mojo.buildhelper;
6
* Copyright (c) 2004, The Codehaus
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:
15
* The above copyright notice and this permission notice shall be included in all
16
* copies or substantial portions of the Software.
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
27
import java.util.List;
28
import java.util.Properties;
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;
40
* Resolve the latest released version of this project.
41
* This mojo sets the following properties:
44
* [propertyPrefix].version
45
* [propertyPrefix].majorVersion
46
* [propertyPrefix].minorVersion
47
* [propertyPrefix].incrementalVersion
50
* Where the propertyPrefix is the string set in the mojo parameter.
52
* @author Robert Scholte
53
* @goal released-version
58
public class ReleasedVersionMojo
65
* @parameter expression="${project}"
69
private MavenProject project;
72
* The artifact metadata source to use.
78
private ArtifactMetadataSource artifactMetadataSource;
85
private ArtifactFactory artifactFactory;
88
* @parameter expression="${localRepository}"
92
private ArtifactRepository localRepository;
95
* @parameter expression="${project.remoteArtifactRepositories}"
99
private List<ArtifactRepository> remoteArtifactRepositories;
102
* Prefix string to use for the set of version properties.
104
* @parameter default-value="releasedVersion"
106
private String propertyPrefix;
108
@SuppressWarnings( "unchecked" )
109
public void execute()
111
org.apache.maven.artifact.Artifact artifact =
112
artifactFactory.createArtifact( project.getGroupId(), project.getArtifactId(), "", "", "" );
115
ArtifactVersion releasedVersion = null;
116
List<ArtifactVersion> versions = artifactMetadataSource
117
.retrieveAvailableVersions( artifact, localRepository, remoteArtifactRepositories );
118
for ( ArtifactVersion version : versions )
120
if ( !ArtifactUtils.isSnapshot( version.toString() )
121
&& ( releasedVersion == null || version.compareTo( releasedVersion ) == 1 ) )
123
releasedVersion = version;
126
if ( releasedVersion != null )
128
String releasedVersionValue;
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 )
135
releasedVersionValue = releasedVersion.toString().substring( 0, dashIndex );
139
releasedVersionValue = releasedVersion.toString();
141
if ( getLog().isDebugEnabled() )
143
getLog().debug( propertyPrefix + ".version = " + releasedVersionValue );
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() ) );
158
catch ( ArtifactMetadataRetrievalException e )
160
if ( getLog().isWarnEnabled() )
162
getLog().warn( "Failed to retrieve artifacts metadata, cannot resolve the released version" );