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 1672304 2015-04-09 12:04:28Z khmarbaise $
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 (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          // write the settings, if needed
97          if ( !ajdtSettings.isEmpty() )
98          {
99              File settingsDir = new File( config.getEclipseProjectDirectory(), DIR_DOT_SETTINGS ); //$NON-NLS-1$
100 
101             settingsDir.mkdirs();
102 
103             ajdtSettings.put( PROP_ECLIPSE_PREFERENCES_VERSION, "1" ); //$NON-NLS-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", //$NON-NLS-1$
131                                                   ajdtSettingsFile.getCanonicalPath() ) );
132                 }
133             }
134             catch ( FileNotFoundException e )
135             {
136                 throw new MojoExecutionException( Messages.getString( "EclipseSettingsWriter.cannotcreatesettings" ), e ); //$NON-NLS-1$
137             }
138             catch ( IOException e )
139             {
140                 throw new MojoExecutionException( Messages.getString( "EclipseSettingsWriter.errorwritingsettings" ), e ); //$NON-NLS-1$
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(); //$NON-NLS-1$
154         }
155         else
156         {
157             File artifactPath = dep.getFile();
158 
159             if ( artifactPath == null )
160             {
161                 log.error( Messages.getString( "EclipsePlugin.artifactpathisnull", dep.getId() ) ); //$NON-NLS-1$
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", //$NON-NLS-1$
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 + "/" //$NON-NLS-1$
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 }