File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\maven-plugin\src\main\java\hudson\maven\PomInfo.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.maven;
25
26 import org.apache.commons.lang.StringUtils;
27 import org.apache.maven.model.CiManagement;
28 import org.apache.maven.model.Dependency;
29 import org.apache.maven.model.Extension;
30 import org.apache.maven.model.Notifier;
31 import org.apache.maven.model.Plugin;
32 import org.apache.maven.model.ReportPlugin;
33 import org.apache.maven.project.MavenProject;
34
35 import java.io.Serializable;
36 import java.util.HashSet;
37 import java.util.List;
38 import java.util.Set;
39 import java.util.ArrayList;
40
41 /**
42 * Serializable representation of the key information obtained from Maven POM.
43 *
44 * <p>
45 * This is used for the master to introspect POM, which is only available
46 * as {@link MavenProject} object on slaves.
47 *
48 * @author Kohsuke Kawaguchi
49 */
50 final class PomInfo implements Serializable {
51
52 public static final String PACKAGING_TYPE_PLUGIN = "maven-plugin";
53
54 public final ModuleName name;
55
56 /**
57 * This is a human readable name of the POM. Not necessarily unique
58 * or file system safe.
59 *
60 * @see MavenProject#getName()
61 */
62 public final String displayName;
63
64 /**
65 * Relative path from the root directory of the root POM to
66 * the root directory of this module.
67 *
68 * Strings like "" (if this is the root), "abc", "foo/bar/zot".
69 */
70 public final String relativePath;
71
72 /**
73 * Version number taken from POM.
74 *
75 * @see MavenProject#getVersion()
76 */
77 public final String version;
78
79 /**
80 * Dependency of this project.
81 *
82 * See Maven's ProjectSorter class for the definition of the 'dependencies' in Maven.
83 */
84 public final Set<ModuleDependency> dependencies = new HashSet<ModuleDependency>();
85
86 /**
87 * Children of this module.
88 */
89 public final List<ModuleName> children = new ArrayList<ModuleName>();
90
91 /**
92 * The default goal specified in POM or null.
93 */
94 public final String defaultGoal;
95
96 /**
97 * Parent module.
98 */
99 public final PomInfo parent;
100
101 /**
102 * maven groupId
103 */
104 private final String groupId;
105
106 /**
107 * maven artifactId
108 */
109 private final String artifactId;
110
111 public final Notifier mailNotifier;
112
113 /**
114 * Packaging type taken from the POM.
115 *
116 * @since 1.425
117 */
118 public final String packaging;
119
120 public PomInfo(MavenProject project, PomInfo parent, String relPath) {
121 this.name = new ModuleName(project);
122 this.version = project.getVersion();
123 this.displayName = project.getName();
124 this.defaultGoal = project.getDefaultGoal();
125 this.relativePath = relPath;
126 this.parent = parent;
127 if(parent!=null)
128 parent.children.add(name);
129
130 for (Dependency dep : (List<Dependency>)project.getDependencies())
131 dependencies.add(new ModuleDependency(dep));
132
133 MavenProject parentProject = project.getParent();
134 if(parentProject!=null)
135 dependencies.add(new ModuleDependency(parentProject));
136 if(parent!=null)
137 dependencies.add(parent.asDependency());
138
139 addPluginsAsDependencies(project.getBuildPlugins(),dependencies);
140 addReportPluginsAsDependencies(project.getReportPlugins(),dependencies);
141
142 List<Extension> extensions = project.getBuildExtensions();
143 if(extensions!=null)
144 for (Extension ext : extensions)
145 dependencies.add(new ModuleDependency(ext));
146
147 // when the parent POM uses a plugin and builds a plugin at the same time,
148 // the plugin module ends up depending on itself
149 dependencies.remove(asDependency());
150
151 CiManagement ciMgmt = project.getCiManagement();
152 if ((ciMgmt != null) && (ciMgmt.getSystem()==null || ciMgmt.getSystem().equals("hudson"))) {
153 Notifier mailNotifier = null;
154 for (Notifier n : (List<Notifier>)ciMgmt.getNotifiers()) {
155 if (n.getType().equals("mail")) {
156 mailNotifier = n;
157 break;
158 }
159 }
160 this.mailNotifier = mailNotifier;
161 } else
162 this.mailNotifier = null;
163
164 this.groupId = project.getGroupId();
165 this.artifactId = project.getArtifactId();
166 this.packaging = project.getPackaging();
167 }
168
169 /**
170 * Creates {@link ModuleDependency} that represents this {@link PomInfo}.
171 */
172 private ModuleDependency asDependency() {
173 return new ModuleDependency(name,version,PACKAGING_TYPE_PLUGIN.equals(this.packaging));
174 }
175
176 private void addPluginsAsDependencies(List<Plugin> plugins, Set<ModuleDependency> dependencies) {
177 if(plugins==null) return;
178 for (Plugin p : plugins)
179 dependencies.add(new ModuleDependency(p));
180 }
181
182 private void addReportPluginsAsDependencies(List<ReportPlugin> plugins, Set<ModuleDependency> dependencies) {
183 if(plugins==null) return;
184 for (ReportPlugin p : plugins)
185 dependencies.add(new ModuleDependency(p));
186 }
187
188 /**
189 * Avoids dependency cycles.
190 *
191 * <p>
192 * People often write configuration in parent POMs that use the plugin
193 * which is a part of the build. To avoid this kind of dependency,
194 * make sure parent POMs don't depend on a child module.
195 */
196 /*package*/ void cutCycle() {
197 ModuleDependency dep = asDependency();
198 for(PomInfo p=parent; p!=null; p=p.parent) {
199 if(p.dependencies.contains(dep))
200 p.dependencies.remove(dep);
201 }
202 }
203
204 /**
205 * Computes the number of ancestors of this POM.
206 * returns 0 if this is the top-level module.
207 */
208 public int getNestLevel() {
209 int i=0;
210 for(PomInfo p=parent; p!=null; p=p.parent)
211 i++;
212 return i;
213 }
214
215 private static final long serialVersionUID = 1L;
216
217 @Override
218 public int hashCode()
219 {
220 int hash = 23 + this.groupId == null ? 1 : this.groupId.hashCode();
221 hash += this.artifactId == null ? 1 : this.artifactId.hashCode();
222 return hash;
223 }
224
225 @Override
226 public boolean equals( Object obj )
227 {
228 if (obj == null) {
229 return false;
230 }
231 if (obj == this) {
232 return true;
233 }
234 if (!(obj instanceof PomInfo)) {
235 return false;
236 }
237 PomInfo pomInfo = (PomInfo) obj;
238 return StringUtils.equals( pomInfo.groupId, this.groupId )
239 && StringUtils.equals( pomInfo.artifactId, this.artifactId );
240 }
241
242 /**
243 * Returns if groupId, artifactId and dependencies are the same.
244 */
245 public boolean isSimilar(ModuleName moduleName, Set<ModuleDependency> dependencies) {
246 return StringUtils.equals(this.groupId, moduleName.groupId)
247 && StringUtils.equals(this.artifactId, moduleName.artifactId)
248 && this.dependencies.equals(dependencies);
249 }
250
251 /**
252 * for debug purpose
253 */
254 public String toString() {
255 return "PomInfo:["+groupId+':'+artifactId+']'+"[relativePath:"+relativePath+']';
256 }
257 }
258