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