| 1 | |
package no.sesat.mojo; |
| 2 | |
|
| 3 | |
import java.io.File; |
| 4 | |
import java.io.IOException; |
| 5 | |
import java.io.InputStream; |
| 6 | |
import java.io.PrintStream; |
| 7 | |
import java.util.Enumeration; |
| 8 | |
import java.util.Iterator; |
| 9 | |
import java.util.List; |
| 10 | |
import java.util.Map; |
| 11 | |
import java.util.jar.JarEntry; |
| 12 | |
import java.util.jar.JarFile; |
| 13 | |
|
| 14 | |
import no.sesat.mojo.modes.Builder; |
| 15 | |
|
| 16 | |
import org.apache.maven.artifact.Artifact; |
| 17 | |
import org.apache.maven.artifact.factory.ArtifactFactory; |
| 18 | |
import org.apache.maven.artifact.resolver.ArtifactNotFoundException; |
| 19 | |
import org.apache.maven.artifact.resolver.ArtifactResolutionException; |
| 20 | |
import org.apache.maven.artifact.resolver.ArtifactResolver; |
| 21 | |
import org.apache.maven.plugin.AbstractMojo; |
| 22 | |
import org.apache.maven.plugin.MojoExecutionException; |
| 23 | |
import org.apache.maven.project.MavenProject; |
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | 0 | public class SearchModesSchemaGenerator extends AbstractMojo { |
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
private MavenProject project; |
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
private List<String> classpaths; |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
private List<String> sourceArtifacts; |
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
private String outputDir; |
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
private ArtifactFactory factory; |
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
private ArtifactResolver resolver; |
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
private org.apache.maven.artifact.repository.ArtifactRepository local; |
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
private java.util.List remoteRepos; |
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
public void execute() throws MojoExecutionException { |
| 101 | 0 | getLog().info(this.getClass().getName()); |
| 102 | |
|
| 103 | 0 | if (outputDir == null) { |
| 104 | 0 | getLog().error("outputDir variable must be specified"); |
| 105 | |
} |
| 106 | |
|
| 107 | 0 | String classpath = ""; |
| 108 | 0 | if (classpaths != null) { |
| 109 | 0 | for (final Iterator<String> iterator = classpaths.iterator(); iterator.hasNext();) { |
| 110 | 0 | final String name = iterator.next(); |
| 111 | 0 | File file = new File(name); |
| 112 | 0 | if (!file.isAbsolute()) { |
| 113 | 0 | file = new File(project.getBasedir(), name); |
| 114 | |
} |
| 115 | 0 | if (file.exists()) { |
| 116 | |
try { |
| 117 | 0 | classpath += file.getCanonicalPath() + File.separator; |
| 118 | 0 | } catch (IOException e) { |
| 119 | 0 | getLog().warn(e); |
| 120 | 0 | } |
| 121 | 0 | if (iterator.hasNext()) { |
| 122 | 0 | classpath += File.pathSeparator; |
| 123 | |
} |
| 124 | |
} else { |
| 125 | 0 | getLog().warn("Classpath not found : " + file.getAbsolutePath()); |
| 126 | |
} |
| 127 | 0 | } |
| 128 | |
} |
| 129 | |
|
| 130 | 0 | if (sourceArtifacts != null) { |
| 131 | 0 | Map<String, Artifact> artifactMap = project.getArtifactMap(); |
| 132 | 0 | for (String artifactName : sourceArtifacts) { |
| 133 | 0 | String[] ap = artifactName.split(":"); |
| 134 | |
|
| 135 | 0 | Artifact a = factory.createArtifactWithClassifier(ap[0], ap[1], ap[2], "jar", "sources"); |
| 136 | |
|
| 137 | |
try { |
| 138 | 0 | resolver.resolve(a, remoteRepos, local); |
| 139 | 0 | } catch (ArtifactResolutionException e) { |
| 140 | 0 | e.printStackTrace(); |
| 141 | 0 | } catch (ArtifactNotFoundException e) { |
| 142 | 0 | e.printStackTrace(); |
| 143 | 0 | } |
| 144 | |
|
| 145 | 0 | File outFolder = new File("target/source/"); |
| 146 | 0 | outFolder.mkdirs(); |
| 147 | 0 | if (!classpath.equals("")) { |
| 148 | 0 | classpath += File.pathSeparator; |
| 149 | |
} |
| 150 | 0 | classpath += outFolder.getAbsolutePath(); |
| 151 | |
|
| 152 | |
try { |
| 153 | 0 | JarFile jarFile = new JarFile(a.getFile()); |
| 154 | |
|
| 155 | 0 | for (Enumeration<JarEntry> e = jarFile.entries(); e.hasMoreElements();) { |
| 156 | 0 | JarEntry entry = (JarEntry) e.nextElement(); |
| 157 | 0 | File file = new File(outFolder, entry.getName()); |
| 158 | 0 | if (entry.isDirectory()) { |
| 159 | 0 | file.mkdir(); |
| 160 | |
} else { |
| 161 | 0 | InputStream in = jarFile.getInputStream(entry); |
| 162 | 0 | PrintStream out = new PrintStream(file); |
| 163 | 0 | byte[] buf = new byte[1024]; |
| 164 | |
int len; |
| 165 | 0 | while ((len = in.read(buf)) > 0) { |
| 166 | 0 | out.write(buf, 0, len); |
| 167 | |
} |
| 168 | |
} |
| 169 | 0 | } |
| 170 | 0 | } catch (IOException e1) { |
| 171 | 0 | e1.printStackTrace(); |
| 172 | 0 | } |
| 173 | 0 | } |
| 174 | |
} |
| 175 | |
|
| 176 | 0 | File outputDirFile = new File(outputDir); |
| 177 | 0 | if (!outputDirFile.isAbsolute()) { |
| 178 | 0 | outputDirFile = new File(project.getBasedir(), outputDir); |
| 179 | |
} |
| 180 | |
|
| 181 | 0 | outputDir = outputDirFile.getAbsolutePath(); |
| 182 | |
|
| 183 | 0 | getLog().info("Using: classpath = " + classpath); |
| 184 | 0 | getLog().info("Using: outputDir = " + outputDir); |
| 185 | |
|
| 186 | 0 | Builder.build(classpath, outputDir, project.getName()); |
| 187 | 0 | } |
| 188 | |
} |