View Javadoc
1   package org.apache.maven.ant.tasks;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.plugins.antrun.AntRunMojo;
23  import org.apache.maven.plugins.antrun.MavenAntRunProject;
24  import org.apache.maven.project.MavenProject;
25  import org.apache.maven.project.MavenProjectHelper;
26  import org.apache.tools.ant.BuildException;
27  import org.apache.tools.ant.Project;
28  import org.apache.tools.ant.Task;
29  import org.codehaus.plexus.util.FileUtils;
30  
31  import java.io.File;
32  
33  /**
34   * 
35   */
36  public class AttachArtifactTask
37      extends Task
38  {
39  
40      /**
41       * The refId of the Maven project.
42       */
43      private String mavenProjectRefId = AntRunMojo.DEFAULT_MAVEN_PROJECT_REF_REFID;
44  
45      /**
46       * The refId of the Maven project helper component.
47       */
48      @SuppressWarnings( "FieldCanBeLocal" )
49      private String mavenProjectHelperRefId = AntRunMojo.DEFAULT_MAVEN_PROJECT_HELPER_REFID;
50  
51      /**
52       * The file to attach.
53       */
54      private File file;
55  
56      /**
57       * The classifier of the artifact to attach
58       */
59      private String classifier;
60  
61      /**
62       * The type of the artifact to attach.  Defaults to file extension.
63       */
64      private String type;
65  
66      /** {@inheritDoc} */
67      @Override
68      public void execute()
69      {
70          if ( file == null )
71          {
72              throw new BuildException( "File is a required parameter." );
73          }
74  
75          if ( !file.exists() )
76          {
77              throw new BuildException( "File does not exist: " + file );
78          }
79  
80          if ( this.getProject().getReference( mavenProjectRefId ) == null )
81          {
82              throw new BuildException( "Maven project reference not found: " + mavenProjectRefId );
83          }
84  
85          if ( type == null )
86          {
87              type = FileUtils.getExtension( file.getName() );
88          }
89  
90          MavenProject mavenProject =
91              ( (MavenAntRunProject) this.getProject().getReference( mavenProjectRefId ) ).getMavenProject();
92  
93          if ( this.getProject().getReference( mavenProjectHelperRefId ) == null )
94          {
95              throw new BuildException( "Maven project helper reference not found: " + mavenProjectHelperRefId );
96          }
97  
98          log( "Attaching " + file + " as an attached artifact", Project.MSG_VERBOSE );
99          MavenProjectHelper projectHelper = getProject().getReference( mavenProjectHelperRefId );
100         projectHelper.attachArtifact( mavenProject, type, classifier, file );
101     }
102 
103     /**
104      * @return {@link #file}
105      */
106     public File getFile()
107     {
108         return file;
109     }
110 
111     /**
112      * @param file {@link #file}
113      */
114     public void setFile( File file )
115     {
116         this.file = file;
117     }
118 
119     /**
120      * @return {@link #mavenProjectRefId}
121      */
122     public String getMavenProjectRefId()
123     {
124         return mavenProjectRefId;
125     }
126 
127     /**
128      * @param mavenProjectRefId {@link #mavenProjectRefId}
129      */
130     public void setMavenProjectRefId( String mavenProjectRefId )
131     {
132         this.mavenProjectRefId = mavenProjectRefId;
133     }
134 
135     /**
136      * @return {@link #classifier}
137      */
138     public String getClassifier()
139     {
140         return classifier;
141     }
142 
143     /**
144      * @param classifier {@link #classifier}
145      */
146     public void setClassifier( String classifier )
147     {
148         this.classifier = classifier;
149     }
150 
151     /**
152      * @return {@link #type}
153      */
154     public String getType()
155     {
156         return type;
157     }
158 
159     /**
160      * @param type {@link #type}
161      */
162     public void setType( String type )
163     {
164         this.type = type;
165     }
166 }