View Javadoc
1   package org.apache.maven.plugin.eclipse.writers;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
36   * @author <a href="mailto:kenney@neonics.com">Kenney Westerhof</a>
37   * @author <a href="mailto:fgiust@users.sourceforge.net">Fabrizio Giustina</a>
38   * @version $Id$
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"; //$NON-NLS-1$
65  
66      private static final String PROP_ECLIPSE_PREFERENCES_VERSION = "eclipse.preferences.version"; //$NON-NLS-1$
67  
68      private static final String DIR_DOT_SETTINGS = ".settings"; //$NON-NLS-1$
69  
70      private static final String AJDT_PROP_PREFIX = "org.eclipse.ajdt.ui."; //$NON-NLS-1$
71  
72      private static final String ASPECT_DEP_PROP = "aspectPath";
73  
74      private static final String WEAVE_DEP_PROP = "inPath";
75  
76      /**
77       * @see org.apache.maven.plugin.eclipse.writers.EclipseWriter#write()
78       */
79      public void write()
80          throws MojoExecutionException
81      {
82  
83          // check if it's necessary to create project specific settings
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         // write the settings, if needed
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" ); //$NON-NLS-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(); //$NON-NLS-1$
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 }