~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/AttachArtifactMojo.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.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;
 
33
 
 
34
import java.util.HashSet;
 
35
import java.util.Set;
 
36
 
 
37
/**
 
38
 * Attach additional artifacts to be installed and deployed.
 
39
 *
 
40
 * @goal attach-artifact
 
41
 * @phase package
 
42
 * @author <a href="dantran@gmail.com">Dan T. Tran</a>
 
43
 * @version $Id: AttachArtifactMojo.java 14208 2011-06-21 21:19:32Z rfscholte $
 
44
 * @since 1.0
 
45
 */
 
46
public class AttachArtifactMojo
 
47
    extends AbstractMojo
 
48
{
 
49
    /**
 
50
     * Attach an array of artifacts to the project.
 
51
     *
 
52
     * @parameter
 
53
     * @required
 
54
     */
 
55
    private Artifact [] artifacts;
 
56
 
 
57
    /**
 
58
     * This project's base directory.
 
59
     *
 
60
     * @parameter expression="${basedir}"
 
61
     * @required
 
62
     * @since 1.5
 
63
     */
 
64
    private String basedir;
 
65
 
 
66
    /**
 
67
     * The Maven Session.
 
68
     *
 
69
     * @parameter expression="${session}"
 
70
     * @readonly
 
71
     * @required
 
72
     * @since 1.5
 
73
     */
 
74
    private MavenSession mavenSession;
 
75
 
 
76
    /**
 
77
     * @parameter expression="${project}"
 
78
     * @required
 
79
     * @readonly
 
80
     */
 
81
    private MavenProject project;
 
82
 
 
83
    /**
 
84
     * Maven ProjectHelper.
 
85
     *
 
86
     * @component
 
87
     * @readonly
 
88
     */
 
89
    private MavenProjectHelper projectHelper;
 
90
 
 
91
    /**
 
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.
 
95
     *
 
96
     * @parameter expression="${buildhelper.runOnlyAtExecutionRoot}" default-value="false"
 
97
     * @since 1.5
 
98
     */
 
99
    private boolean runOnlyAtExecutionRoot;
 
100
 
 
101
    /**
 
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.
 
104
     * 
 
105
     * @parameter expression="${buildhelper.skipAttach}" default-value="false"
 
106
     * @since 1.6
 
107
     */
 
108
    private boolean skipAttach;
 
109
    
 
110
    public void execute()
 
111
        throws MojoExecutionException, MojoFailureException
 
112
    {
 
113
 
 
114
        if ( skipAttach )
 
115
        {
 
116
            getLog().info( "Skip attaching artifacts" );
 
117
            return;
 
118
        }
 
119
        
 
120
        // Run only at the execution root
 
121
        if ( runOnlyAtExecutionRoot && !isThisTheExecutionRoot() )
 
122
        {
 
123
            getLog().info( "Skip attaching artifacts in this project because it's not the Execution Root" );
 
124
        }
 
125
        else
 
126
        {
 
127
            this.validateArtifacts();
 
128
 
 
129
            for ( Artifact artifact : artifacts )
 
130
            {
 
131
                projectHelper.attachArtifact( this.project,
 
132
                                              artifact.getType(),
 
133
                                              artifact.getClassifier(),
 
134
                                              artifact.getFile() );
 
135
            }
 
136
        }
 
137
 
 
138
    }
 
139
 
 
140
    /**
 
141
     * Returns <code>true</code> if the current project is located at the
 
142
     * Execution Root Directory (where mvn was launched).
 
143
     *
 
144
     * @return <code>true</code> if the current project is at the Execution Root
 
145
     */
 
146
    private boolean isThisTheExecutionRoot()
 
147
    {
 
148
        if( getLog().isDebugEnabled() )
 
149
        {
 
150
            getLog().debug( "Root Folder:" + mavenSession.getExecutionRootDirectory() );
 
151
            getLog().debug( "Current Folder:" + basedir );
 
152
            
 
153
        }
 
154
        boolean result = mavenSession.getExecutionRootDirectory().equalsIgnoreCase( basedir.toString() );
 
155
        if( getLog().isDebugEnabled() )
 
156
        {
 
157
            if ( result )
 
158
            {
 
159
                getLog().debug( "This is the execution root." );
 
160
            }
 
161
            else
 
162
            {
 
163
                getLog().debug( "This is NOT the execution root." );
 
164
            }
 
165
        }
 
166
        return result;
 
167
    }
 
168
 
 
169
    private void validateArtifacts()
 
170
        throws MojoFailureException
 
171
    {
 
172
        // check unique of types and classifiers
 
173
        Set<String> extensionClassifiers = new HashSet<String>();
 
174
        for ( Artifact artifact : artifacts )
 
175
        {
 
176
            String extensionClassifier = artifact.getType() + ":" + artifact.getClassifier();
 
177
 
 
178
            if ( !extensionClassifiers.add( extensionClassifier  ) )
 
179
            {
 
180
                throw new MojoFailureException( "The artifact with same type and classifier: "
 
181
                                                + extensionClassifier + " is used more than once." );
 
182
            }
 
183
 
 
184
        }
 
185
    }
 
186
}