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 @ 37881

History | View | Annotate | Download (6.77 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="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
public abstract class AbstractGeneratePackageMojo extends AbstractMojo {
59

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

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

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

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

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

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

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

    
113
                Log log = getLog();
114

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

    
120
                if (!"jar".equals(packaging)) {
121
                        log
122
                                        .info("Running on a project with packaging of type "
123
                                                        + 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
            manager.setDefaultLocalAddonRepository(pluginsFolder);
139
                        makePluginService = manager.getMakePluginPackageService();
140
                } catch (LocatorException e) {
141
                        throw new MojoExecutionException(
142
                                        "Error getting a reference to the InstallerManager", e);
143
                } catch (MakePluginPackageServiceException e) {
144
                        throw new MojoExecutionException(
145
                                        "Error getting a MakePluginPackageService for the "
146
                                                        + " plugin folder: " + pluginsFolder, e);
147
                }
148

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

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

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

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

    
167
                        createPackage(makePluginService, info, bos);
168

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

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

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

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

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