1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package net.sf.maventaglib;
25
26 import java.io.File;
27 import java.io.IOException;
28
29 import org.apache.maven.artifact.handler.ArtifactHandler;
30 import org.apache.maven.plugin.AbstractMojo;
31 import org.apache.maven.plugin.MojoExecutionException;
32 import org.apache.maven.plugins.annotations.Component;
33 import org.apache.maven.plugins.annotations.Execute;
34 import org.apache.maven.plugins.annotations.LifecyclePhase;
35 import org.apache.maven.plugins.annotations.Mojo;
36 import org.apache.maven.plugins.annotations.Parameter;
37 import org.apache.maven.project.MavenProject;
38 import org.apache.maven.project.MavenProjectHelper;
39 import org.codehaus.plexus.archiver.ArchiverException;
40 import org.codehaus.plexus.archiver.jar.JarArchiver;
41
42
43
44
45
46 @Mojo(name="taglibdocjar", defaultPhase=LifecyclePhase.PACKAGE)
47 @Execute(goal="taglibdoc")
48 public class TaglibdocJar
49 extends AbstractMojo
50 {
51
52
53
54
55 @Parameter(defaultValue="${project.reporting.outputDirectory}/tlddoc")
56 private File tldDocDir;
57
58
59
60
61 @Parameter(property="project", readonly=true, required=true)
62 private MavenProject project;
63
64
65
66
67 @Component
68 private MavenProjectHelper projectHelper;
69
70
71
72
73 @Parameter(defaultValue="${project.build.directory}/${project.build.finalName}-tlddoc.jar", required=true)
74 private File tlddocJar;
75
76
77
78
79
80 @Parameter(property="attach", defaultValue="true")
81 private boolean attach = true;
82
83 public void execute()
84 throws MojoExecutionException
85 {
86 ArtifactHandler artifactHandler = project.getArtifact().getArtifactHandler();
87 if ( !"java".equals( artifactHandler.getLanguage() ) )
88 {
89 getLog().info( "Not executing tlddoc as the project is not a Java classpath-capable package" );
90 return;
91 }
92
93 try
94 {
95 File outputFile = generateArchive( tldDocDir, tlddocJar );
96
97 if ( !attach )
98 {
99 getLog().info( "NOT adding tlddoc to attached artifacts list." );
100 }
101 else
102 {
103 projectHelper.attachArtifact( project, "jar", "tlddoc", outputFile );
104 }
105 }
106 catch ( ArchiverException e )
107 {
108 throw new MojoExecutionException( "Error while creating archive.", e );
109 }
110 catch ( IOException e )
111 {
112 throw new MojoExecutionException( "Error while creating archive.", e );
113 }
114 }
115
116 private File generateArchive( File tdldocDir, File tlddocJar )
117 throws MojoExecutionException, ArchiverException, IOException
118 {
119
120 if ( !tdldocDir.exists() )
121 {
122 throw new MojoExecutionException( "tlddoc files not found." );
123 }
124
125 if ( tlddocJar.exists() && !tlddocJar.delete())
126 {
127 throw new MojoExecutionException( "tlddocJar could not deleted." );
128 }
129
130 JarArchiver archiver = new JarArchiver();
131
132 archiver.addDirectory( tdldocDir );
133 archiver.setDestFile( tlddocJar );
134 archiver.createArchive();
135
136 return tlddocJar;
137 }
138
139 }