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