View Javadoc

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  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         getLog().info(this.getClass().getName());
102 
103         if (outputDir == null) {
104             getLog().error("outputDir variable must be specified");
105         }
106 
107         String classpath = "";
108         if (classpaths != null) {
109             for (final Iterator<String> iterator = classpaths.iterator(); iterator.hasNext();) {
110                 final String name = iterator.next();
111                 File file = new File(name);
112                 if (!file.isAbsolute()) {
113                     file = new File(project.getBasedir(), name);
114                 }
115                 if (file.exists()) {
116                     try {
117                         classpath += file.getCanonicalPath() + File.separator;
118                     } catch (IOException e) {
119                         getLog().warn(e);
120                     }
121                     if (iterator.hasNext()) {
122                         classpath += File.pathSeparator;
123                     }
124                 } else {
125                     getLog().warn("Classpath not found : " + file.getAbsolutePath());
126                 }
127             }
128         }
129 
130         if (sourceArtifacts != null) {
131             Map<String, Artifact> artifactMap = project.getArtifactMap();
132             for (String artifactName : sourceArtifacts) {
133                 String[] ap = artifactName.split(":");
134 
135                 Artifact a = factory.createArtifactWithClassifier(ap[0], ap[1], ap[2], "jar", "sources");
136 
137                 try {
138                     resolver.resolve(a, remoteRepos, local);
139                 } catch (ArtifactResolutionException e) {
140                     e.printStackTrace();
141                 } catch (ArtifactNotFoundException e) {
142                     e.printStackTrace();
143                 }
144 
145                 File outFolder = new File("target/source/");
146                 outFolder.mkdirs();
147                 if (!classpath.equals("")) {
148                     classpath += File.pathSeparator;
149                 }
150                 classpath += outFolder.getAbsolutePath();
151 
152                 try {
153                     JarFile jarFile = new JarFile(a.getFile());
154 
155                     for (Enumeration<JarEntry> e = jarFile.entries(); e.hasMoreElements();) {
156                         JarEntry entry = (JarEntry) e.nextElement();
157                         File file = new File(outFolder, entry.getName());
158                         if (entry.isDirectory()) {
159                             file.mkdir();
160                         } else {
161                             InputStream in = jarFile.getInputStream(entry);
162                             PrintStream out = new PrintStream(file);
163                             byte[] buf = new byte[1024];
164                             int len;
165                             while ((len = in.read(buf)) > 0) {
166                                 out.write(buf, 0, len);
167                             }
168                         }
169                     }
170                 } catch (IOException e1) {
171                     e1.printStackTrace();
172                 }
173             }
174         }
175 
176         File outputDirFile = new File(outputDir);
177         if (!outputDirFile.isAbsolute()) {
178             outputDirFile = new File(project.getBasedir(), outputDir);
179         }
180 
181         outputDir = outputDirFile.getAbsolutePath();
182 
183         getLog().info("Using: classpath = " + classpath);
184         getLog().info("Using: outputDir = " + outputDir);
185 
186         Builder.build(classpath, outputDir, project.getName());
187     }
188 }