View Javadoc
1   /*
2    * The MIT License
3    * Copyright © 2004-2014 Fabrizio Giustina
4    * Copyright © 2022-2022 Web-Legacy
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy
7    * of this software and associated documentation files (the "Software"), to deal
8    * in the Software without restriction, including without limitation the rights
9    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10   * copies of the Software, and to permit persons to whom the Software is
11   * furnished to do so, subject to the following conditions:
12   *
13   * The above copyright notice and this permission notice shall be included in
14   * all copies or substantial portions of the Software.
15   *
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22   * THE SOFTWARE.
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   * Generates a jar containing the tlddoc generated documentation. The generated jar is installed/deployed to the
44   * repository if install/deploy is executed after this goal.
45   */
46  @Mojo(name="taglibdocjar", defaultPhase=LifecyclePhase.PACKAGE)
47  @Execute(goal="taglibdoc")
48  public class TaglibdocJar
49      extends AbstractMojo
50  {
51  
52      /**
53       * TldDoc output dir.
54       */
55      @Parameter(defaultValue="${project.reporting.outputDirectory}/tlddoc")
56      private File tldDocDir;
57  
58      /**
59       * Maven project.
60       */
61      @Parameter(property="project", readonly=true, required=true)
62      private MavenProject project;
63  
64      /**
65       * Maven Project Helper.
66       */
67      @Component
68      private MavenProjectHelper projectHelper;
69  
70      /**
71       * Generated jar name/location.
72       */
73      @Parameter(defaultValue="${project.build.directory}/${project.build.finalName}-tlddoc.jar", required=true)
74      private File tlddocJar;
75  
76      /**
77       * Attach the generated jar to the main artifact. Set this to false if you don't want the taglibdoc jar deployed to
78       * the repository.
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 }