1 package hudson.maven; 2 3 import hudson.model.TaskListener; 4 import org.apache.maven.embedder.MavenEmbedderException; 5 import org.apache.maven.project.MavenProject; 6 import org.apache.maven.project.ProjectBuildingException; 7 8 import java.io.File ; 9 import java.io.IOException ; 10 import java.util.ArrayList ; 11 import java.util.List ; 12 import java.util.Map ; 13 import java.util.Enumeration ; 14 import java.net.URL ; 15 16 19 class MavenUtil { 20 26 public static MavenEmbedder createEmbedder(TaskListener listener) throws MavenEmbedderException { 27 MavenEmbedder maven = new MavenEmbedder(); 28 29 ClassLoader cl = MavenUtil.class.getClassLoader(); 30 maven.setClassLoader(new MaskingClassLoader(cl)); 31 maven.setLogger( new EmbedderLoggerImpl(listener) ); 32 33 maven.start(); 34 35 return maven; 36 } 37 38 50 public static void resolveModules(MavenEmbedder embedder, MavenProject project, String rel, Map <MavenProject,String > relativePathInfo) throws ProjectBuildingException { 51 52 File basedir = project.getFile().getParentFile(); 53 relativePathInfo.put(project,rel); 54 55 List <MavenProject> modules = new ArrayList <MavenProject>(); 56 57 for (String modulePath : (List <String >) project.getModules()) { 58 File moduleFile = new File (new File (basedir, modulePath),"pom.xml"); 59 60 String relativePath = rel; 61 if(relativePath.length()>0) relativePath+='/'; 62 relativePath+=modulePath; 63 64 MavenProject child = embedder.readProject(moduleFile); 65 resolveModules(embedder,child,relativePath,relativePathInfo); 66 modules.add(child); 67 } 68 69 project.setCollectedProjects(modules); 70 } 71 72 85 private static final class MaskingClassLoader extends ClassLoader { 86 87 public MaskingClassLoader(ClassLoader parent) { 88 super(parent); 89 } 90 91 public Enumeration <URL > getResources(String name) throws IOException { 92 final Enumeration <URL > e = super.getResources(name); 93 return new Enumeration <URL >() { 94 URL next; 95 96 public boolean hasMoreElements() { 97 fetch(); 98 return next!=null; 99 } 100 101 public URL nextElement() { 102 fetch(); 103 URL r = next; 104 next = null; 105 return r; 106 } 107 108 private void fetch() { 109 while(next==null && e.hasMoreElements()) { 110 next = e.nextElement(); 111 if(next.toExternalForm().contains("maven-plugin-tools-api")) 112 next = null; 113 } 114 } 115 }; 116 } 117 } 118 } 119 | Popular Tags |