PageRenderTime 55ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/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. package com.alibaba.citrus.maven.eclipse.base.eclipse.writers.wtp;
  20. import java.io.File;
  21. import java.io.FileOutputStream;
  22. import java.io.IOException;
  23. import java.io.OutputStreamWriter;
  24. import java.io.Writer;
  25. import com.alibaba.citrus.maven.eclipse.base.eclipse.Constants;
  26. import com.alibaba.citrus.maven.eclipse.base.eclipse.EclipseSourceDir;
  27. import com.alibaba.citrus.maven.eclipse.base.eclipse.Messages;
  28. import com.alibaba.citrus.maven.eclipse.base.ide.IdeUtils;
  29. import com.alibaba.citrus.maven.eclipse.base.ide.JeeUtils;
  30. import org.apache.maven.artifact.repository.ArtifactRepository;
  31. import org.apache.maven.plugin.MojoExecutionException;
  32. import org.codehaus.plexus.util.IOUtil;
  33. import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
  34. import org.codehaus.plexus.util.xml.XMLWriter;
  35. import org.codehaus.plexus.util.xml.Xpp3Dom;
  36. /**
  37. * Creates a .settings folder for Eclipse WTP 1.x release and writes out the configuration under it.
  38. *
  39. * @author <a href="mailto:rahul.thakur.xdev@gmail.com">Rahul Thakur</a>
  40. * @author <a href="mailto:fgiust@apache.org">Fabrizio Giustina</a>
  41. * @version $Id: EclipseWtpComponentWriter.java 1152335 2011-07-29 18:40:03Z rfscholte $
  42. */
  43. public class EclipseWtpComponentWriter
  44. extends AbstractWtpResourceWriter {
  45. /** Context root attribute. */
  46. public static final String ATTR_CONTEXT_ROOT = "context-root"; //$NON-NLS-1$
  47. /** The .settings folder for Web Tools Project 1.x release. */
  48. public static final String DIR_WTP_SETTINGS = ".settings"; //$NON-NLS-1$
  49. /**
  50. * File name where the WTP component settings will be stored for our Eclipse Project.
  51. *
  52. * @return <code>.component</code>
  53. */
  54. protected String getComponentFileName() {
  55. return ".component"; //$NON-NLS-1$
  56. }
  57. /**
  58. * Version number added to component configuration.
  59. *
  60. * @return <code>1.0</code>
  61. */
  62. protected String getProjectVersion() {
  63. return null;
  64. }
  65. /** @see com.alibaba.citrus.maven.eclipse.base.eclipse.writers.EclipseWriter#write() */
  66. public void write()
  67. throws MojoExecutionException {
  68. // create a .settings directory (if not existing)
  69. File settingsDir = new File(config.getEclipseProjectDirectory(), DIR_WTP_SETTINGS);
  70. settingsDir.mkdirs();
  71. Writer w;
  72. try {
  73. w =
  74. new OutputStreamWriter(new FileOutputStream(new File(settingsDir, getComponentFileName())),
  75. "UTF-8");
  76. } catch (IOException ex) {
  77. throw new MojoExecutionException(Messages.getString("EclipsePlugin.erroropeningfile"), ex); //$NON-NLS-1$
  78. }
  79. // create a .component file and write out to it
  80. XMLWriter writer = new PrettyPrintXMLWriter(w, "UTF-8", null);
  81. writeModuleTypeComponent(writer, config.getPackaging(), config.getBuildOutputDirectory(),
  82. config.getSourceDirs(), config.getLocalRepository());
  83. IOUtil.close(w);
  84. }
  85. /**
  86. * Writes out the module type settings for a Web Tools Project to a component file.
  87. *
  88. * @param writer
  89. * @param packaging
  90. * @param buildOutputDirectory
  91. * @param sourceDirs
  92. * @param localRepository
  93. * @throws MojoExecutionException
  94. */
  95. private void writeModuleTypeComponent(XMLWriter writer, String packaging, File buildOutputDirectory,
  96. EclipseSourceDir[] sourceDirs, ArtifactRepository localRepository)
  97. throws MojoExecutionException {
  98. writer.startElement(ELT_PROJECT_MODULES);
  99. writer.addAttribute(ATTR_MODULE_ID, "moduleCoreId"); //$NON-NLS-1$
  100. if (getProjectVersion() != null) {
  101. writer.addAttribute(ATTR_PROJECT_VERSION, getProjectVersion());
  102. }
  103. writer.startElement(ELT_WB_MODULE);
  104. // we should use the eclipse project name as the deploy name.
  105. writer.addAttribute(ATTR_DEPLOY_NAME, this.config.getEclipseProjectName());
  106. // deploy-path is "/" for utility and ejb projects, "/WEB-INF/classes" for webapps
  107. String target = "/"; //$NON-NLS-1$
  108. if (Constants.PROJECT_PACKAGING_WAR.equalsIgnoreCase(packaging)) //$NON-NLS-1$
  109. {
  110. target = "/WEB-INF/classes"; //$NON-NLS-1$
  111. File warSourceDirectory =
  112. new File(IdeUtils.getPluginSetting(config.getProject(), JeeUtils.ARTIFACT_MAVEN_WAR_PLUGIN,
  113. "warSourceDirectory", //$NON-NLS-1$
  114. config.getProject().getBasedir() + "/src/main/webapp")); //$NON-NLS-1$
  115. writeContextRoot(writer);
  116. writer.startElement(ELT_WB_RESOURCE);
  117. writer.addAttribute(ATTR_DEPLOY_PATH, "/"); //$NON-NLS-1$
  118. writer.addAttribute(ATTR_SOURCE_PATH,
  119. IdeUtils.toRelativeAndFixSeparator(config.getEclipseProjectDirectory(),
  120. warSourceDirectory, false));
  121. writer.endElement();
  122. // add web resources over the top of the war source directory
  123. Xpp3Dom[] webResources =
  124. IdeUtils.getPluginConfigurationDom(config.getProject(), JeeUtils.ARTIFACT_MAVEN_WAR_PLUGIN,
  125. new String[] { "webResources", "resource" });
  126. for (int index = 0; index < webResources.length; index++) {
  127. File webResourceDirectory = new File(webResources[index].getChild("directory").getValue());
  128. writer.startElement(ELT_WB_RESOURCE);
  129. writer.addAttribute(ATTR_DEPLOY_PATH, "/"); //$NON-NLS-1$
  130. writer.addAttribute(ATTR_SOURCE_PATH,
  131. IdeUtils.toRelativeAndFixSeparator(config.getEclipseProjectDirectory(),
  132. webResourceDirectory, false));
  133. writer.endElement();
  134. }
  135. // @todo is this really needed?
  136. writer.startElement(ELT_PROPERTY);
  137. writer.addAttribute(ATTR_NAME, "java-output-path"); //$NON-NLS-1$
  138. writer.addAttribute(ATTR_VALUE, "/" //$NON-NLS-1$
  139. + IdeUtils.toRelativeAndFixSeparator(config.getProject().getBasedir(),
  140. buildOutputDirectory,
  141. false));
  142. writer.endElement(); // property
  143. } else if (Constants.PROJECT_PACKAGING_EAR.equalsIgnoreCase(packaging)) //$NON-NLS-1$
  144. {
  145. String defaultApplicationXML =
  146. config.getWtpapplicationxml() ? "/target/eclipseEar" : "/src/main/application";
  147. String earSourceDirectory =
  148. IdeUtils.getPluginSetting(config.getProject(), JeeUtils.ARTIFACT_MAVEN_EAR_PLUGIN,
  149. "earSourceDirectory", //$NON-NLS-1$
  150. config.getProject().getBasedir() + defaultApplicationXML); //$NON-NLS-1$
  151. writer.startElement(ELT_WB_RESOURCE);
  152. writer.addAttribute(ATTR_DEPLOY_PATH, "/"); //$NON-NLS-1$
  153. writer.addAttribute(ATTR_SOURCE_PATH,
  154. IdeUtils.toRelativeAndFixSeparator(config.getEclipseProjectDirectory(),
  155. new File(earSourceDirectory), false));
  156. writer.endElement();
  157. }
  158. if (Constants.PROJECT_PACKAGING_WAR.equalsIgnoreCase(packaging)
  159. || Constants.PROJECT_PACKAGING_EAR.equalsIgnoreCase(packaging)) //$NON-NLS-1$ //$NON-NLS-2$
  160. {
  161. // write out the dependencies.
  162. writeWarOrEarResources(writer, config.getProject(), localRepository);
  163. }
  164. for (int j = 0; j < sourceDirs.length; j++) {
  165. EclipseSourceDir dir = sourceDirs[j];
  166. // test src/resources are not added to wtpmodules
  167. if (!dir.isTest()) {
  168. // <wb-resource deploy-path="/" source-path="/src/java" />
  169. writer.startElement(ELT_WB_RESOURCE);
  170. writer.addAttribute(ATTR_DEPLOY_PATH, target);
  171. writer.addAttribute(ATTR_SOURCE_PATH, dir.getPath());
  172. writer.endElement();
  173. }
  174. }
  175. writer.endElement(); // wb-module
  176. writer.endElement(); // project-modules
  177. }
  178. /** @param writer */
  179. protected void writeContextRoot(XMLWriter writer) {
  180. writer.startElement(ELT_PROPERTY);
  181. writer.addAttribute(ATTR_CONTEXT_ROOT, config.getContextName());
  182. writer.endElement(); // property
  183. }
  184. }