1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  package org.codehaus.gmavenplus.mojo;
18  
19  import org.apache.maven.archiver.MavenArchiveConfiguration;
20  import org.apache.maven.archiver.MavenArchiver;
21  import org.apache.maven.artifact.DependencyResolutionRequiredException;
22  import org.apache.maven.plugin.MojoExecutionException;
23  import org.apache.maven.plugins.annotations.Component;
24  import org.apache.maven.plugins.annotations.LifecyclePhase;
25  import org.apache.maven.plugins.annotations.Mojo;
26  import org.apache.maven.plugins.annotations.Parameter;
27  import org.apache.maven.plugins.annotations.ResolutionScope;
28  import org.apache.maven.project.MavenProjectHelper;
29  import org.codehaus.plexus.archiver.Archiver;
30  import org.codehaus.plexus.archiver.ArchiverException;
31  import org.codehaus.plexus.archiver.jar.JarArchiver;
32  import org.codehaus.plexus.archiver.jar.ManifestException;
33  
34  import java.io.File;
35  import java.io.IOException;
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  @Mojo(name = "groovydoc-jar", defaultPhase = LifecyclePhase.PACKAGE, requiresDependencyResolution = ResolutionScope.COMPILE, threadSafe = true)
46  public class GroovyDocJarMojo extends GroovyDocMojo {
47  
48      
49  
50  
51  
52      @Parameter(property = "project.build.finalName")
53      protected String finalName;
54  
55      
56  
57  
58      @Parameter(property = "project.build.directory")
59      protected String jarOutputDirectory;
60  
61      
62  
63  
64      @Component(role = Archiver.class, hint = "jar")
65      protected JarArchiver jarArchiver;
66  
67      
68  
69  
70  
71      @Parameter
72      protected final MavenArchiveConfiguration archive = new MavenArchiveConfiguration();
73  
74      
75  
76  
77      @Parameter(property = "attach", defaultValue = "true")
78      protected boolean attach;
79  
80      
81  
82  
83      @Component
84      private MavenProjectHelper projectHelper;
85  
86      
87  
88  
89  
90      @Parameter(defaultValue = "${project.build.outputDirectory}/META-INF/MANIFEST.MF", required = true, readonly = true)
91      protected File defaultManifestFile;
92  
93      
94  
95  
96      @Parameter(defaultValue = "false")
97      private boolean useDefaultManifestFile;
98  
99      
100 
101 
102     @Parameter(defaultValue = "groovydoc")
103     private String classifier;
104 
105     
106 
107 
108 
109 
110     @Parameter(defaultValue = "javadoc")
111     private String artifactType;
112 
113     
114 
115 
116     @Parameter(defaultValue = "true")
117     protected boolean invokeGroovyDoc;
118 
119     
120 
121 
122     @Override
123     public void execute() throws MojoExecutionException {
124         if (invokeGroovyDoc) {
125             
126             super.execute();
127         }
128 
129         try {
130             File outputFile = generateArchive(groovyDocOutputDirectory, finalName + "-" + classifier + ".jar");
131 
132             if (attach) {
133                 projectHelper.attachArtifact(project, artifactType, classifier, outputFile);
134             } else {
135                 getLog().info("Not adding GroovyDoc jar to attached artifacts list.");
136             }
137         } catch (ArchiverException e) {
138             throw new MojoExecutionException("ArchiverException: Error while creating archive", e);
139         } catch (IOException e) {
140             throw new MojoExecutionException("IOException: Error while creating archive", e);
141         } catch (RuntimeException e) {
142             throw new MojoExecutionException("RuntimeException: Error while creating archive", e);
143         }
144     }
145 
146     
147 
148 
149 
150 
151 
152 
153 
154 
155     protected File generateArchive(File groovydocFiles, String jarFileName) throws ArchiverException, IOException {
156         File groovydocJar = new File(jarOutputDirectory, jarFileName);
157 
158         if (groovydocJar.exists()) {
159             if (!groovydocJar.delete()) {
160                 getLog().warn("Unable to delete " + groovydocJar.getAbsolutePath());
161             }
162         }
163 
164         MavenArchiver archiver = new MavenArchiver();
165         archiver.setArchiver(jarArchiver);
166         archiver.setOutputFile(groovydocJar);
167 
168         if (!groovydocFiles.exists()) {
169             getLog().warn("JAR will be empty - no content was marked for inclusion!");
170         } else {
171             archiver.getArchiver().addDirectory(groovydocFiles);
172         }
173 
174         if (useDefaultManifestFile && defaultManifestFile.exists() && archive.getManifestFile() == null) {
175             getLog().info("Adding existing MANIFEST to archive. Found under: " + defaultManifestFile.getPath());
176             archive.setManifestFile(defaultManifestFile);
177         }
178 
179         try {
180             archiver.createArchive(session, project, archive);
181         } catch (ManifestException e) {
182             throw new ArchiverException("ManifestException: " + e.getMessage(), e);
183         } catch (DependencyResolutionRequiredException e) {
184             throw new ArchiverException("DependencyResolutionRequiredException: " + e.getMessage(), e);
185         }
186 
187         return groovydocJar;
188     }
189 
190 }