1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.maven.plugin.eclipse.writers.wtp; |
20 | |
|
21 | |
import java.io.File; |
22 | |
import java.io.FileOutputStream; |
23 | |
import java.io.IOException; |
24 | |
import java.io.OutputStreamWriter; |
25 | |
import java.io.Writer; |
26 | |
|
27 | |
import org.apache.maven.artifact.repository.ArtifactRepository; |
28 | |
import org.apache.maven.plugin.MojoExecutionException; |
29 | |
import org.apache.maven.plugin.eclipse.Constants; |
30 | |
import org.apache.maven.plugin.eclipse.EclipseSourceDir; |
31 | |
import org.apache.maven.plugin.eclipse.Messages; |
32 | |
import org.apache.maven.plugin.ide.IdeUtils; |
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 | |
import org.codehaus.plexus.util.xml.Xpp3Dom; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | 6 | public class EclipseWtpComponentWriter |
47 | |
extends AbstractWtpResourceWriter |
48 | |
{ |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
public static final String ATTR_CONTEXT_ROOT = "context-root"; |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
public static final String DIR_WTP_SETTINGS = ".settings"; |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
protected String getComponentFileName() |
66 | |
{ |
67 | 0 | return ".component"; |
68 | |
} |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
protected String getProjectVersion() |
76 | |
{ |
77 | 0 | return null; |
78 | |
} |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
public void write() |
84 | |
throws MojoExecutionException |
85 | |
{ |
86 | |
|
87 | |
|
88 | 6 | File settingsDir = new File( config.getEclipseProjectDirectory(), DIR_WTP_SETTINGS ); |
89 | 6 | settingsDir.mkdirs(); |
90 | |
|
91 | |
Writer w; |
92 | |
try |
93 | |
{ |
94 | 6 | w = |
95 | |
new OutputStreamWriter( new FileOutputStream( new File( settingsDir, getComponentFileName() ) ), |
96 | |
"UTF-8" ); |
97 | |
} |
98 | 0 | catch ( IOException ex ) |
99 | |
{ |
100 | 0 | throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex ); |
101 | 6 | } |
102 | |
|
103 | |
|
104 | 6 | XMLWriter writer = new PrettyPrintXMLWriter( w ); |
105 | |
|
106 | 6 | writeModuleTypeComponent( writer, config.getPackaging(), config.getBuildOutputDirectory(), |
107 | |
config.getSourceDirs(), config.getLocalRepository() ); |
108 | |
|
109 | 6 | IOUtil.close( w ); |
110 | 6 | } |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
private void writeModuleTypeComponent( XMLWriter writer, String packaging, File buildOutputDirectory, |
123 | |
EclipseSourceDir[] sourceDirs, ArtifactRepository localRepository ) |
124 | |
throws MojoExecutionException |
125 | |
{ |
126 | 6 | writer.startElement( ELT_PROJECT_MODULES ); |
127 | 6 | writer.addAttribute( ATTR_MODULE_ID, "moduleCoreId" ); |
128 | 6 | if ( getProjectVersion() != null ) |
129 | |
{ |
130 | 6 | writer.addAttribute( ATTR_PROJECT_VERSION, getProjectVersion() ); |
131 | |
} |
132 | 6 | writer.startElement( ELT_WB_MODULE ); |
133 | |
|
134 | |
|
135 | 6 | writer.addAttribute( ATTR_DEPLOY_NAME, this.config.getEclipseProjectName() ); |
136 | |
|
137 | |
|
138 | 6 | String target = "/"; |
139 | |
|
140 | 6 | if ( Constants.PROJECT_PACKAGING_WAR.equalsIgnoreCase( packaging ) ) |
141 | |
{ |
142 | 0 | target = "/WEB-INF/classes"; |
143 | |
|
144 | 0 | File warSourceDirectory = |
145 | |
new File( IdeUtils.getPluginSetting( config.getProject(), JeeUtils.ARTIFACT_MAVEN_WAR_PLUGIN, |
146 | |
"warSourceDirectory", |
147 | |
config.getProject().getBasedir() + "/src/main/webapp" ) ); |
148 | |
|
149 | 0 | writeContextRoot( writer ); |
150 | |
|
151 | 0 | writer.startElement( ELT_WB_RESOURCE ); |
152 | 0 | writer.addAttribute( ATTR_DEPLOY_PATH, "/" ); |
153 | 0 | writer.addAttribute( ATTR_SOURCE_PATH, |
154 | |
IdeUtils.toRelativeAndFixSeparator( config.getEclipseProjectDirectory(), |
155 | |
warSourceDirectory, false ) ); |
156 | 0 | writer.endElement(); |
157 | |
|
158 | |
|
159 | 0 | Xpp3Dom[] webResources = |
160 | |
IdeUtils.getPluginConfigurationDom( config.getProject(), JeeUtils.ARTIFACT_MAVEN_WAR_PLUGIN, |
161 | |
new String[] { "webResources", "resource" } ); |
162 | 0 | for ( int index = 0; index < webResources.length; index++ ) |
163 | |
{ |
164 | 0 | File webResourceDirectory = new File( webResources[index].getChild( "directory" ).getValue() ); |
165 | 0 | writer.startElement( ELT_WB_RESOURCE ); |
166 | 0 | writer.addAttribute( ATTR_DEPLOY_PATH, "/" ); |
167 | 0 | writer.addAttribute( ATTR_SOURCE_PATH, |
168 | |
IdeUtils.toRelativeAndFixSeparator( config.getEclipseProjectDirectory(), |
169 | |
webResourceDirectory, false ) ); |
170 | 0 | writer.endElement(); |
171 | |
} |
172 | |
|
173 | |
|
174 | 0 | writer.startElement( ELT_PROPERTY ); |
175 | 0 | writer.addAttribute( ATTR_NAME, "java-output-path" ); |
176 | 0 | writer.addAttribute( ATTR_VALUE, "/" |
177 | |
+ IdeUtils.toRelativeAndFixSeparator( config.getProject().getBasedir(), buildOutputDirectory, false ) ); |
178 | 0 | writer.endElement(); |
179 | |
|
180 | 0 | } |
181 | 6 | else if ( Constants.PROJECT_PACKAGING_EAR.equalsIgnoreCase( packaging ) ) |
182 | |
{ |
183 | |
|
184 | 6 | String defaultApplicationXML = |
185 | |
config.getWtpapplicationxml() ? "/target/eclipseEar" : "/src/main/application"; |
186 | |
|
187 | 6 | String earSourceDirectory = |
188 | |
IdeUtils.getPluginSetting( config.getProject(), JeeUtils.ARTIFACT_MAVEN_EAR_PLUGIN, |
189 | |
"earSourceDirectory", |
190 | |
config.getProject().getBasedir() + defaultApplicationXML ); |
191 | 6 | writer.startElement( ELT_WB_RESOURCE ); |
192 | 6 | writer.addAttribute( ATTR_DEPLOY_PATH, "/" ); |
193 | 6 | writer.addAttribute( ATTR_SOURCE_PATH, |
194 | |
IdeUtils.toRelativeAndFixSeparator( config.getEclipseProjectDirectory(), |
195 | |
new File( earSourceDirectory ), false ) ); |
196 | 6 | writer.endElement(); |
197 | |
} |
198 | |
|
199 | 6 | if ( Constants.PROJECT_PACKAGING_WAR.equalsIgnoreCase( packaging ) |
200 | |
|| Constants.PROJECT_PACKAGING_EAR.equalsIgnoreCase( packaging ) ) |
201 | |
{ |
202 | |
|
203 | 6 | writeWarOrEarResources( writer, config.getProject(), localRepository ); |
204 | |
|
205 | |
} |
206 | |
|
207 | 6 | for ( int j = 0; j < sourceDirs.length; j++ ) |
208 | |
{ |
209 | 0 | EclipseSourceDir dir = sourceDirs[j]; |
210 | |
|
211 | 0 | if ( !dir.isTest() ) |
212 | |
{ |
213 | |
|
214 | 0 | writer.startElement( ELT_WB_RESOURCE ); |
215 | 0 | writer.addAttribute( ATTR_DEPLOY_PATH, target ); |
216 | 0 | writer.addAttribute( ATTR_SOURCE_PATH, dir.getPath() ); |
217 | 0 | writer.endElement(); |
218 | |
} |
219 | |
} |
220 | |
|
221 | 6 | writer.endElement(); |
222 | 6 | writer.endElement(); |
223 | 6 | } |
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
protected void writeContextRoot( XMLWriter writer ) |
229 | |
{ |
230 | 0 | writer.startElement( ELT_PROPERTY ); |
231 | 0 | writer.addAttribute( ATTR_CONTEXT_ROOT, config.getContextName() ); |
232 | 0 | writer.endElement(); |
233 | 0 | } |
234 | |
|
235 | |
} |