Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / org.gvsig.installer / org.gvsig.installer.maven / src / main / java / org / gvsig / installer / maven / AbstractGeneratePackageMojo.java @ 38540

History | View | Annotate | Download (7.83 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.installer.maven;
23

    
24
import java.io.BufferedOutputStream;
25
import java.io.File;
26
import java.io.FileNotFoundException;
27
import java.io.FileOutputStream;
28
import java.io.IOException;
29
import java.io.OutputStream;
30

    
31
import org.apache.maven.plugin.AbstractMojo;
32
import org.apache.maven.plugin.MojoExecutionException;
33
import org.apache.maven.plugin.MojoFailureException;
34
import org.apache.maven.plugin.logging.Log;
35

    
36
import org.gvsig.installer.lib.api.InstallerLocator;
37
import org.gvsig.installer.lib.api.InstallerManager;
38
import org.gvsig.installer.lib.api.PackageInfo;
39
import org.gvsig.installer.lib.api.creation.MakePluginPackageService;
40
import org.gvsig.installer.lib.api.creation.MakePluginPackageServiceException;
41
import org.gvsig.installer.lib.impl.utils.MD5BinaryFileUtils;
42
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer;
43
import org.gvsig.tools.locator.LocatorException;
44

    
45
/**
46
 * Abstract Maven mojo to launch the gvSIG installer to generate a package of a
47
 * gvSIG plugin.
48
 * <p>
49
 * Look at the <a href="http://www.gvsig.org/web/projects/gvsig-desktop/docs/devel/gvsig-devel-guide/2.0.0/anexos/proyectos-oficiales-en-gvsig/nombrado-de-binarios-para-un-plugin-de-gvsig"
50
 * >gvSIG plugin naming standard</a> for information about installers naming and
51
 * versioning.
52
 * </p>
53
 * 
54
 * @see InstallerManager
55
 * 
56
 * @author gvSIG Team
57
 * @version $Id$
58
 */
59
public abstract class AbstractGeneratePackageMojo extends AbstractMojo {
60

    
61
        /**
62
         * Location of the gvSIG plugins folder.
63
         * 
64
         * @parameter
65
         * @required
66
         */
67
        private File pluginsFolder;
68

    
69
        /**
70
         * Location of the folder where to create the package file.
71
         * 
72
         * @parameter
73
         * @required
74
         */
75
        private File packageFolder;
76

    
77
        /**
78
         * Name of the package file. If not provided, the official gvSIG name will
79
         * be used, as provided by the org.gvsig.installer.lib.api.InstallerManager.
80
         * 
81
         * @parameter
82
         */
83
        private String packageFileName;
84

    
85
        /**
86
         * Plugin project artifactId or code, used as gvSIG plugin name.
87
         * 
88
         * @parameter expression="${project.artifactId}"
89
         * @required
90
         */
91
        private String artifactId;
92

    
93
        /**
94
         * Plugin project packaging, to check it is of jar type.
95
         * 
96
         * @parameter expression="${project.packaging}"
97
         * @required
98
         */
99
        private String packaging;
100

    
101
        /**
102
         * If the mojo execution is disabled. Useful for projects that inherit the
103
         * maven plugin configuration but don't generate installer.
104
         * 
105
         * @parameter
106
         */
107
        private boolean disabled = false;
108

    
109
        public void execute() throws MojoExecutionException, MojoFailureException {
110
                String packageName;
111
                MakePluginPackageService makePluginService;
112
                File packageBundleFile = null;
113

    
114
                Log log = getLog();
115

    
116
                if (disabled) {
117
                        log.info(getPackageTypeName() + " generation disabled.");
118
                        return;
119
                }
120

    
121
                if (!"jar".equals(packaging)) {
122
                        log
123
                                        .info("Running on a project with packaging of type "
124
                                                        + packaging
125
                                                        + ". Do nothing, as we only create installers for projects "
126
                                                        + "with jar packaging");
127
                        return;
128
                }
129

    
130
                log.info("Generating a " + getPackageTypeName() + " for the plugin: "
131
                                + artifactId + " with the following information:");
132
                log.info("\tgvSIG Plugin's folder: " + pluginsFolder);
133
                log.info("\tPackage file destination folder: " + packageFolder);
134

    
135
                try {
136
                        new DefaultLibrariesInitializer().fullInitialize();
137

    
138
                        InstallerManager manager = InstallerLocator.getInstallerManager();
139
            manager.setDefaultLocalAddonRepository(pluginsFolder);
140
                        makePluginService = manager.getMakePluginPackageService();
141
                } catch (LocatorException e) {
142
                        throw new MojoExecutionException(
143
                                        "Error getting a reference to the InstallerManager", e);
144
                } catch (MakePluginPackageServiceException e) {
145
                        throw new MojoExecutionException(
146
                                        "Error getting a MakePluginPackageService for the "
147
                                                        + " plugin folder: " + pluginsFolder, e);
148
                }
149

    
150
                // Get and fill the package info data
151
                PackageInfo info = makePluginService.getPluginPackageInfo(artifactId);
152

    
153
                log.info("Creating package of the package info:");
154
                log.info(info.toString());
155

    
156
                // Create the package bundle file
157
                packageName = packageFileName == null ? getPackageFileName(info)
158
                                : packageFileName;
159
                if (!packageFolder.exists()) {
160
                        packageFolder.mkdirs();
161
                }
162
                packageBundleFile = new File(packageFolder, packageName);
163

    
164
                try {
165
                        FileOutputStream fos = new FileOutputStream(packageBundleFile);
166
                        BufferedOutputStream bos = new BufferedOutputStream(fos);
167

    
168
                        createPackage(makePluginService, info, bos);
169

    
170
                        bos.flush();
171
                        bos.close();
172
                        fos.close();
173
                        
174
                        writeMD5FileForBinaryFile(packageBundleFile);
175
                } catch (FileNotFoundException e) {
176
                        throw new MojoExecutionException("Error creating the "
177
                                        + getPackageTypeName() + " file: " + packageBundleFile, e);
178
                } catch (MakePluginPackageServiceException e) {
179
                        throw new MojoExecutionException(
180
                                        "Error creating the package file: " + packageName
181
                                                        + ", for the plugin: " + artifactId, e);
182
                } catch (IOException e) {
183
                        throw new MojoExecutionException("I/O error writing the "
184
                                        + getPackageTypeName() + " file: " + packageBundleFile, e);
185
                }
186

    
187
                log.info(getPackageTypeName() + " file created successfully as: "
188
                                + packageName);
189
        }
190

    
191
    private void writeMD5FileForBinaryFile(File the_file) throws IOException {
192
        
193
        String md5_str = "";
194
        try {
195
            md5_str = MD5BinaryFileUtils.getMD5Checksum(the_file);
196
        } catch (Exception ex) {
197
            throw new IOException(
198
                "Unable to compute MD5 checksum for file: "
199
                + the_file.getAbsolutePath() + " (" + ex.getMessage() + ")");
200
        }
201
        
202
        String separator = " *";
203
        // for ascii file: separator = "  ";
204
        
205
        md5_str = md5_str + separator + the_file.getName();
206
        
207
        File md5_file = new File(the_file.getAbsolutePath() + ".md5");
208
        if (md5_file.exists()) {
209
            md5_file.delete();
210
        }
211

    
212
        FileOutputStream fos = new FileOutputStream(md5_file);
213
        BufferedOutputStream bos = new BufferedOutputStream(fos);
214
        
215
        bos.write(md5_str.getBytes());
216

    
217
        bos.flush();
218
        bos.close();
219
        fos.close();
220
    }
221

    
222
    /**
223
         * Returns the name of the package file to create.
224
         * 
225
         * @param info
226
         *            package information
227
         * @return the name of the file to create
228
         */
229
        protected abstract String getPackageFileName(PackageInfo info);
230

    
231
        /**
232
         * Returns the name of the package type to create.
233
         * 
234
         * @return the name of the package type to create
235
         */
236
        protected abstract String getPackageTypeName();
237

    
238
        /**
239
         * Creates the package file into the given {@link OutputStream}.
240
         * 
241
         * @param makePluginService
242
         *            to use to create the package
243
         * @param info
244
         *            the information of the package to create
245
         * @param os
246
         *            where to create the package file
247
         * @throws MakePluginPackageServiceException
248
         *             if there is an error creating the package file
249
         */
250
        protected abstract void createPackage(
251
                        MakePluginPackageService makePluginService, PackageInfo info,
252
                        OutputStream os) throws MakePluginPackageServiceException;
253
}