1 17 package org.apache.servicemix.maven.plugin.jbi; 18 19 import java.io.File ; 20 import java.util.Collections ; 21 import java.util.HashMap ; 22 import java.util.HashSet ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.Map ; 26 import java.util.Set ; 27 28 import org.apache.maven.artifact.Artifact; 29 import org.apache.maven.artifact.factory.ArtifactFactory; 30 import org.apache.maven.artifact.metadata.ArtifactMetadataSource; 31 import org.apache.maven.artifact.repository.ArtifactRepository; 32 import org.apache.maven.artifact.resolver.ArtifactCollector; 33 import org.apache.maven.artifact.resolver.ArtifactResolutionException; 34 import org.apache.maven.artifact.resolver.ArtifactResolver; 35 import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; 36 import org.apache.maven.artifact.versioning.VersionRange; 37 import org.apache.maven.model.Dependency; 38 import org.apache.maven.model.DependencyManagement; 39 import org.apache.maven.plugin.AbstractMojo; 40 import org.apache.maven.project.MavenProject; 41 import org.apache.maven.project.MavenProjectBuilder; 42 import org.apache.maven.project.MavenProjectHelper; 43 import org.apache.maven.project.ProjectBuildingException; 44 import org.apache.servicemix.maven.plugin.jbi.JbiResolutionListener.Node; 45 46 public abstract class AbstractJbiMojo extends AbstractMojo { 47 48 public static final String META_INF = "META-INF"; 49 50 public static final String JBI_DESCRIPTOR = "jbi.xml"; 51 52 public static final String LIB_DIRECTORY = "lib"; 53 54 59 protected MavenProjectHelper projectHelper; 60 61 68 protected MavenProject project; 69 70 76 protected File workDirectory; 77 78 81 protected MavenProjectBuilder projectBuilder; 82 83 86 protected ArtifactRepository localRepo; 87 88 91 protected List remoteRepos; 92 93 96 protected ArtifactMetadataSource artifactMetadataSource; 97 98 101 protected ArtifactResolver resolver; 102 103 106 protected ArtifactCollector collector; 107 108 111 protected ArtifactFactory factory; 112 113 protected MavenProject getProject() { 114 return project; 115 } 116 117 protected File getWorkDirectory() { 118 return workDirectory; 119 } 120 121 public MavenProjectHelper getProjectHelper() { 122 return projectHelper; 123 } 124 125 protected void removeBranch(JbiResolutionListener listener, 126 Artifact artifact) { 127 Node n = listener.getNode(artifact); 128 if (n != null && n.getParent() != null) { 129 n.getParent().getChildren().remove(n); 130 } 131 } 132 133 protected void removeChildren(JbiResolutionListener listener, 134 Artifact artifact) { 135 Node n = listener.getNode(artifact); 136 n.getChildren().clear(); 137 } 138 139 protected Set getArtifacts(Node n, Set s) { 140 s.add(n.getArtifact()); 141 for (Iterator iter = n.getChildren().iterator(); iter.hasNext();) { 142 Node c = (Node) iter.next(); 143 getArtifacts(c, s); 144 } 145 return s; 146 } 147 148 protected void excludeBranch(Node n, Set excludes) { 149 excludes.add(n); 150 for (Iterator iter = n.getChildren().iterator(); iter.hasNext();) { 151 Node c = (Node) iter.next(); 152 excludeBranch(c, excludes); 153 } 154 } 155 156 protected void print(Node rootNode, String string) { 157 getLog().info(string + rootNode.getArtifact()); 158 for (Iterator iter = rootNode.getChildren().iterator(); iter.hasNext();) { 159 Node n = (Node) iter.next(); 160 print(n, string + " "); 161 } 162 } 163 164 protected Set retainArtifacts(Set includes, JbiResolutionListener listener) { 165 HashSet finalIncludes = new HashSet (); 166 Set filteredArtifacts = getArtifacts(listener.getRootNode(), 167 new HashSet ()); 168 for (Iterator iter = includes.iterator(); iter.hasNext();) { 169 Artifact artifact = (Artifact) iter.next(); 170 for (Iterator iter2 = filteredArtifacts.iterator(); iter2.hasNext();) { 171 Artifact filteredArtifact = (Artifact) iter2.next(); 172 if (filteredArtifact.getArtifactId().equals( 173 artifact.getArtifactId()) 174 && filteredArtifact.getType() 175 .equals(artifact.getType()) 176 && filteredArtifact.getGroupId().equals( 177 artifact.getGroupId())) { 178 if (!filteredArtifact.getVersion().equals( 179 artifact.getVersion())) { 180 getLog() 181 .warn( 182 "Resolved artifact " 183 + artifact 184 + " has a different version from that in dependency management " 185 + filteredArtifact 186 + ", overriding dependency management"); 187 } 188 finalIncludes.add(artifact); 189 } 190 } 191 192 } 193 194 return finalIncludes; 195 } 196 197 protected JbiResolutionListener resolveProject() { 198 Map managedVersions = null; 199 try { 200 managedVersions = createManagedVersionMap(project.getId(), project 201 .getDependencyManagement()); 202 } catch (ProjectBuildingException e) { 203 getLog().error( 204 "An error occurred while resolving project dependencies.", 205 e); 206 } 207 JbiResolutionListener listener = new JbiResolutionListener(); 208 try { 209 collector.collect(project.getDependencyArtifacts(), project 210 .getArtifact(), managedVersions, localRepo, remoteRepos, 211 artifactMetadataSource, null, Collections 212 .singletonList(listener)); 213 } catch (ArtifactResolutionException e) { 214 getLog().error( 215 "An error occurred while resolving project dependencies.", 216 e); 217 } 218 return listener; 219 } 220 221 protected Map createManagedVersionMap(String projectId, 222 DependencyManagement dependencyManagement) 223 throws ProjectBuildingException { 224 Map map; 225 if (dependencyManagement != null 226 && dependencyManagement.getDependencies() != null) { 227 map = new HashMap (); 228 for (Iterator i = dependencyManagement.getDependencies().iterator(); i 229 .hasNext();) { 230 Dependency d = (Dependency) i.next(); 231 232 try { 233 VersionRange versionRange = VersionRange 234 .createFromVersionSpec(d.getVersion()); 235 Artifact artifact = factory.createDependencyArtifact(d 236 .getGroupId(), d.getArtifactId(), versionRange, d 237 .getType(), d.getClassifier(), d.getScope()); 238 map.put(d.getManagementKey(), artifact); 239 } catch (InvalidVersionSpecificationException e) { 240 throw new ProjectBuildingException(projectId, 241 "Unable to parse version '" + d.getVersion() 242 + "' for dependency '" 243 + d.getManagementKey() + "': " 244 + e.getMessage(), e); 245 } 246 } 247 } else { 248 map = Collections.EMPTY_MAP; 249 } 250 return map; 251 } 252 253 } 254 | Popular Tags |