View Javadoc

1   package org.apache.maven.plugin.eclipse.writers.workspace;
2   
3   import java.io.File;
4   import java.io.FileInputStream;
5   import java.io.FileNotFoundException;
6   import java.io.FileOutputStream;
7   import java.io.IOException;
8   import java.io.OutputStream;
9   import java.util.Properties;
10  
11  import org.apache.maven.plugin.MojoExecutionException;
12  import org.apache.maven.plugin.eclipse.Messages;
13  import org.apache.maven.plugin.eclipse.WorkspaceConfiguration;
14  import org.apache.maven.plugin.logging.Log;
15  import org.codehaus.plexus.util.IOUtil;
16  
17  public class EclipseWorkspaceWriter
18      implements WorkspaceWriter
19  {
20  
21      /**
22       * Path under Eclipse workspace where Eclipse Plugin metadata/config is stored.
23       */
24      public static final String ECLIPSE_PLUGINS_METADATA_DIR = ".metadata/.plugins"; //$NON-NLS-1$
25  
26      /**
27       * Path under {@value #ECLIPSE_PLUGINS_METADATA_DIR } folder where Eclipse Workspace Runtime settings are stored.
28       */
29      public static final String ECLIPSE_CORE_RUNTIME_SETTINGS_DIR =
30          ECLIPSE_PLUGINS_METADATA_DIR + "/org.eclipse.core.runtime/.settings"; //$NON-NLS-1$
31  
32      /**
33       * File that stores the Eclipse JDT Core preferences.
34       */
35      public static final String ECLIPSE_JDT_CORE_PREFS_FILE = "org.eclipse.jdt.core.prefs"; //$NON-NLS-1$
36  
37      /**
38       * Property constant under which Variable 'M2_REPO' is setup.
39       */
40      public static final String CLASSPATH_VARIABLE_M2_REPO = "org.eclipse.jdt.core.classpathVariable.M2_REPO"; //$NON-NLS-1$
41  
42      /**
43       * File that stores the Eclipse JDT UI preferences.
44       */
45      public static final String ECLIPSE_JDT_UI_PREFS_FILE = "org.eclipse.jdt.ui.prefs"; //$NON-NLS-1$
46  
47      private WorkspaceConfiguration config;
48  
49      private Log logger;
50  
51      private File workDir;
52  
53      public WorkspaceWriter init( Log logger, WorkspaceConfiguration config )
54      {
55          this.logger = logger;
56          this.config = config;
57  
58          workDir = new File( config.getWorkspaceDirectory(), ECLIPSE_CORE_RUNTIME_SETTINGS_DIR );
59          workDir.mkdirs();
60  
61          return this;
62      }
63  
64      public void write()
65          throws MojoExecutionException
66      {
67          this.writeLocalRepositoryConfiguration();
68  
69          if ( config.getCodeStylesURL() != null )
70          {
71              this.writeCodeStyleConfiguration();
72          }
73      }
74  
75      private void writeCodeStyleConfiguration()
76          throws MojoExecutionException
77      {
78          File f = new File( workDir, ECLIPSE_JDT_UI_PREFS_FILE );
79  
80          Properties props = loadProperties( f );
81  
82          EclipseCodeFormatterProfile codeFormatter =
83              new EclipseCodeFormatterProfile().init( config.getCodeStylesURL(), config.getActiveStyleProfileName() );
84  
85          if ( codeFormatter.getProfileName() != null )
86          {
87              logger.info( "Set active code style profile name: " + codeFormatter.getProfileName() );
88              props.setProperty( "formatter_profile", "_" + codeFormatter.getProfileName() );
89          }
90  
91          props.setProperty( "org.eclipse.jdt.ui.formatterprofiles", codeFormatter.getContent() );
92  
93          storeProperties( props, f );
94      }
95  
96      private void writeLocalRepositoryConfiguration()
97          throws MojoExecutionException
98      {
99          File f = new File( workDir, ECLIPSE_JDT_CORE_PREFS_FILE );
100 
101         Properties props = loadProperties( f );
102 
103         props.put( CLASSPATH_VARIABLE_M2_REPO, config.getLocalRepository().getBasedir() ); //$NON-NLS-1$  //$NON-NLS-2$
104 
105         storeProperties( props, f );
106     }
107 
108     private static Properties loadProperties( File f )
109         throws MojoExecutionException
110     {
111         Properties props = new Properties();
112 
113         // preserve old settings
114         if ( f.exists() )
115         {
116             try
117             {
118                 props.load( new FileInputStream( f ) );
119             }
120             catch ( FileNotFoundException e )
121             {
122                 throw new MojoExecutionException(
123                                                   Messages.getString( "EclipsePlugin.cantreadfile", f.getAbsolutePath() ), e ); //$NON-NLS-1$
124             }
125             catch ( IOException e )
126             {
127                 throw new MojoExecutionException(
128                                                   Messages.getString( "EclipsePlugin.cantreadfile", f.getAbsolutePath() ), e ); //$NON-NLS-1$
129             }
130         }
131 
132         return props;
133     }
134 
135     private static void storeProperties( Properties props, File f )
136         throws MojoExecutionException
137     {
138         OutputStream os = null;
139 
140         try
141         {
142             os = new FileOutputStream( f );
143             props.store( os, null );
144         }
145         catch ( IOException ioe )
146         {
147             throw new MojoExecutionException( Messages.getString( "EclipsePlugin.cantwritetofile", //$NON-NLS-1$
148                                                                   f.getAbsolutePath() ) );
149         }
150         finally
151         {
152             IOUtil.close( os );
153         }
154     }
155 }