1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
40
41 private String mavenProjectRefId = AntRunMojo.DEFAULT_MAVEN_PROJECT_REF_REFID;
42
43
44
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
86
87 public String getMavenProjectRefId() {
88 return mavenProjectRefId;
89 }
90
91
92
93
94 public void setMavenProjectRefId(String mavenProjectRefId) {
95 this.mavenProjectRefId = mavenProjectRefId;
96 }
97
98
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 }