Coverage Report - no.sesat.mojo.SearchModesSchemaGenerator
 
Classes in this File Line Coverage Branch Coverage Complexity
SearchModesSchemaGenerator
0%
0/60
0%
0/26
0
 
 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  
  * @goal searchModesSchemaGenerator
 27  
  */
 28  0
 public class SearchModesSchemaGenerator extends AbstractMojo {
 29  
 
 30  
     /**
 31  
      * The Maven project.
 32  
      * 
 33  
      * @parameter expression="${project}"
 34  
      * @required
 35  
      * @readonly
 36  
      * @description "the maven project to use"
 37  
      */
 38  
     private MavenProject project;
 39  
 
 40  
     /**
 41  
      * Classpath
 42  
      * 
 43  
      * @parameter
 44  
      */
 45  
     private List<String> classpaths;
 46  
 
 47  
     /**
 48  
      * sourceArtifacts
 49  
      * 
 50  
      * @parameter
 51  
      */
 52  
     private List<String> sourceArtifacts;
 53  
 
 54  
     /**
 55  
      * Output directory
 56  
      * 
 57  
      * @parameter
 58  
      */
 59  
     private String outputDir;
 60  
 
 61  
     /**
 62  
      * Used to look up Artifacts in the remote serverDeployLocation.
 63  
      * 
 64  
      * @parameter expression="${component.org.apache.maven.artifact.factory.ArtifactFactory}"
 65  
      * @required
 66  
      * @readonly
 67  
      */
 68  
     private ArtifactFactory factory;
 69  
 
 70  
     /**
 71  
      * Used to look up Artifacts in the remote serverDeployLocation.
 72  
      * 
 73  
      * @parameter expression="${component.org.apache.maven.artifact.resolver.ArtifactResolver}"
 74  
      * @required
 75  
      * @readonly
 76  
      */
 77  
     private ArtifactResolver resolver;
 78  
 
 79  
     /**
 80  
      * Location of the local serverDeployLocation.
 81  
      * 
 82  
      * @parameter expression="${localRepository}"
 83  
      * @readonly
 84  
      * @required
 85  
      */
 86  
     private org.apache.maven.artifact.repository.ArtifactRepository local;
 87  
 
 88  
     /**
 89  
      * List of Remote Repositories used by the resolver
 90  
      * 
 91  
      * @parameter expression="${project.remoteArtifactRepositories}"
 92  
      * @readonly
 93  
      * @required
 94  
      */
 95  
     private java.util.List remoteRepos;
 96  
 
 97  
     /**
 98  
      * @see org.apache.maven.plugin.Mojo#execute()
 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  
 }