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 (IdeDependency dep : deps) {
87              if (dep.isAjdtDependency()) {
88                  addDependency(ajdtSettings, dep, ASPECT_DEP_PROP, ++ajdtDepCount);
89              }
90  
91              if (dep.isAjdtWeaveDependency()) {
92                  addDependency(ajdtSettings, dep, WEAVE_DEP_PROP, ++ajdtWeaveDepCount);
93              }
94          }
95  
96          
97          if ( !ajdtSettings.isEmpty() )
98          {
99              File settingsDir = new File( config.getEclipseProjectDirectory(), DIR_DOT_SETTINGS ); 
100 
101             settingsDir.mkdirs();
102 
103             ajdtSettings.put( PROP_ECLIPSE_PREFERENCES_VERSION, "1" ); 
104 
105             try
106             {
107                 File oldAjdtSettingsFile;
108 
109                 File ajdtSettingsFile = new File( settingsDir, FILE_AJDT_PREFS );
110 
111                 if ( ajdtSettingsFile.exists() )
112                 {
113                     oldAjdtSettingsFile = ajdtSettingsFile;
114 
115                     Properties oldsettings = new Properties();
116                     oldsettings.load( new FileInputStream( oldAjdtSettingsFile ) );
117 
118                     Properties newsettings = (Properties) oldsettings.clone();
119                     newsettings.putAll( ajdtSettings );
120 
121                     if ( !oldsettings.equals( newsettings ) )
122                     {
123                         newsettings.store( new FileOutputStream( ajdtSettingsFile ), null );
124                     }
125                 }
126                 else
127                 {
128                     ajdtSettings.store( new FileOutputStream( ajdtSettingsFile ), null );
129 
130                     log.info( Messages.getString( "EclipseSettingsWriter.wrotesettings", 
131                                                   ajdtSettingsFile.getCanonicalPath() ) );
132                 }
133             }
134             catch ( FileNotFoundException e )
135             {
136                 throw new MojoExecutionException( Messages.getString( "EclipseSettingsWriter.cannotcreatesettings" ), e ); 
137             }
138             catch ( IOException e )
139             {
140                 throw new MojoExecutionException( Messages.getString( "EclipseSettingsWriter.errorwritingsettings" ), e ); 
141             }
142         }
143     }
144 
145     private void addDependency( Properties ajdtSettings, IdeDependency dep, String propName, int index )
146         throws MojoExecutionException
147     {
148 
149         String path;
150 
151         if ( dep.isReferencedProject() )
152         {
153             path = "/" + dep.getEclipseProjectName(); 
154         }
155         else
156         {
157             File artifactPath = dep.getFile();
158 
159             if ( artifactPath == null )
160             {
161                 log.error( Messages.getString( "EclipsePlugin.artifactpathisnull", dep.getId() ) ); 
162                 return;
163             }
164 
165             if ( dep.isSystemScoped() )
166             {
167                 path = IdeUtils.toRelativeAndFixSeparator( config.getEclipseProjectDirectory(), artifactPath, false );
168 
169                 if ( log.isDebugEnabled() )
170                 {
171                     log.debug( Messages.getString( "EclipsePlugin.artifactissystemscoped", 
172                                                    new Object[] { dep.getArtifactId(), path } ) );
173                 }
174             }
175             else
176             {
177                 File localRepositoryFile = new File( config.getLocalRepository().getBasedir() );
178 
179                 String fullPath = artifactPath.getPath();
180                 String relativePath =
181                     IdeUtils.toRelativeAndFixSeparator( localRepositoryFile, new File( fullPath ), false );
182 
183                 if ( !new File( relativePath ).isAbsolute() )
184                 {
185                     path = EclipseClasspathWriter.M2_REPO + "/" 
186                         + relativePath;
187                 }
188                 else
189                 {
190                     path = relativePath;
191                 }
192             }
193         }
194 
195         ajdtSettings.setProperty( AJDT_PROP_PREFIX + propName + CONTENT_KIND + index, BINARY );
196         ajdtSettings.setProperty( AJDT_PROP_PREFIX + propName + ENTRY_KIND + index, LIBRARY );
197         ajdtSettings.setProperty( AJDT_PROP_PREFIX + propName + index, path );
198     }
199 }