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  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          Properties ajdtSettings = new Properties();
82  
83          IdeDependency[] deps = config.getDeps();
84          int ajdtDepCount = 0;
85          int ajdtWeaveDepCount = 0;
86          for ( int i = 0; i < deps.length; i++ )
87          {
88              if ( deps[i].isAjdtDependency() )
89              {
90                  addDependency( ajdtSettings, deps[i], ASPECT_DEP_PROP, ++ajdtDepCount );
91              }
92  
93              if ( deps[i].isAjdtWeaveDependency() )
94              {
95                  addDependency( ajdtSettings, deps[i], WEAVE_DEP_PROP, ++ajdtWeaveDepCount );
96              }
97          }
98  
99          
100         if ( !ajdtSettings.isEmpty() )
101         {
102             File settingsDir = new File( config.getEclipseProjectDirectory(), DIR_DOT_SETTINGS ); 
103 
104             settingsDir.mkdirs();
105 
106             ajdtSettings.put( PROP_ECLIPSE_PREFERENCES_VERSION, "1" ); 
107 
108             try
109             {
110                 File oldAjdtSettingsFile;
111 
112                 File ajdtSettingsFile = new File( settingsDir, FILE_AJDT_PREFS );
113 
114                 if ( ajdtSettingsFile.exists() )
115                 {
116                     oldAjdtSettingsFile = ajdtSettingsFile;
117 
118                     Properties oldsettings = new Properties();
119                     oldsettings.load( new FileInputStream( oldAjdtSettingsFile ) );
120 
121                     Properties newsettings = (Properties) oldsettings.clone();
122                     newsettings.putAll( ajdtSettings );
123 
124                     if ( !oldsettings.equals( newsettings ) )
125                     {
126                         newsettings.store( new FileOutputStream( ajdtSettingsFile ), null );
127                     }
128                 }
129                 else
130                 {
131                     ajdtSettings.store( new FileOutputStream( ajdtSettingsFile ), null );
132 
133                     log.info( Messages.getString( "EclipseSettingsWriter.wrotesettings", 
134                                                   ajdtSettingsFile.getCanonicalPath() ) );
135                 }
136             }
137             catch ( FileNotFoundException e )
138             {
139                 throw new MojoExecutionException( Messages.getString( "EclipseSettingsWriter.cannotcreatesettings" ), e ); 
140             }
141             catch ( IOException e )
142             {
143                 throw new MojoExecutionException( Messages.getString( "EclipseSettingsWriter.errorwritingsettings" ), e ); 
144             }
145         }
146     }
147 
148     private void addDependency( Properties ajdtSettings, IdeDependency dep, String propName, int index )
149         throws MojoExecutionException
150     {
151 
152         String path;
153 
154         if ( dep.isReferencedProject() && !config.isPde() )
155         {
156             path = "/" + dep.getEclipseProjectName(); 
157         }
158         else if ( dep.isReferencedProject() && config.isPde() )
159         {
160             
161             return;
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                 
188                 
189                 if ( config.isPde() && ( dep.isProvided() || dep.isOsgiBundle() ) )
190                 {
191                     return;
192                 }
193                 else if ( config.isPde() && !dep.isProvided() && !dep.isTestDependency() )
194                 {
195                     
196                     path = dep.getFile().getName();
197                 }
198                 
199                 
200                 else
201                 {
202                     String fullPath = artifactPath.getPath();
203                     String relativePath =
204                         IdeUtils.toRelativeAndFixSeparator( localRepositoryFile, new File( fullPath ), false );
205 
206                     if ( !new File( relativePath ).isAbsolute() )
207                     {
208                         path = EclipseClasspathWriter.M2_REPO + "/" 
209                             + relativePath;
210                     }
211                     else
212                     {
213                         path = relativePath;
214                     }
215                 }
216             }
217         }
218 
219         ajdtSettings.setProperty( AJDT_PROP_PREFIX + propName + CONTENT_KIND + index, BINARY );
220         ajdtSettings.setProperty( AJDT_PROP_PREFIX + propName + ENTRY_KIND + index, LIBRARY );
221         ajdtSettings.setProperty( AJDT_PROP_PREFIX + propName + index, path );
222     }
223 }