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