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 Properties();
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 Properties();
122 oldsettings.load( new FileInputStream( oldAjdtSettingsFile ) );
123
124 Properties newsettings = (Properties) oldsettings.clone();
125 newsettings.putAll( ajdtSettings );
126
127 if ( !oldsettings.equals( newsettings ) )
128 {
129 newsettings.store( new FileOutputStream( ajdtSettingsFile ), null );
130 }
131 }
132 else
133 {
134 ajdtSettings.store( new FileOutputStream( ajdtSettingsFile ), null );
135
136 log.info( Messages.getString( "EclipseSettingsWriter.wrotesettings",
137 ajdtSettingsFile.getCanonicalPath() ) );
138 }
139 }
140 catch ( FileNotFoundException e )
141 {
142 throw new MojoExecutionException(
143 Messages.getString( "EclipseSettingsWriter.cannotcreatesettings" ), e );
144 }
145 catch ( IOException e )
146 {
147 throw new MojoExecutionException(
148 Messages.getString( "EclipseSettingsWriter.errorwritingsettings" ), e );
149 }
150 }
151 }
152
153 private void addDependency( Properties ajdtSettings, IdeDependency dep, String propName, int index )
154 throws MojoExecutionException
155 {
156
157 String path;
158
159 if ( dep.isReferencedProject() )
160 {
161 path = "/" + dep.getEclipseProjectName();
162 }
163 else
164 {
165 File artifactPath = dep.getFile();
166
167 if ( artifactPath == null )
168 {
169 log.error( Messages.getString( "EclipsePlugin.artifactpathisnull", dep.getId() ) );
170 return;
171 }
172
173 if ( dep.isSystemScoped() )
174 {
175 path = IdeUtils.toRelativeAndFixSeparator( config.getEclipseProjectDirectory(), artifactPath, false );
176
177 if ( log.isDebugEnabled() )
178 {
179 log.debug( Messages.getString( "EclipsePlugin.artifactissystemscoped",
180 new Object[] { dep.getArtifactId(), path } ) );
181 }
182 }
183 else
184 {
185 File localRepositoryFile = new File( config.getLocalRepository().getBasedir() );
186
187 String fullPath = artifactPath.getPath();
188 String relativePath =
189 IdeUtils.toRelativeAndFixSeparator( localRepositoryFile, new File( fullPath ), false );
190
191 if ( !new File( relativePath ).isAbsolute() )
192 {
193 path = EclipseClasspathWriter.M2_REPO + "/"
194 + relativePath;
195 }
196 else
197 {
198 path = relativePath;
199 }
200 }
201 }
202
203 ajdtSettings.setProperty( AJDT_PROP_PREFIX + propName + CONTENT_KIND + index, BINARY );
204 ajdtSettings.setProperty( AJDT_PROP_PREFIX + propName + ENTRY_KIND + index, LIBRARY );
205 ajdtSettings.setProperty( AJDT_PROP_PREFIX + propName + index, path );
206 }
207 }