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; |
20 | |
|
21 | |
import java.io.BufferedReader; |
22 | |
import java.io.File; |
23 | |
import java.io.FileInputStream; |
24 | |
import java.io.FileNotFoundException; |
25 | |
import java.io.FileOutputStream; |
26 | |
import java.io.IOException; |
27 | |
import java.io.InputStreamReader; |
28 | |
import java.io.OutputStreamWriter; |
29 | |
import java.io.Writer; |
30 | |
import java.util.Arrays; |
31 | |
|
32 | |
import org.apache.maven.plugin.MojoExecutionException; |
33 | |
import org.apache.maven.plugin.eclipse.Messages; |
34 | |
import org.apache.maven.plugin.ide.IdeDependency; |
35 | |
import org.codehaus.plexus.util.IOUtil; |
36 | |
import org.codehaus.plexus.util.StringUtils; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | 0 | public class EclipseOSGiManifestWriter |
46 | |
extends AbstractEclipseWriter |
47 | |
{ |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
private static final String NEWLINE = "\n"; |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
public final static String ENTRY_BUNDLE_CLASSPATH = "Bundle-ClassPath:"; |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
public final static String ENTRY_BUNDLE_NAME = "Bundle-Name:"; |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
public final static String ENTRY_BUNDLE_SYMBOLICNAME = "Bundle-SymbolicName:"; |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
public final static String ENTRY_BUNDLE_VERSION = "Bundle-Version:"; |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
public final static String ENTRY_BUNDLE_VENDOR = "Bundle-Vendor:"; |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
public void write() |
85 | |
throws MojoExecutionException |
86 | |
{ |
87 | |
|
88 | 0 | if ( !config.getOSGIManifestFile().exists() ) |
89 | |
{ |
90 | 0 | log.warn( Messages.getString( "EclipseOSGiManifestWriter.nomanifestfile", |
91 | |
config.getOSGIManifestFile().getAbsolutePath() ) ); |
92 | 0 | return; |
93 | |
} |
94 | |
|
95 | 0 | StringBuffer manifestSb = rewriteManifest( config.getOSGIManifestFile() ); |
96 | 0 | Writer out = null; |
97 | |
try |
98 | |
{ |
99 | 0 | out = new OutputStreamWriter( new FileOutputStream( config.getOSGIManifestFile() ), "UTF-8" ); |
100 | 0 | out.write( manifestSb.toString() ); |
101 | |
} |
102 | 0 | catch ( FileNotFoundException e ) |
103 | |
{ |
104 | 0 | throw new MojoExecutionException( Messages.getString( "EclipsePlugin.cantwritetofile", |
105 | |
config.getOSGIManifestFile().getAbsolutePath() ) ); |
106 | |
} |
107 | 0 | catch ( IOException e ) |
108 | |
{ |
109 | 0 | throw new MojoExecutionException( Messages.getString( "EclipsePlugin.cantwritetofile", |
110 | |
config.getOSGIManifestFile().getAbsolutePath() ), e ); |
111 | |
} |
112 | |
finally |
113 | |
{ |
114 | 0 | IOUtil.close( out ); |
115 | 0 | } |
116 | 0 | } |
117 | |
|
118 | |
protected StringBuffer rewriteManifest( File manifestFile ) |
119 | |
throws MojoExecutionException |
120 | |
{ |
121 | |
|
122 | |
|
123 | 0 | boolean inBundleClasspathEntry = false; |
124 | 0 | StringBuffer manifestSb = new StringBuffer(); |
125 | |
try |
126 | |
{ |
127 | 0 | BufferedReader in = |
128 | |
new BufferedReader( new InputStreamReader( new FileInputStream( manifestFile ), "UTF-8" ) ); |
129 | |
String line; |
130 | 0 | while ( ( line = in.readLine() ) != null ) |
131 | |
{ |
132 | 0 | if ( inBundleClasspathEntry && line.indexOf( ":" ) > -1 ) |
133 | |
{ |
134 | 0 | inBundleClasspathEntry = false; |
135 | |
} |
136 | 0 | else if ( inBundleClasspathEntry ) |
137 | |
{ |
138 | |
|
139 | 0 | continue; |
140 | |
} |
141 | |
|
142 | |
|
143 | |
|
144 | 0 | String name = line.substring( 0, line.indexOf( ":" ) + 1 ); |
145 | |
|
146 | 0 | if ( name.equalsIgnoreCase( ENTRY_BUNDLE_CLASSPATH ) ) |
147 | |
{ |
148 | 0 | inBundleClasspathEntry = true; |
149 | |
} |
150 | 0 | else if ( name.equalsIgnoreCase( ENTRY_BUNDLE_NAME ) ) |
151 | |
{ |
152 | 0 | manifestSb.append( ENTRY_BUNDLE_NAME ); |
153 | 0 | manifestSb.append( " " ); |
154 | 0 | manifestSb.append( config.getProject().getName() ); |
155 | 0 | manifestSb.append( NEWLINE ); |
156 | |
} |
157 | 0 | else if ( name.equalsIgnoreCase( ENTRY_BUNDLE_SYMBOLICNAME ) ) |
158 | |
{ |
159 | 0 | manifestSb.append( ENTRY_BUNDLE_SYMBOLICNAME ); |
160 | 0 | manifestSb.append( " " ); |
161 | 0 | manifestSb.append( config.getEclipseProjectName() ); |
162 | 0 | manifestSb.append( ";singleton:=true" ); |
163 | 0 | manifestSb.append( NEWLINE ); |
164 | |
} |
165 | 0 | else if ( name.equalsIgnoreCase( ENTRY_BUNDLE_VERSION ) ) |
166 | |
{ |
167 | 0 | manifestSb.append( ENTRY_BUNDLE_VERSION ); |
168 | 0 | manifestSb.append( " " ); |
169 | 0 | manifestSb.append( getNormalizedVersion( config.getProject().getVersion() ) ); |
170 | 0 | manifestSb.append( NEWLINE ); |
171 | |
} |
172 | 0 | else if ( name.equalsIgnoreCase( ENTRY_BUNDLE_VENDOR ) && config.getProject().getOrganization() != null ) |
173 | |
{ |
174 | 0 | manifestSb.append( ENTRY_BUNDLE_VENDOR ); |
175 | 0 | manifestSb.append( " " ); |
176 | 0 | manifestSb.append( config.getProject().getOrganization().getName() ); |
177 | 0 | manifestSb.append( NEWLINE ); |
178 | |
} |
179 | |
else |
180 | |
{ |
181 | 0 | manifestSb.append( line + NEWLINE ); |
182 | |
} |
183 | 0 | } |
184 | |
|
185 | 0 | IOUtil.close( in ); |
186 | |
} |
187 | 0 | catch ( IOException e ) |
188 | |
{ |
189 | 0 | throw new MojoExecutionException( Messages.getString( "EclipsePlugin.cantreadfile", |
190 | |
manifestFile.getAbsolutePath() ) ); |
191 | 0 | } |
192 | 0 | manifestSb.append( addBundleClasspathEntries() ); |
193 | |
|
194 | |
|
195 | 0 | manifestSb.append( NEWLINE ); |
196 | 0 | return manifestSb; |
197 | |
} |
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
protected static String getNormalizedVersion( String version ) |
207 | |
{ |
208 | |
|
209 | 18 | if ( version.endsWith( "-SNAPSHOT" ) ) |
210 | |
{ |
211 | 18 | String[] versionTokens = StringUtils.split( StringUtils.stripEnd( version, "-SNAPSHOT" ), "." ); |
212 | |
|
213 | 18 | int j = 0; |
214 | 18 | StringBuffer newVersion = new StringBuffer( 20 ); |
215 | 126 | for ( ; j < versionTokens.length; j++ ) |
216 | |
{ |
217 | 54 | newVersion.append( versionTokens[j] ); |
218 | 54 | newVersion.append( "." ); |
219 | |
} |
220 | 30 | for ( ; j < 3; j++ ) |
221 | |
{ |
222 | 6 | newVersion.append( "0." ); |
223 | |
} |
224 | |
|
225 | 18 | newVersion.append( "SNAPSHOT" ); |
226 | 18 | version = newVersion.toString(); |
227 | |
|
228 | |
} |
229 | 18 | return version; |
230 | |
} |
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
protected String addBundleClasspathEntries() |
238 | |
{ |
239 | 0 | StringBuffer bundleClasspathSb = new StringBuffer( ENTRY_BUNDLE_CLASSPATH ); |
240 | |
|
241 | |
|
242 | |
|
243 | 0 | bundleClasspathSb.append( " ." ); |
244 | |
|
245 | 0 | IdeDependency[] deps = config.getDepsOrdered(); |
246 | |
|
247 | |
|
248 | |
|
249 | 0 | Arrays.sort( deps ); |
250 | |
|
251 | 0 | for ( int j = 0; j < deps.length; j++ ) |
252 | |
{ |
253 | 0 | IdeDependency dep = deps[j]; |
254 | 0 | if ( !dep.isProvided() && !dep.isReferencedProject() && !dep.isTestDependency() && !dep.isOsgiBundle() ) |
255 | |
{ |
256 | 0 | bundleClasspathSb.append( "," + NEWLINE ); |
257 | |
|
258 | 0 | log.debug( "Adding artifact to manifest: " + dep.getArtifactId() ); |
259 | |
|
260 | 0 | bundleClasspathSb.append( " " + dep.getFile().getName() ); |
261 | |
} |
262 | |
} |
263 | |
|
264 | 0 | return bundleClasspathSb.toString(); |
265 | |
} |
266 | |
|
267 | |
} |