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.plugin.antrun.AntRunMojo;
23  import org.apache.maven.project.MavenProject;
24  import org.apache.maven.project.MavenProjectHelper;
25  import org.apache.tools.ant.BuildException;
26  import org.apache.tools.ant.Project;
27  import org.apache.tools.ant.Task;
28  import org.codehaus.plexus.util.FileUtils;
29  
30  import java.io.File;
31  
32  /**
33   * 
34   */
35  public class AttachArtifactTask
36      extends Task
37  {
38  
39      /**
40       * The refId of the Maven project.
41       */
42      private String mavenProjectRefId = AntRunMojo.DEFAULT_MAVEN_PROJECT_REFID;
43  
44      /**
45       * The refId of the Maven project helper component.
46       */
47      private String mavenProjectHelperRefId = AntRunMojo.DEFAULT_MAVEN_PROJECT_HELPER_REFID;
48  
49      /**
50       * The file to attach.
51       */
52      private File file;
53  
54      /**
55       * The classifier of the artifact to attach
56       */
57      private String classifier;
58  
59      /**
60       * The type of the artifact to attach.  Defaults to file extension.
61       */
62      private String type;
63  
64      /** {@inheritDoc} */
65      public void execute()
66      {
67          if ( file == null )
68          {
69              throw new BuildException( "File is a required parameter." );
70          }
71  
72          if ( !file.exists() )
73          {
74              throw new BuildException( "File does not exist: " + file );
75          }
76  
77          if ( this.getProject().getReference( mavenProjectRefId ) == null )
78          {
79              throw new BuildException( "Maven project reference not found: " + mavenProjectRefId );
80          }
81  
82          if ( type == null )
83          {
84              type = FileUtils.getExtension( file.getName() );
85          }
86  
87          MavenProject mavenProject = (MavenProject) this.getProject().getReference( mavenProjectRefId );
88  
89          if ( this.getProject().getReference( mavenProjectHelperRefId ) == null )
90          {
91              throw new BuildException( "Maven project helper reference not found: " + mavenProjectHelperRefId );
92          }
93  
94          log( "Attaching " + file + " as an attached artifact", Project.MSG_VERBOSE );
95          MavenProjectHelper projectHelper = (MavenProjectHelper) getProject().getReference( mavenProjectHelperRefId );
96          projectHelper.attachArtifact( mavenProject, type, classifier, file );
97      }
98  
99      /**
100      * @return {@link #file}
101      */
102     public File getFile()
103     {
104         return file;
105     }
106 
107     /**
108      * @param file {@link #file}
109      */
110     public void setFile( File file )
111     {
112         this.file = file;
113     }
114 
115     /**
116      * @return {@link #mavenProjectRefId}
117      */
118     public String getMavenProjectRefId()
119     {
120         return mavenProjectRefId;
121     }
122 
123     /**
124      * @param mavenProjectRefId {@link #mavenProjectRefId}
125      */
126     public void setMavenProjectRefId( String mavenProjectRefId )
127     {
128         this.mavenProjectRefId = mavenProjectRefId;
129     }
130 
131     /**
132      * @return {@link #classifier}
133      */
134     public String getClassifier()
135     {
136         return classifier;
137     }
138 
139     /**
140      * @param classifier {@link #classifier}
141      */
142     public void setClassifier( String classifier )
143     {
144         this.classifier = classifier;
145     }
146 
147     /**
148      * @return {@link #type}
149      */
150     public String getType()
151     {
152         return type;
153     }
154 
155     /**
156      * @param type {@link #type}
157      */
158     public void setType( String type )
159     {
160         this.type = type;
161     }
162 }