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