Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / org.gvsig.installer / org.gvsig.installer.maven / src / main / java / org / gvsig / installer / maven / AbstractGeneratePackageMojo.java @ 34019

History | View | Annotate | Download (7.43 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.tools.library.impl.DefaultLibrariesInitializer;
42
import org.gvsig.tools.locator.LocatorException;
43

    
44
/**
45
 * Abstract Maven mojo to launch the gvSIG installer to generate a package of a
46
 * gvSIG plugin.
47
 * <p>
48
 * Look at the <a href=
49
 * "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
79
     * official gvSIG name will be used, as provided by
80
     * the org.gvsig.installer.lib.api.InstallerManager.
81
     * 
82
     * @parameter
83
     */
84
    private String packageFileName;
85

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

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

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

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

    
115
        Log log = getLog();
116

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

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

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

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

    
137
            InstallerManager manager = InstallerLocator.getInstallerManager();
138

    
139
            makePluginService =
140
                manager.getMakePluginPackageService(pluginsFolder);
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 =
158
            packageFileName == null ? getPackageFileName(info)
159
                : packageFileName;
160
        if (!packageFolder.exists()) {
161
            packageFolder.mkdirs();
162
        }
163
        packageBundleFile = new File(packageFolder, packageName);
164

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

    
169
            createPackage(makePluginService, info, bos);
170

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

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

    
190
    /**
191
     * Returns the name of the package file to create.
192
     * 
193
     * @param info
194
     *            package information
195
     * @return the name of the file to create
196
     */
197
    protected abstract String getPackageFileName(PackageInfo info);
198

    
199
    /**
200
     * Returns the name of the package type to create.
201
     * 
202
     * @return the name of the package type to create
203
     */
204
    protected abstract String getPackageTypeName();
205

    
206
    /**
207
     * Creates the package file into the given {@link OutputStream}.
208
     * 
209
     * @param makePluginService
210
     *            to use to create the package
211
     * @param info
212
     *            the information of the package to create
213
     * @param os
214
     *            where to create the package file
215
     * @throws MakePluginPackageServiceException
216
     *             if there is an error creating the package file
217
     */
218
    protected abstract void createPackage(
219
        MakePluginPackageService makePluginService, PackageInfo info,
220
        OutputStream os) throws MakePluginPackageServiceException;
221
}