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 | 0 | public class DefaultPluginRegistryBuilder |
35 | |
extends AbstractLogEnabled |
36 | |
implements MavenPluginRegistryBuilder, Initializable |
37 | |
{ |
38 | |
|
39 | 0 | 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 | 0 | userRegistryFile = getFile( userRegistryPath, "user.home", MavenPluginRegistryBuilder.ALT_USER_PLUGIN_REG_LOCATION ); |
62 | |
|
63 | 0 | getLogger().debug( "Building Maven user-level plugin registry from: '" + userRegistryFile.getAbsolutePath() + "'" ); |
64 | |
|
65 | 0 | if ( System.getProperty( "maven.home" ) != null || |
66 | |
System.getProperty( MavenPluginRegistryBuilder.ALT_GLOBAL_PLUGIN_REG_LOCATION ) != null ) |
67 | |
{ |
68 | 0 | globalRegistryFile = getFile( globalRegistryPath, "maven.home", MavenPluginRegistryBuilder.ALT_GLOBAL_PLUGIN_REG_LOCATION ); |
69 | |
|
70 | 0 | getLogger().debug( "Building Maven global-level plugin registry from: '" + globalRegistryFile.getAbsolutePath() + "'" ); |
71 | |
} |
72 | 0 | } |
73 | |
|
74 | |
public PluginRegistry buildPluginRegistry() |
75 | |
throws IOException, XmlPullParserException |
76 | |
{ |
77 | 0 | PluginRegistry global = readPluginRegistry( globalRegistryFile ); |
78 | |
|
79 | 0 | PluginRegistry user = readPluginRegistry( userRegistryFile ); |
80 | |
|
81 | 0 | if ( user == null && global != null ) |
82 | |
{ |
83 | |
|
84 | 0 | PluginRegistryUtils.recursivelySetSourceLevel( global, PluginRegistry.GLOBAL_LEVEL ); |
85 | |
|
86 | 0 | user = global; |
87 | |
} |
88 | |
else |
89 | |
{ |
90 | |
|
91 | 0 | PluginRegistryUtils.merge( user, global, TrackableBase.GLOBAL_LEVEL ); |
92 | |
} |
93 | |
|
94 | 0 | return user; |
95 | |
} |
96 | |
|
97 | |
private PluginRegistry readPluginRegistry( File registryFile ) |
98 | |
throws IOException, XmlPullParserException |
99 | |
{ |
100 | 0 | PluginRegistry registry = null; |
101 | |
|
102 | 0 | if ( registryFile != null && registryFile.exists() && registryFile.isFile() ) |
103 | |
{ |
104 | 0 | Reader reader = null; |
105 | |
try |
106 | |
{ |
107 | 0 | reader = ReaderFactory.newXmlReader( registryFile ); |
108 | |
|
109 | 0 | PluginRegistryXpp3Reader modelReader = new PluginRegistryXpp3Reader(); |
110 | |
|
111 | 0 | registry = modelReader.read( reader ); |
112 | |
|
113 | 0 | RuntimeInfo rtInfo = new RuntimeInfo( registry ); |
114 | |
|
115 | 0 | registry.setRuntimeInfo( rtInfo ); |
116 | |
|
117 | 0 | rtInfo.setFile( registryFile ); |
118 | |
} |
119 | |
finally |
120 | |
{ |
121 | 0 | IOUtil.close( reader ); |
122 | 0 | } |
123 | |
} |
124 | |
|
125 | 0 | 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 | 0 | String path = System.getProperty( altLocationSysProp ); |
144 | |
|
145 | 0 | if ( StringUtils.isEmpty( path ) ) |
146 | |
{ |
147 | |
|
148 | |
|
149 | 0 | String basedir = System.getProperty( basedirSysProp ); |
150 | |
|
151 | 0 | basedir = basedir.replaceAll( "\\\\", "/" ); |
152 | 0 | basedir = basedir.replaceAll("\\$", "\\\\\\$"); |
153 | |
|
154 | 0 | path = pathPattern.replaceAll( "\\$\\{" + basedirSysProp + "\\}", basedir ); |
155 | 0 | path = path.replaceAll( "\\\\", "/" ); |
156 | 0 | path = path.replaceAll( "//", "/" ); |
157 | |
|
158 | 0 | return new File( path ).getAbsoluteFile(); |
159 | |
} |
160 | |
else |
161 | |
{ |
162 | 0 | return new File( path ).getAbsoluteFile(); |
163 | |
} |
164 | |
} |
165 | |
|
166 | |
public PluginRegistry createUserPluginRegistry() |
167 | |
{ |
168 | 0 | PluginRegistry registry = new PluginRegistry(); |
169 | |
|
170 | 0 | RuntimeInfo rtInfo = new RuntimeInfo( registry ); |
171 | |
|
172 | 0 | registry.setRuntimeInfo( rtInfo ); |
173 | |
|
174 | 0 | rtInfo.setFile( userRegistryFile ); |
175 | |
|
176 | 0 | return registry; |
177 | |
} |
178 | |
|
179 | |
} |