1 package org.apache.maven.plugin.eclipse.writers.myeclipse;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import java.io.File;
24 import java.io.FileWriter;
25 import java.io.IOException;
26
27 import org.apache.maven.plugin.MojoExecutionException;
28 import org.apache.maven.plugin.eclipse.Constants;
29 import org.apache.maven.plugin.eclipse.Messages;
30 import org.apache.maven.plugin.eclipse.writers.AbstractEclipseWriter;
31 import org.apache.maven.plugin.ide.IdeUtils;
32 import org.apache.maven.plugin.ide.JeeDescriptor;
33 import org.apache.maven.plugin.ide.JeeUtils;
34 import org.codehaus.plexus.util.IOUtil;
35 import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
36 import org.codehaus.plexus.util.xml.XMLWriter;
37
38
39
40
41
42
43 public class MyEclipseMetadataWriter
44 extends AbstractEclipseWriter
45 {
46
47 private static final String MYECLIPSE_MYMETADATA_FILENAME = ".mymetadata";
48
49 private static final String MYECLIPSE_METADATA_PROJECT = "project-module";
50
51 private static final String MYECLIPSE_METADATA_PROJECT_TYPE = "type";
52
53 private static final String MYECLIPSE_METADATA_PROJECT_NAME = "name";
54
55 private static final String MYECLIPSE_METADATA_PROJECT_ID = "id";
56
57 private static final String MYECLIPSE_METADATA_PROJECT_CONTEXT_ROOT = "context-root";
58
59 private static final String MYECLIPSE_METADATA_PROJECT_J2EE_SPEC = "j2ee-spec";
60
61 private static final String MYECLIPSE_METADATA_PROJECT_ARCHIVE = "archive";
62
63 private static final String MYECLIPSE_METADATA_PROJECT_TYPE_WAR = "WEB";
64
65 private static final String MYECLIPSE_METADATA_PROJECT_TYPE_EAR = "EAR";
66
67 private static final String MYECLIPSE_METADATA_PROJECT_TYPE_EJB = "EJB";
68
69 private static final String MYECLIPSE_METADATA_PROJECT_ATTRIBUTES = "attributes";
70
71 private static final String MYECLIPSE_METADATA_PROJECT_ATTRIBUTE = "attribute";
72
73
74
75
76
77
78 public void write()
79 throws MojoExecutionException
80 {
81 String packaging = config.getProject().getPackaging();
82
83 if ( !Constants.PROJECT_PACKAGING_EAR.equalsIgnoreCase( packaging )
84 && !Constants.PROJECT_PACKAGING_WAR.equalsIgnoreCase( packaging )
85 && !Constants.PROJECT_PACKAGING_EJB.equalsIgnoreCase( packaging ) )
86 {
87 return;
88 }
89
90 FileWriter w;
91
92 try
93 {
94 w = new FileWriter( new File( config.getEclipseProjectDirectory(), MYECLIPSE_MYMETADATA_FILENAME ) );
95 }
96 catch ( IOException ex )
97 {
98 throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex );
99 }
100
101 XMLWriter writer = new PrettyPrintXMLWriter( w, "UTF-8", null );
102
103 writer.startElement( MYECLIPSE_METADATA_PROJECT );
104 writer.addAttribute( MYECLIPSE_METADATA_PROJECT_TYPE, getMyEclipseProjectType( packaging ) );
105 writer.addAttribute( MYECLIPSE_METADATA_PROJECT_NAME, config.getEclipseProjectName() );
106 writer.addAttribute( MYECLIPSE_METADATA_PROJECT_ID, config.getEclipseProjectName() );
107
108 if ( Constants.PROJECT_PACKAGING_WAR.equalsIgnoreCase( packaging ) )
109 {
110
111
112 String warContextRoot =
113 IdeUtils.getPluginSetting( config.getProject(), JeeUtils.ARTIFACT_MAVEN_WAR_PLUGIN, "warContextRoot",
114 "/" + config.getProject().getArtifactId() );
115
116 writer.addAttribute( MYECLIPSE_METADATA_PROJECT_CONTEXT_ROOT, warContextRoot );
117
118 writer.addAttribute( MYECLIPSE_METADATA_PROJECT_J2EE_SPEC, getJeeVersion() );
119
120 writer.addAttribute( MYECLIPSE_METADATA_PROJECT_ARCHIVE, config.getEclipseProjectName() + ".war" );
121 }
122
123 if ( Constants.PROJECT_PACKAGING_EAR.equalsIgnoreCase( packaging ) )
124 {
125
126 writer.addAttribute( MYECLIPSE_METADATA_PROJECT_ARCHIVE, config.getEclipseProjectName() + ".ear" );
127 }
128
129 writer.startElement( MYECLIPSE_METADATA_PROJECT_ATTRIBUTES );
130 if ( Constants.PROJECT_PACKAGING_WAR.equalsIgnoreCase( packaging ) )
131 {
132 writer.startElement( MYECLIPSE_METADATA_PROJECT_ATTRIBUTE );
133 writer.addAttribute( "name", "webrootdir" );
134
135 writer.addAttribute( "value", "src/main/webapp" );
136 writer.endElement();
137 }
138
139 writer.endElement();
140
141
142 writer.endElement();
143
144 IOUtil.close( w );
145 }
146
147
148
149
150
151 private String getMyEclipseProjectType( String packaging )
152 {
153 if ( Constants.PROJECT_PACKAGING_WAR.equalsIgnoreCase( packaging ) )
154 {
155 return MYECLIPSE_METADATA_PROJECT_TYPE_WAR;
156 }
157 if ( Constants.PROJECT_PACKAGING_EAR.equalsIgnoreCase( packaging ) )
158 {
159 return MYECLIPSE_METADATA_PROJECT_TYPE_EAR;
160 }
161 if ( Constants.PROJECT_PACKAGING_EJB.equalsIgnoreCase( packaging ) )
162 {
163 return MYECLIPSE_METADATA_PROJECT_TYPE_EJB;
164 }
165
166 return null;
167 }
168
169
170
171
172
173
174
175 private String getJeeVersion()
176 {
177 String jeeVersion;
178 if ( config.getJeeVersion() != null )
179 {
180 jeeVersion = JeeUtils.getJeeDescriptorFromJeeVersion( config.getJeeVersion() ).getJeeVersion();
181 }
182 else
183 {
184 jeeVersion =
185 JeeUtils.getJeeDescriptorFromServletVersion( JeeUtils.resolveServletVersion( config.getProject() ) ).getJeeVersion();
186 }
187
188 if ( jeeVersion == null )
189 {
190 return JeeDescriptor.JEE_1_4;
191 }
192
193 return jeeVersion;
194 }
195 }