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: AttachArtifact.java 11887 2010-02-12 10:13:13Z dennisl $
46
public class AttachArtifact
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;
101
public void execute()
102
throws MojoExecutionException, MojoFailureException
105
// Run only at the execution root
106
if ( runOnlyAtExecutionRoot && !isThisTheExecutionRoot() )
108
getLog().info( "Skip attaching artifacts in this project because it's not the Execution Root" );
112
this.validateArtifacts();
114
for ( int i = 0 ; i < this.artifacts.length; ++ i )
116
projectHelper.attachArtifact( this.project,
117
this.artifacts[i].getType(),
118
this.artifacts[i].getClassifier(),
119
this.artifacts[i].getFile() );
126
* Returns <code>true</code> if the current project is located at the
127
* Execution Root Directory (where mvn was launched).
129
* @return <code>true</code> if the current project is at the Execution Root
131
private boolean isThisTheExecutionRoot()
133
getLog().debug( "Root Folder:" + mavenSession.getExecutionRootDirectory() );
134
getLog().debug( "Current Folder:" + basedir );
135
boolean result = mavenSession.getExecutionRootDirectory().equalsIgnoreCase( basedir.toString() );
138
getLog().debug( "This is the execution root." );
142
getLog().debug( "This is NOT the execution root." );
147
private void validateArtifacts()
148
throws MojoFailureException
150
// check unique of types and classifiers
151
Set extensionClassifiers = new HashSet();
152
for ( int i = 0; i < this.artifacts.length; ++i )
154
Artifact artifact = this.artifacts[i];
156
String extensionClassifier = artifact.getType() + ":" + artifact.getClassifier();
158
if ( !extensionClassifiers.add( extensionClassifier ) )
160
throw new MojoFailureException( "The artifact with same type and classifier: "
161
+ extensionClassifier + " is used more than once." );