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