1 package org.apache.maven.plugin.registry; 2 3 import org.apache.maven.plugin.registry.io.xpp3.PluginRegistryXpp3Reader; 4 import org.codehaus.plexus.logging.AbstractLogEnabled; 5 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable; 6 import org.codehaus.plexus.util.IOUtil; 7 import org.codehaus.plexus.util.StringUtils; 8 import org.codehaus.plexus.util.xml.pull.XmlPullParserException; 9 10 import java.io.File ; 11 import java.io.FileReader ; 12 import java.io.IOException ; 13 14 29 30 public class DefaultPluginRegistryBuilder 31 extends AbstractLogEnabled 32 implements MavenPluginRegistryBuilder, Initializable 33 { 34 35 public static final String userHome = System.getProperty( "user.home" ); 36 37 40 private String userRegistryPath; 41 42 45 private String globalRegistryPath; 46 47 private File userRegistryFile; 48 49 private File globalRegistryFile; 50 51 55 public void initialize() 56 { 57 userRegistryFile = getFile( userRegistryPath, "user.home", MavenPluginRegistryBuilder.ALT_USER_PLUGIN_REG_LOCATION ); 58 59 getLogger().debug( "Building Maven user-level plugin registry from: '" + userRegistryFile.getAbsolutePath() + "'" ); 60 61 if ( System.getProperty( "maven.home" ) != null || 62 System.getProperty( MavenPluginRegistryBuilder.ALT_GLOBAL_PLUGIN_REG_LOCATION ) != null ) 63 { 64 globalRegistryFile = getFile( globalRegistryPath, "maven.home", MavenPluginRegistryBuilder.ALT_GLOBAL_PLUGIN_REG_LOCATION ); 65 66 getLogger().debug( "Building Maven global-level plugin registry from: '" + globalRegistryFile.getAbsolutePath() + "'" ); 67 } 68 } 69 70 public PluginRegistry buildPluginRegistry() 71 throws IOException , XmlPullParserException 72 { 73 PluginRegistry global = readPluginRegistry( globalRegistryFile ); 74 75 PluginRegistry user = readPluginRegistry( userRegistryFile ); 76 77 if ( user == null && global != null ) 78 { 79 PluginRegistryUtils.recursivelySetSourceLevel( global, PluginRegistry.GLOBAL_LEVEL ); 81 82 user = global; 83 } 84 else 85 { 86 PluginRegistryUtils.merge( user, global, TrackableBase.GLOBAL_LEVEL ); 88 } 89 90 return user; 91 } 92 93 private PluginRegistry readPluginRegistry( File registryFile ) 94 throws IOException , XmlPullParserException 95 { 96 PluginRegistry registry = null; 97 98 if ( registryFile != null && registryFile.exists() && registryFile.isFile() ) 99 { 100 FileReader reader = null; 101 try 102 { 103 reader = new FileReader ( registryFile ); 104 105 PluginRegistryXpp3Reader modelReader = new PluginRegistryXpp3Reader(); 106 107 registry = modelReader.read( reader ); 108 109 RuntimeInfo rtInfo = new RuntimeInfo( registry ); 110 111 registry.setRuntimeInfo( rtInfo ); 112 113 rtInfo.setFile( registryFile ); 114 } 115 finally 116 { 117 IOUtil.close( reader ); 118 } 119 } 120 121 return registry; 122 } 123 124 private File getFile( String pathPattern, String basedirSysProp, String altLocationSysProp ) 125 { 126 139 String path = System.getProperty( altLocationSysProp ); 140 141 if ( StringUtils.isEmpty( path ) ) 142 { 143 String basedir = System.getProperty( basedirSysProp ); 146 147 basedir = basedir.replaceAll( "\\\\", "/" ); 148 basedir = basedir.replaceAll("\\$", "\\\\\\$"); 149 150 path = pathPattern.replaceAll( "\\$\\{" + basedirSysProp + "\\}", basedir ); 151 path = path.replaceAll( "\\\\", "/" ); 152 path = path.replaceAll( "//", "/" ); 153 154 return new File ( path ).getAbsoluteFile(); 155 } 156 else 157 { 158 return new File ( path ).getAbsoluteFile(); 159 } 160 } 161 162 public PluginRegistry createUserPluginRegistry() 163 { 164 PluginRegistry registry = new PluginRegistry(); 165 166 RuntimeInfo rtInfo = new RuntimeInfo( registry ); 167 168 registry.setRuntimeInfo( rtInfo ); 169 170 rtInfo.setFile( userRegistryFile ); 171 172 return registry; 173 } 174 175 } 176 | Popular Tags |