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 org.apache.maven.execution.MavenSession;
28
import org.apache.maven.plugin.AbstractMojo;
29
import org.apache.maven.plugin.MojoExecutionException;
30
import org.apache.maven.plugin.MojoFailureException;
31
import org.apache.maven.project.MavenProject;
32
import org.apache.maven.project.MavenProjectHelper;
34
import java.util.HashSet;
38
* Attach additional artifacts to be installed and deployed.
40
* @goal attach-artifact
42
* @author <a href="dantran@gmail.com">Dan T. Tran</a>
43
* @version $Id: AttachArtifactMojo.java 14208 2011-06-21 21:19:32Z rfscholte $
46
public class AttachArtifactMojo
50
* Attach an array of artifacts to the project.
55
private Artifact [] artifacts;
58
* This project's base directory.
60
* @parameter expression="${basedir}"
64
private String basedir;
69
* @parameter expression="${session}"
74
private MavenSession mavenSession;
77
* @parameter expression="${project}"
81
private MavenProject project;
84
* Maven ProjectHelper.
89
private MavenProjectHelper projectHelper;
92
* This will cause the execution to be run only at the top of a given module
93
* tree. That is, run in the project contained in the same folder where the
94
* mvn execution was launched.
96
* @parameter expression="${buildhelper.runOnlyAtExecutionRoot}" default-value="false"
99
private boolean runOnlyAtExecutionRoot;
102
* This allows to skip the attach execution in case it is known that the corresponding file does not exists.
103
* For exemple, when the previous ant-run task is skipped with a unless.
105
* @parameter expression="${buildhelper.skipAttach}" default-value="false"
108
private boolean skipAttach;
110
public void execute()
111
throws MojoExecutionException, MojoFailureException
116
getLog().info( "Skip attaching artifacts" );
120
// Run only at the execution root
121
if ( runOnlyAtExecutionRoot && !isThisTheExecutionRoot() )
123
getLog().info( "Skip attaching artifacts in this project because it's not the Execution Root" );
127
this.validateArtifacts();
129
for ( Artifact artifact : artifacts )
131
projectHelper.attachArtifact( this.project,
133
artifact.getClassifier(),
134
artifact.getFile() );
141
* Returns <code>true</code> if the current project is located at the
142
* Execution Root Directory (where mvn was launched).
144
* @return <code>true</code> if the current project is at the Execution Root
146
private boolean isThisTheExecutionRoot()
148
if( getLog().isDebugEnabled() )
150
getLog().debug( "Root Folder:" + mavenSession.getExecutionRootDirectory() );
151
getLog().debug( "Current Folder:" + basedir );
154
boolean result = mavenSession.getExecutionRootDirectory().equalsIgnoreCase( basedir.toString() );
155
if( getLog().isDebugEnabled() )
159
getLog().debug( "This is the execution root." );
163
getLog().debug( "This is NOT the execution root." );
169
private void validateArtifacts()
170
throws MojoFailureException
172
// check unique of types and classifiers
173
Set<String> extensionClassifiers = new HashSet<String>();
174
for ( Artifact artifact : artifacts )
176
String extensionClassifier = artifact.getType() + ":" + artifact.getClassifier();
178
if ( !extensionClassifiers.add( extensionClassifier ) )
180
throw new MojoFailureException( "The artifact with same type and classifier: "
181
+ extensionClassifier + " is used more than once." );