1 | |
package org.apache.maven.plugin.eclipse.writers; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
import java.io.File; |
20 | |
import java.io.FileInputStream; |
21 | |
import java.io.FileNotFoundException; |
22 | |
import java.io.FileOutputStream; |
23 | |
import java.io.IOException; |
24 | |
import java.util.Properties; |
25 | |
|
26 | |
import org.apache.maven.plugin.MojoExecutionException; |
27 | |
import org.apache.maven.plugin.eclipse.Messages; |
28 | |
import org.apache.maven.plugin.ide.IdeDependency; |
29 | |
import org.apache.maven.plugin.ide.IdeUtils; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | 0 | public class EclipseAjdtWriter |
38 | |
extends AbstractEclipseWriter |
39 | |
{ |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
private static final String LIBRARY = "LIBRARY"; |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
private static final String BINARY = "BINARY"; |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
private static final String CONTENT_KIND = ".contentKind"; |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
private static final String ENTRY_KIND = ".entryKind"; |
60 | |
|
61 | |
private static final String FILE_AJDT_PREFS = "org.eclipse.ajdt.ui.prefs"; |
62 | |
|
63 | |
private static final String PROP_ECLIPSE_PREFERENCES_VERSION = "eclipse.preferences.version"; |
64 | |
|
65 | |
private static final String DIR_DOT_SETTINGS = ".settings"; |
66 | |
|
67 | |
private static final String AJDT_PROP_PREFIX = "org.eclipse.ajdt.ui."; |
68 | |
|
69 | |
private static final String ASPECT_DEP_PROP = "aspectPath"; |
70 | |
|
71 | |
private static final String WEAVE_DEP_PROP = "inPath"; |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
public void write() |
77 | |
throws MojoExecutionException |
78 | |
{ |
79 | |
|
80 | |
|
81 | 0 | Properties ajdtSettings = new Properties(); |
82 | |
|
83 | 0 | IdeDependency[] deps = config.getDeps(); |
84 | 0 | int ajdtDepCount = 0; |
85 | 0 | int ajdtWeaveDepCount = 0; |
86 | 0 | for ( int i = 0; i < deps.length; i++ ) |
87 | |
{ |
88 | 0 | if ( deps[i].isAjdtDependency() ) |
89 | |
{ |
90 | 0 | addDependency( ajdtSettings, deps[i], ASPECT_DEP_PROP, ++ajdtDepCount ); |
91 | |
} |
92 | |
|
93 | 0 | if ( deps[i].isAjdtWeaveDependency() ) |
94 | |
{ |
95 | 0 | addDependency( ajdtSettings, deps[i], WEAVE_DEP_PROP, ++ajdtWeaveDepCount ); |
96 | |
} |
97 | |
} |
98 | |
|
99 | |
|
100 | 0 | if ( !ajdtSettings.isEmpty() ) |
101 | |
{ |
102 | 0 | File settingsDir = new File( config.getEclipseProjectDirectory(), DIR_DOT_SETTINGS ); |
103 | |
|
104 | 0 | settingsDir.mkdirs(); |
105 | |
|
106 | 0 | ajdtSettings.put( PROP_ECLIPSE_PREFERENCES_VERSION, "1" ); |
107 | |
|
108 | |
try |
109 | |
{ |
110 | |
File oldAjdtSettingsFile; |
111 | |
|
112 | 0 | File ajdtSettingsFile = new File( settingsDir, FILE_AJDT_PREFS ); |
113 | |
|
114 | 0 | if ( ajdtSettingsFile.exists() ) |
115 | |
{ |
116 | 0 | oldAjdtSettingsFile = ajdtSettingsFile; |
117 | |
|
118 | 0 | Properties oldsettings = new Properties(); |
119 | 0 | oldsettings.load( new FileInputStream( oldAjdtSettingsFile ) ); |
120 | |
|
121 | 0 | Properties newsettings = (Properties) oldsettings.clone(); |
122 | 0 | newsettings.putAll( ajdtSettings ); |
123 | |
|
124 | 0 | if ( !oldsettings.equals( newsettings ) ) |
125 | |
{ |
126 | 0 | newsettings.store( new FileOutputStream( ajdtSettingsFile ), null ); |
127 | |
} |
128 | |
} |
129 | |
else |
130 | |
{ |
131 | 0 | ajdtSettings.store( new FileOutputStream( ajdtSettingsFile ), null ); |
132 | |
|
133 | 0 | log.info( Messages.getString( "EclipseSettingsWriter.wrotesettings", |
134 | |
ajdtSettingsFile.getCanonicalPath() ) ); |
135 | |
} |
136 | |
} |
137 | 0 | catch ( FileNotFoundException e ) |
138 | |
{ |
139 | 0 | throw new MojoExecutionException( Messages.getString( "EclipseSettingsWriter.cannotcreatesettings" ), e ); |
140 | |
} |
141 | 0 | catch ( IOException e ) |
142 | |
{ |
143 | 0 | throw new MojoExecutionException( Messages.getString( "EclipseSettingsWriter.errorwritingsettings" ), e ); |
144 | 0 | } |
145 | |
} |
146 | 0 | } |
147 | |
|
148 | |
private void addDependency( Properties ajdtSettings, IdeDependency dep, String propName, int index ) |
149 | |
throws MojoExecutionException |
150 | |
{ |
151 | |
|
152 | |
String path; |
153 | |
|
154 | 0 | if ( dep.isReferencedProject() && !config.isPde() ) |
155 | |
{ |
156 | 0 | path = "/" + dep.getEclipseProjectName(); |
157 | |
} |
158 | 0 | else if ( dep.isReferencedProject() && config.isPde() ) |
159 | |
{ |
160 | |
|
161 | 0 | return; |
162 | |
} |
163 | |
else |
164 | |
{ |
165 | 0 | File artifactPath = dep.getFile(); |
166 | |
|
167 | 0 | if ( artifactPath == null ) |
168 | |
{ |
169 | 0 | log.error( Messages.getString( "EclipsePlugin.artifactpathisnull", dep.getId() ) ); |
170 | 0 | return; |
171 | |
} |
172 | |
|
173 | 0 | if ( dep.isSystemScoped() ) |
174 | |
{ |
175 | 0 | path = IdeUtils.toRelativeAndFixSeparator( config.getEclipseProjectDirectory(), artifactPath, false ); |
176 | |
|
177 | 0 | if ( log.isDebugEnabled() ) |
178 | |
{ |
179 | 0 | log.debug( Messages.getString( "EclipsePlugin.artifactissystemscoped", |
180 | |
new Object[] { dep.getArtifactId(), path } ) ); |
181 | |
} |
182 | |
} |
183 | |
else |
184 | |
{ |
185 | 0 | File localRepositoryFile = new File( config.getLocalRepository().getBasedir() ); |
186 | |
|
187 | |
|
188 | |
|
189 | 0 | if ( config.isPde() && ( dep.isProvided() || dep.isOsgiBundle() ) ) |
190 | |
{ |
191 | 0 | return; |
192 | |
} |
193 | 0 | else if ( config.isPde() && !dep.isProvided() && !dep.isTestDependency() ) |
194 | |
{ |
195 | |
|
196 | 0 | path = dep.getFile().getName(); |
197 | |
} |
198 | |
|
199 | |
|
200 | |
else |
201 | |
{ |
202 | 0 | String fullPath = artifactPath.getPath(); |
203 | 0 | String relativePath = |
204 | |
IdeUtils.toRelativeAndFixSeparator( localRepositoryFile, new File( fullPath ), false ); |
205 | |
|
206 | 0 | if ( !new File( relativePath ).isAbsolute() ) |
207 | |
{ |
208 | 0 | path = EclipseClasspathWriter.M2_REPO + "/" |
209 | |
+ relativePath; |
210 | |
} |
211 | |
else |
212 | |
{ |
213 | 0 | path = relativePath; |
214 | |
} |
215 | |
} |
216 | |
} |
217 | |
} |
218 | |
|
219 | 0 | ajdtSettings.setProperty( AJDT_PROP_PREFIX + propName + CONTENT_KIND + index, BINARY ); |
220 | 0 | ajdtSettings.setProperty( AJDT_PROP_PREFIX + propName + ENTRY_KIND + index, LIBRARY ); |
221 | 0 | ajdtSettings.setProperty( AJDT_PROP_PREFIX + propName + index, path ); |
222 | 0 | } |
223 | |
} |