Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / org.gvsig.installer / org.gvsig.installer.maven / src / main / java / org / gvsig / installer / maven / GenerateInstallerMojo.java @ 33705

History | View | Annotate | Download (6.06 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

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

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

    
43
/**
44
 * Maven mojo to launch the gvSIG installer to generate a installation
45
 * bundle of a gvSIG plugin.
46
 * <p>
47
 * Look at the <a href=
48
 * "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"
49
 * >gvSIG plugin naming standard</a> for information about installers naming and
50
 * versioning.
51
 * </p>
52
 * 
53
 * @see InstallerManager
54
 * 
55
 * @author gvSIG Team
56
 * @version $Id$
57
 * 
58
 * @goal create-installer
59
 */
60
public class GenerateInstallerMojo extends AbstractMojo {
61

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

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

    
78
    /**
79
     * Name of the package bundle file. If not provided, the
80
     * official gvSIG name will be used, as provided by
81
     * the org.gvsig.installer.lib.api.InstallerManager.
82
     * 
83
     * @parameter
84
     */
85
    private String bundleFileName;
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
105
     * the 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
        Log log = getLog();
113

    
114
        if (disabled) {
115
            log.info("Installer generation disabled.");
116
            return;
117
        }
118

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

    
126
        log.info("Generating a installable for the plugin: " + artifactId
127
            + " with the following information:");
128
        log.info("\tgvSIG Plugin's folder: " + pluginsFolder);
129
        log.info("\tBundle installation file to create: " + bundleFolder);
130

    
131
        File packageBundleFile = null;
132

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

    
136
            InstallerManager manager = InstallerLocator.getInstallerManager();
137

    
138
            MakePluginPackageService makePluginService =
139
                manager.getMakePluginPackageService(pluginsFolder);
140

    
141
            // Get and fill the package info data
142
            PackageInfo info =
143
                makePluginService.getPluginPackageInfo(artifactId);
144

    
145
            // Create the package bundle file
146
            String packageBundleFileName =
147
                bundleFileName == null ? manager.getPackageBundleFileName(info)
148
                    : bundleFileName;
149

    
150
            if (!bundleFolder.exists()) {
151
                bundleFolder.mkdirs();
152
            }
153
            packageBundleFile = new File(bundleFolder, packageBundleFileName);
154

    
155
            FileOutputStream fos = new FileOutputStream(packageBundleFile);
156
            BufferedOutputStream bos = new BufferedOutputStream(fos);
157

    
158
            makePluginService.createPluginPackage(info, bos);
159

    
160
            bos.flush();
161
            bos.close();
162
            fos.close();
163
        } catch (LocatorException e) {
164
            throw new MojoExecutionException(
165
                "Error getting a reference to the InstallerManager", e);
166
        } catch (MakePluginPackageServiceException e) {
167
            throw new MojoExecutionException(
168
                "Error getting a MakePluginPackageService for the "
169
                    + "plugin folder: " + pluginsFolder, e);
170
        } catch (FileNotFoundException e) {
171
            throw new MojoExecutionException(
172
                "Error creating the bundle installation file: "
173
                    + packageBundleFile, e);
174
        } catch (IOException e) {
175
            throw new MojoExecutionException(
176
                "I/O error writing the bundle installation file: "
177
                    + packageBundleFile, e);
178
        }
179

    
180
        log.info("Bundle installation file created successfully");
181
    }
182
}