Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.installer / org.gvsig.installer.maven / src / main / java / org / gvsig / installer / maven / AbstractGeneratePackageMojo.java @ 40560

History | View | Annotate | Download (7.88 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.installer.maven;
25

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

    
33
import org.apache.maven.plugin.AbstractMojo;
34
import org.apache.maven.plugin.MojoExecutionException;
35
import org.apache.maven.plugin.MojoFailureException;
36
import org.apache.maven.plugin.logging.Log;
37

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

    
47
/**
48
 * Abstract Maven mojo to launch the gvSIG installer to generate a package of a
49
 * gvSIG plugin.
50
 * <p>
51
 * 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"
52
 * >gvSIG plugin naming standard</a> for information about installers naming and
53
 * versioning.
54
 * </p>
55
 * 
56
 * @see InstallerManager
57
 * 
58
 * @author gvSIG Team
59
 * @version $Id$
60
 */
61
public abstract class AbstractGeneratePackageMojo extends AbstractMojo {
62

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

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

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

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

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

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

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

    
116
                Log log = getLog();
117

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

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

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

    
137
                try {
138
                        new DefaultLibrariesInitializer().fullInitialize();
139

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

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

    
155
                log.info("Creating package of the package info:");
156
                log.info(info.toString());
157

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

    
166
                try {
167
                        FileOutputStream fos = new FileOutputStream(packageBundleFile);
168
                        BufferedOutputStream bos = new BufferedOutputStream(fos);
169

    
170
                        createPackage(makePluginService, info, bos);
171

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

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

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

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

    
219
        bos.flush();
220
        bos.close();
221
        fos.close();
222
    }
223

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

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

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