View Javadoc

1   /*
2    * Copyright 2007-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.maven.exceptions;
17  
18  import java.io.BufferedOutputStream;
19  import java.io.File;
20  import java.io.FileOutputStream;
21  import java.io.IOException;
22  import java.io.OutputStream;
23  import java.util.Properties;
24  
25  import org.apache.commons.io.IOUtils;
26  import org.apache.maven.archiver.MavenArchiveConfiguration;
27  import org.apache.maven.archiver.MavenArchiver;
28  import org.apache.maven.plugin.MojoExecutionException;
29  import org.apache.maven.plugin.MojoFailureException;
30  import org.apache.maven.project.DefaultMavenProjectHelper;
31  import org.apache.maven.project.MavenProjectHelper;
32  import org.codehaus.plexus.archiver.jar.JarArchiver;
33  
34  import de.smartics.maven.io.MojoIoUtils;
35  
36  /**
37   * Exports the SDoc documents.
38   *
39   * @goal export
40   * @phase package
41   * @requiresProject
42   * @description XML export of project error codes.
43   */
44  public class ExportSdocCodeMojo extends AbstractSdocCodeMojo // NOPMD
45  {
46    // ********************************* Fields *********************************
47  
48    // --- constants ------------------------------------------------------------
49  
50    // --- members --------------------------------------------------------------
51  
52    // ... Mojo infrastructure ..................................................
53  
54    /**
55     * The client Jar archiver.
56     *
57     * @component role="org.codehaus.plexus.archiver.Archiver" roleHint="jar"
58     */
59    private JarArchiver sdocJarArchiver;
60  
61    /**
62     * The archive configuration to use. See <a
63     * href="http://maven.apache.org/shared/maven-archiver/index.html">Maven
64     * Archiver Reference</a>.
65     *
66     * @parameter
67     */
68    private final MavenArchiveConfiguration archive =
69        new MavenArchiveConfiguration();
70  
71  
72    /**
73     * Specifies whether to attach the generated artifact to the project helper.
74     *
75     * @parameter expression="${attach}" default-value="true"
76     * @since 1.0
77     */
78    private boolean attach;
79  
80    /**
81     * The classifier to use to generate the attached artifact.
82     *
83     * @parameter default-value="sdoc_appcode"
84     * @since 1.0
85     */
86    private String classifier;
87  
88    // ****************************** Initializer *******************************
89  
90    // ****************************** Constructors ******************************
91  
92    // ****************************** Inner Classes *****************************
93  
94    // ********************************* Methods ********************************
95  
96    // --- init -----------------------------------------------------------------
97  
98    // --- get&set --------------------------------------------------------------
99  
100   // --- business -------------------------------------------------------------
101 
102 
103   @Override
104   protected void handleGeneratedDocuments() throws MojoExecutionException,
105     MojoFailureException
106   {
107     addMetaData();
108     attachArtifact();
109   }
110 
111   private void addMetaData() throws MojoExecutionException
112   {
113     final File metaInfDir = new File(output, "META-INF");
114     MojoIoUtils.provideMojoDirectory(metaInfDir);
115 
116     final Properties properties = new Properties();
117     properties.setProperty("res.type", "instances");
118     properties.setProperty("res.export.groupId", project.getGroupId());
119     properties.setProperty("res.export.artifactId", project.getArtifactId());
120     properties.setProperty("res.export.version", project.getVersion());
121     properties.setProperty("res.namespace",
122         "http://www.smartics.de/project/process/implementation/appcode");
123     properties.setProperty("res.instances", "process/implementation/appcode");
124 
125     final File propertiesFile =
126         new File(metaInfDir, "projectdoc-resource.properties");
127     OutputStream out = null;
128     try
129     {
130       out = new BufferedOutputStream(new FileOutputStream(propertiesFile));
131       properties.store(out,
132           "Export of document instances for projectdoc enabled projects.");
133     }
134     catch (final IOException e)
135     {
136       throw new MojoExecutionException(
137           "Cannot create properties file for sdoc export.", e);
138     }
139     finally
140     {
141       IOUtils.closeQuietly(out);
142     }
143 
144   }
145 
146   private void attachArtifact() throws MojoExecutionException
147   {
148     final String finalName =
149         project.getBuild().getFinalName() + '-' + classifier + ".jar";
150     final File sdocJarFile =
151         new File(project.getBuild().getDirectory(), finalName);
152     final MavenProjectHelper helper = new DefaultMavenProjectHelper();
153     final MavenArchiver clientArchiver = new MavenArchiver();
154     clientArchiver.setArchiver(sdocJarArchiver);
155     clientArchiver.setOutputFile(sdocJarFile);
156 
157     try
158     {
159       clientArchiver.getArchiver().addDirectory(output);
160       clientArchiver.createArchive(project, archive);
161       if (this.attach)
162       {
163         helper.attachArtifact(project, sdocJarFile, classifier);
164       }
165     }
166     catch (final Exception e)
167     {
168       throw new MojoExecutionException(
169           "There was a problem creating the sdoc archive: " + e.getMessage(), e);
170     }
171   }
172 
173   // --- object basics --------------------------------------------------------
174 
175 }