View Javadoc

1   package org.apache.maven.plugin.eclipse.writers;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
33   * @author <a href="mailto:kenney@neonics.com">Kenney Westerhof</a>
34   * @author <a href="mailto:fgiust@users.sourceforge.net">Fabrizio Giustina</a>
35   * @version $Id: EclipseAjdtWriter.java 691425 2008-09-02 23:17:03Z aheritier $
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"; //$NON-NLS-1$
62  
63      private static final String PROP_ECLIPSE_PREFERENCES_VERSION = "eclipse.preferences.version"; //$NON-NLS-1$
64  
65      private static final String DIR_DOT_SETTINGS = ".settings"; //$NON-NLS-1$
66  
67      private static final String AJDT_PROP_PREFIX = "org.eclipse.ajdt.ui."; //$NON-NLS-1$
68  
69      private static final String ASPECT_DEP_PROP = "aspectPath";
70  
71      private static final String WEAVE_DEP_PROP = "inPath";
72  
73      /**
74       * @see org.apache.maven.plugin.eclipse.writers.EclipseWriter#write()
75       */
76      public void write()
77          throws MojoExecutionException
78      {
79  
80          // check if it's necessary to create project specific settings
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          // write the settings, if needed
100         if ( !ajdtSettings.isEmpty() )
101         {
102             File settingsDir = new File( config.getEclipseProjectDirectory(), DIR_DOT_SETTINGS ); //$NON-NLS-1$
103 
104             settingsDir.mkdirs();
105 
106             ajdtSettings.put( PROP_ECLIPSE_PREFERENCES_VERSION, "1" ); //$NON-NLS-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", //$NON-NLS-1$
134                                                   ajdtSettingsFile.getCanonicalPath() ) );
135                 }
136             }
137             catch ( FileNotFoundException e )
138             {
139                 throw new MojoExecutionException( Messages.getString( "EclipseSettingsWriter.cannotcreatesettings" ), e ); //$NON-NLS-1$
140             }
141             catch ( IOException e )
142             {
143                 throw new MojoExecutionException( Messages.getString( "EclipseSettingsWriter.errorwritingsettings" ), e ); //$NON-NLS-1$
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(); //$NON-NLS-1$
157         }
158         else if ( dep.isReferencedProject() && config.isPde() )
159         {
160             // don't do anything, referenced projects are automatically handled by eclipse in PDE builds
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() ) ); //$NON-NLS-1$
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", //$NON-NLS-1$
180                                                    new Object[] { dep.getArtifactId(), path } ) );
181                 }
182             }
183             else
184             {
185                 File localRepositoryFile = new File( config.getLocalRepository().getBasedir() );
186 
187                 // if the dependency is not provided and the plugin runs in "pde mode", the dependency is
188                 // added to the Bundle-Classpath:
189                 if ( config.isPde() && ( dep.isProvided() || dep.isOsgiBundle() ) )
190                 {
191                     return;
192                 }
193                 else if ( config.isPde() && !dep.isProvided() && !dep.isTestDependency() )
194                 {
195                     // path for link created in .project, not to the actual file
196                     path = dep.getFile().getName();
197                 }
198                 // running in PDE mode and the dependency is provided means, that it is provided by
199                 // the target platform. This case is covered by adding the plugin container
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 + "/" //$NON-NLS-1$
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 }