1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.resolver.internal.ant.tasks;
20
21 import java.io.File;
22 import java.util.HashMap;
23 import java.util.Map;
24
25 import org.apache.maven.model.Model;
26 import org.apache.maven.resolver.internal.ant.AntRepoSys;
27 import org.apache.maven.resolver.internal.ant.types.Artifact;
28 import org.apache.maven.resolver.internal.ant.types.Artifacts;
29 import org.apache.maven.resolver.internal.ant.types.Pom;
30 import org.apache.tools.ant.BuildException;
31 import org.apache.tools.ant.Project;
32 import org.apache.tools.ant.Task;
33 import org.apache.tools.ant.types.Reference;
34
35
36
37 public abstract class AbstractDistTask extends Task {
38
39 private Pom pom;
40
41 private Artifacts artifacts;
42
43 protected void validate() {
44 getArtifacts().validate(this);
45
46 final Map<String, File> duplicates = new HashMap<>();
47 for (final Artifact artifact : getArtifacts().getArtifacts()) {
48 final String key = artifact.getType() + ':' + artifact.getClassifier();
49 if ("pom:".equals(key)) {
50 throw new BuildException(
51 "You must not specify an <artifact> with type=pom" + ", please use the <pom> element instead.");
52 } else if (duplicates.containsKey(key)) {
53 throw new BuildException("You must not specify two or more artifacts with the same type ("
54 + artifact.getType() + ") and classifier (" + artifact.getClassifier() + ")");
55 } else {
56 duplicates.put(key, artifact.getFile());
57 }
58
59 validateArtifactGav(artifact);
60 }
61
62 final Pom defaultPom = AntRepoSys.getInstance(getProject()).getDefaultPom();
63 if (pom == null && defaultPom != null) {
64 log("Using default POM (" + defaultPom.getCoords() + ")", Project.MSG_INFO);
65 pom = defaultPom;
66 }
67
68 if (pom == null) {
69 throw new BuildException(
70 "You must specify the <pom file=\"...\"> element" + " to denote the descriptor for the artifacts");
71 }
72 if (pom.getFile() == null) {
73 throw new BuildException("You must specify a <pom> element that has the 'file' attribute set");
74 }
75 }
76
77 private void validateArtifactGav(final Artifact artifact) {
78 final Pom artifactPom = artifact.getPom();
79 if (artifactPom != null) {
80 final String gid;
81 final String aid;
82 final String version;
83 if (artifactPom.getFile() != null) {
84 final Model model = artifactPom.getModel(this);
85 gid = model.getGroupId();
86 aid = model.getArtifactId();
87 version = model.getVersion();
88 } else {
89 gid = artifactPom.getGroupId();
90 aid = artifactPom.getArtifactId();
91 version = artifactPom.getVersion();
92 }
93
94 final Model model = getPom().getModel(this);
95
96 if (!(model.getGroupId().equals(gid)
97 && model.getArtifactId().equals(aid)
98 && model.getVersion().equals(version))) {
99 throw new BuildException(
100 "Artifact references different pom than it would be installed with: " + artifact.toString());
101 }
102 }
103 }
104
105 protected Artifacts getArtifacts() {
106 if (artifacts == null) {
107 artifacts = new Artifacts();
108 artifacts.setProject(getProject());
109 }
110 return artifacts;
111 }
112
113 public void addArtifact(final Artifact artifact) {
114 getArtifacts().addArtifact(artifact);
115 }
116
117 public void addArtifacts(final Artifacts artifacts) {
118 getArtifacts().addArtifacts(artifacts);
119 }
120
121 public void setArtifactsRef(final Reference ref) {
122 final Artifacts artifacts = new Artifacts();
123 artifacts.setProject(getProject());
124 artifacts.setRefid(ref);
125 getArtifacts().addArtifacts(artifacts);
126 }
127
128 protected Pom getPom() {
129 if (pom == null) {
130 return AntRepoSys.getInstance(getProject()).getDefaultPom();
131 }
132
133 return pom;
134 }
135
136 public void addPom(final Pom pom) {
137 if (this.pom != null) {
138 throw new BuildException("You must not specify multiple <pom> elements");
139 }
140 this.pom = pom;
141 }
142
143 public void setPomRef(final Reference ref) {
144 if (this.pom != null) {
145 throw new BuildException("You must not specify multiple <pom> elements");
146 }
147 pom = new Pom();
148 pom.setProject(getProject());
149 pom.setRefid(ref);
150 }
151 }