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