PageRenderTime 1ms CodeModel.GetById 29ms app.highlight 6ms RepoModel.GetById 0ms app.codeStats 1ms

/maven/util/src/main/java/com/alibaba/citrus/maven/eclipse/base/eclipse/writers/wtp/EclipseWtpComponentWriter.java

https://github.com/webx/citrus-tool
Java | 213 lines | 126 code | 29 blank | 58 comment | 10 complexity | 692a6026091d1a00cb3a10c6658c43db MD5 | raw file
  1/*
  2 * Licensed to the Apache Software Foundation (ASF) under one
  3 * or more contributor license agreements.  See the NOTICE file
  4 * distributed with this work for additional information
  5 * regarding copyright ownership.  The ASF licenses this file
  6 * to you under the Apache License, Version 2.0 (the
  7 * "License"); you may not use this file except in compliance
  8 * with the License.  You may obtain a copy of the License at
  9 *
 10 *   http://www.apache.org/licenses/LICENSE-2.0
 11 *
 12 * Unless required by applicable law or agreed to in writing,
 13 * software distributed under the License is distributed on an
 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15 * KIND, either express or implied.  See the License for the
 16 * specific language governing permissions and limitations
 17 * under the License.
 18 */
 19
 20package com.alibaba.citrus.maven.eclipse.base.eclipse.writers.wtp;
 21
 22import java.io.File;
 23import java.io.FileOutputStream;
 24import java.io.IOException;
 25import java.io.OutputStreamWriter;
 26import java.io.Writer;
 27
 28import com.alibaba.citrus.maven.eclipse.base.eclipse.Constants;
 29import com.alibaba.citrus.maven.eclipse.base.eclipse.EclipseSourceDir;
 30import com.alibaba.citrus.maven.eclipse.base.eclipse.Messages;
 31import com.alibaba.citrus.maven.eclipse.base.ide.IdeUtils;
 32import com.alibaba.citrus.maven.eclipse.base.ide.JeeUtils;
 33import org.apache.maven.artifact.repository.ArtifactRepository;
 34import org.apache.maven.plugin.MojoExecutionException;
 35import org.codehaus.plexus.util.IOUtil;
 36import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
 37import org.codehaus.plexus.util.xml.XMLWriter;
 38import org.codehaus.plexus.util.xml.Xpp3Dom;
 39
 40/**
 41 * Creates a .settings folder for Eclipse WTP 1.x release and writes out the configuration under it.
 42 *
 43 * @author <a href="mailto:rahul.thakur.xdev@gmail.com">Rahul Thakur</a>
 44 * @author <a href="mailto:fgiust@apache.org">Fabrizio Giustina</a>
 45 * @version $Id: EclipseWtpComponentWriter.java 1152335 2011-07-29 18:40:03Z rfscholte $
 46 */
 47public class EclipseWtpComponentWriter
 48        extends AbstractWtpResourceWriter {
 49
 50    /** Context root attribute. */
 51    public static final String ATTR_CONTEXT_ROOT = "context-root"; //$NON-NLS-1$
 52
 53    /** The .settings folder for Web Tools Project 1.x release. */
 54    public static final String DIR_WTP_SETTINGS = ".settings"; //$NON-NLS-1$
 55
 56    /**
 57     * File name where the WTP component settings will be stored for our Eclipse Project.
 58     *
 59     * @return <code>.component</code>
 60     */
 61    protected String getComponentFileName() {
 62        return ".component"; //$NON-NLS-1$
 63    }
 64
 65    /**
 66     * Version number added to component configuration.
 67     *
 68     * @return <code>1.0</code>
 69     */
 70    protected String getProjectVersion() {
 71        return null;
 72    }
 73
 74    /** @see com.alibaba.citrus.maven.eclipse.base.eclipse.writers.EclipseWriter#write() */
 75    public void write()
 76            throws MojoExecutionException {
 77
 78        // create a .settings directory (if not existing)
 79        File settingsDir = new File(config.getEclipseProjectDirectory(), DIR_WTP_SETTINGS);
 80        settingsDir.mkdirs();
 81
 82        Writer w;
 83        try {
 84            w =
 85                    new OutputStreamWriter(new FileOutputStream(new File(settingsDir, getComponentFileName())),
 86                                           "UTF-8");
 87        } catch (IOException ex) {
 88            throw new MojoExecutionException(Messages.getString("EclipsePlugin.erroropeningfile"), ex); //$NON-NLS-1$
 89        }
 90
 91        // create a .component file and write out to it
 92        XMLWriter writer = new PrettyPrintXMLWriter(w, "UTF-8", null);
 93
 94        writeModuleTypeComponent(writer, config.getPackaging(), config.getBuildOutputDirectory(),
 95                                 config.getSourceDirs(), config.getLocalRepository());
 96
 97        IOUtil.close(w);
 98    }
 99
100    /**
101     * Writes out the module type settings for a Web Tools Project to a component file.
102     *
103     * @param writer
104     * @param packaging
105     * @param buildOutputDirectory
106     * @param sourceDirs
107     * @param localRepository
108     * @throws MojoExecutionException
109     */
110    private void writeModuleTypeComponent(XMLWriter writer, String packaging, File buildOutputDirectory,
111                                          EclipseSourceDir[] sourceDirs, ArtifactRepository localRepository)
112            throws MojoExecutionException {
113        writer.startElement(ELT_PROJECT_MODULES);
114        writer.addAttribute(ATTR_MODULE_ID, "moduleCoreId"); //$NON-NLS-1$
115        if (getProjectVersion() != null) {
116            writer.addAttribute(ATTR_PROJECT_VERSION, getProjectVersion());
117        }
118        writer.startElement(ELT_WB_MODULE);
119
120        // we should use the eclipse project name as the deploy name.
121        writer.addAttribute(ATTR_DEPLOY_NAME, this.config.getEclipseProjectName());
122
123        // deploy-path is "/" for utility and ejb projects, "/WEB-INF/classes" for webapps
124        String target = "/"; //$NON-NLS-1$
125
126        if (Constants.PROJECT_PACKAGING_WAR.equalsIgnoreCase(packaging)) //$NON-NLS-1$
127        {
128            target = "/WEB-INF/classes"; //$NON-NLS-1$
129
130            File warSourceDirectory =
131                    new File(IdeUtils.getPluginSetting(config.getProject(), JeeUtils.ARTIFACT_MAVEN_WAR_PLUGIN,
132                                                       "warSourceDirectory", //$NON-NLS-1$
133                                                       config.getProject().getBasedir() + "/src/main/webapp")); //$NON-NLS-1$
134
135            writeContextRoot(writer);
136
137            writer.startElement(ELT_WB_RESOURCE);
138            writer.addAttribute(ATTR_DEPLOY_PATH, "/"); //$NON-NLS-1$
139            writer.addAttribute(ATTR_SOURCE_PATH,
140                                IdeUtils.toRelativeAndFixSeparator(config.getEclipseProjectDirectory(),
141                                                                   warSourceDirectory, false));
142            writer.endElement();
143
144            // add web resources over the top of the war source directory
145            Xpp3Dom[] webResources =
146                    IdeUtils.getPluginConfigurationDom(config.getProject(), JeeUtils.ARTIFACT_MAVEN_WAR_PLUGIN,
147                                                       new String[] { "webResources", "resource" });
148            for (int index = 0; index < webResources.length; index++) {
149                File webResourceDirectory = new File(webResources[index].getChild("directory").getValue());
150                writer.startElement(ELT_WB_RESOURCE);
151                writer.addAttribute(ATTR_DEPLOY_PATH, "/"); //$NON-NLS-1$
152                writer.addAttribute(ATTR_SOURCE_PATH,
153                                    IdeUtils.toRelativeAndFixSeparator(config.getEclipseProjectDirectory(),
154                                                                       webResourceDirectory, false));
155                writer.endElement();
156            }
157
158            // @todo is this really needed?
159            writer.startElement(ELT_PROPERTY);
160            writer.addAttribute(ATTR_NAME, "java-output-path"); //$NON-NLS-1$
161            writer.addAttribute(ATTR_VALUE, "/" //$NON-NLS-1$
162                                            + IdeUtils.toRelativeAndFixSeparator(config.getProject().getBasedir(),
163                                                                                 buildOutputDirectory,
164                                                                                 false));
165            writer.endElement(); // property
166        } else if (Constants.PROJECT_PACKAGING_EAR.equalsIgnoreCase(packaging)) //$NON-NLS-1$
167        {
168
169            String defaultApplicationXML =
170                    config.getWtpapplicationxml() ? "/target/eclipseEar" : "/src/main/application";
171
172            String earSourceDirectory =
173                    IdeUtils.getPluginSetting(config.getProject(), JeeUtils.ARTIFACT_MAVEN_EAR_PLUGIN,
174                                              "earSourceDirectory", //$NON-NLS-1$
175                                              config.getProject().getBasedir() + defaultApplicationXML); //$NON-NLS-1$
176            writer.startElement(ELT_WB_RESOURCE);
177            writer.addAttribute(ATTR_DEPLOY_PATH, "/"); //$NON-NLS-1$
178            writer.addAttribute(ATTR_SOURCE_PATH,
179                                IdeUtils.toRelativeAndFixSeparator(config.getEclipseProjectDirectory(),
180                                                                   new File(earSourceDirectory), false));
181            writer.endElement();
182        }
183
184        if (Constants.PROJECT_PACKAGING_WAR.equalsIgnoreCase(packaging)
185            || Constants.PROJECT_PACKAGING_EAR.equalsIgnoreCase(packaging)) //$NON-NLS-1$ //$NON-NLS-2$
186        {
187            // write out the dependencies.
188            writeWarOrEarResources(writer, config.getProject(), localRepository);
189        }
190
191        for (int j = 0; j < sourceDirs.length; j++) {
192            EclipseSourceDir dir = sourceDirs[j];
193            // test src/resources are not added to wtpmodules
194            if (!dir.isTest()) {
195                // <wb-resource deploy-path="/" source-path="/src/java" />
196                writer.startElement(ELT_WB_RESOURCE);
197                writer.addAttribute(ATTR_DEPLOY_PATH, target);
198                writer.addAttribute(ATTR_SOURCE_PATH, dir.getPath());
199                writer.endElement();
200            }
201        }
202
203        writer.endElement(); // wb-module
204        writer.endElement(); // project-modules
205    }
206
207    /** @param writer  */
208    protected void writeContextRoot(XMLWriter writer) {
209        writer.startElement(ELT_PROPERTY);
210        writer.addAttribute(ATTR_CONTEXT_ROOT, config.getContextName());
211        writer.endElement(); // property
212    }
213}