Coverage Report - org.apache.maven.plugin.eclipse.AddMavenRepoMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
AddMavenRepoMojo
100%
19/19
N/A
2.4
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  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,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied.  See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 package org.apache.maven.plugin.eclipse;
 20  
 
 21  
 import java.io.File;
 22  
 import java.io.FileInputStream;
 23  
 import java.io.FileNotFoundException;
 24  
 import java.io.FileOutputStream;
 25  
 import java.io.IOException;
 26  
 import java.io.OutputStream;
 27  
 import java.util.Properties;
 28  
 
 29  
 import org.apache.maven.artifact.repository.ArtifactRepository;
 30  
 import org.apache.maven.plugin.AbstractMojo;
 31  
 import org.apache.maven.plugin.MojoExecutionException;
 32  
 
 33  
 /**
 34  
  * Adds the classpath variable MAVEN_REPO to Eclipse.
 35  
  * 
 36  
  * @goal add-maven-repo
 37  
  * @requiresProject false
 38  
  */
 39  1
 public class AddMavenRepoMojo
 40  
     extends AbstractMojo
 41  
 {
 42  
 
 43  
     /**
 44  
      * Path under Eclipse workspace where Eclipse Plugin metadata/config is
 45  
      * stored.
 46  
      */
 47  
     public static final String DIR_ECLIPSE_PLUGINS_METADATA = ".metadata/.plugins"; //$NON-NLS-1$
 48  
 
 49  
     /**
 50  
      * Path under {@value #DIR_ECLIPSE_PLUGINS_METADATA } folder where Eclipse
 51  
      * Workspace Runtime settings are stored.
 52  
      */
 53  
     public static final String DIR_ECLIPSE_CORE_RUNTIME_SETTINGS = DIR_ECLIPSE_PLUGINS_METADATA
 54  
         + "/org.eclipse.core.runtime/.settings"; //$NON-NLS-1$
 55  
 
 56  
     /**
 57  
      * File that stores the Eclipse JDT Core preferences.
 58  
      */
 59  
     public static final String FILE_ECLIPSE_JDT_CORE_PREFS = "org.eclipse.jdt.core.prefs"; //$NON-NLS-1$
 60  
 
 61  
     /**
 62  
      * Property constant under which Variable 'M2_REPO' is setup.
 63  
      */
 64  
     public static final String CLASSPATH_VARIABLE_M2_REPO = "org.eclipse.jdt.core.classpathVariable.M2_REPO"; //$NON-NLS-1$
 65  
 
 66  
     /**
 67  
      * Location of the <code>Eclipse</code> workspace that holds your
 68  
      * configuration and source. On Windows, this will be the
 69  
      * <code>workspace</code> directory under your eclipse installation. For
 70  
      * example, if you installed eclipse into <code>c:\eclipse</code>, the
 71  
      * workspace is <code>c:\eclipse\workspace</code>.
 72  
      * 
 73  
      * @parameter expression="${eclipse.workspace}"
 74  
      * @required
 75  
      */
 76  
     private String workspace;
 77  
 
 78  
     /**
 79  
      * @parameter expression="${localRepository}"
 80  
      * @required
 81  
      * @readonly
 82  
      */
 83  
     private ArtifactRepository localRepository;
 84  
 
 85  
     public void execute()
 86  
         throws MojoExecutionException
 87  
     {
 88  
 
 89  1
         File workDir = new File( workspace, DIR_ECLIPSE_CORE_RUNTIME_SETTINGS );
 90  1
         workDir.mkdirs();
 91  
 
 92  1
         Properties props = new Properties();
 93  
 
 94  1
         File f = new File( workDir, FILE_ECLIPSE_JDT_CORE_PREFS );
 95  
 
 96  
         // preserve old settings
 97  1
         if ( f.exists() )
 98  
         {
 99  
             try
 100  
             {
 101  1
                 props.load( new FileInputStream( f ) );
 102  
             }
 103  
             catch ( FileNotFoundException e )
 104  
             {
 105  
                 throw new MojoExecutionException( Messages
 106  
                     .getString( "EclipsePlugin.cantreadfile", f.getAbsolutePath() ), e ); //$NON-NLS-1$
 107  
             }
 108  
             catch ( IOException e )
 109  
             {
 110  
                 throw new MojoExecutionException( Messages
 111  
                     .getString( "EclipsePlugin.cantreadfile", f.getAbsolutePath() ), e ); //$NON-NLS-1$
 112  1
             }
 113  
         }
 114  
 
 115  1
         props.put( CLASSPATH_VARIABLE_M2_REPO, localRepository.getBasedir() ); //$NON-NLS-1$  //$NON-NLS-2$
 116  
 
 117  
         try
 118  
         {
 119  1
             OutputStream os = new FileOutputStream( f );
 120  1
             props.store( os, null );
 121  1
             os.close();
 122  
         }
 123  
         catch ( IOException ioe )
 124  
         {
 125  
             throw new MojoExecutionException( Messages.getString( "EclipsePlugin.cantwritetofile", //$NON-NLS-1$
 126  
                                                                   f.getAbsolutePath() ) );
 127  1
         }
 128  1
     }
 129  
 
 130  
     public ArtifactRepository getLocalRepository()
 131  
     {
 132  
         return localRepository;
 133  
     }
 134  
 
 135  
     public void setLocalRepository( ArtifactRepository localRepository )
 136  
     {
 137  1
         this.localRepository = localRepository;
 138  1
     }
 139  
 
 140  
     public String getWorkspace()
 141  
     {
 142  1
         return workspace;
 143  
     }
 144  
 
 145  
     public void setWorkspace( String workspace )
 146  
     {
 147  1
         this.workspace = workspace;
 148  1
     }
 149  
 }