下面列出了怎么用org.apache.maven.plugins.shade.relocation.Relocator的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void processResource(String resource, InputStream is, List<Relocator> relocators) throws IOException {
List<String> trandformedLines = new ArrayList<>();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line = reader.readLine();
while (line != null) {
trandformedLines.add(transformLine(line, relocators));
line = reader.readLine();
}
this.transformedResources.put(resource, trandformedLines);
}
private String transformLine(String line, List<Relocator> relocators) {
int splitIndex = line.lastIndexOf('=');
if (splitIndex == -1) {
return line;
}
String key = line.substring(0, splitIndex);
String value = line.substring(splitIndex + 1);
return relocateKey(key, relocators) + "=" + value;
}
private String relocateKey(String key, List<Relocator> relocators) {
for (Relocator relocator : relocators) {
if (relocator.canRelocateClass(key)) {
return relocator.relocateClass(key);
}
}
return key;
}
public void processResource(String resource, InputStream is, List<Relocator> relocators) throws IOException {
final File tempFile = File.createTempFile("Log4j2Plugins", "dat");
FileOutputStream fos = new FileOutputStream(tempFile);
try {
IOUtils.copyLarge(is, fos);
} finally {
IOUtils.closeQuietly(fos);
}
tempFiles.add(tempFile);
if (relocators != null) {
this.relocators.addAll(relocators);
}
}
void relocatePlugin(PluginCache aggregator, List<Relocator> relocators) {
for (Map.Entry<String, Map<String, PluginEntry>> categoryEntry : aggregator.getAllCategories().entrySet()) {
for (Map.Entry<String, PluginEntry> pluginMapEntry : categoryEntry.getValue().entrySet()) {
PluginEntry pluginEntry = pluginMapEntry.getValue();
String originalClassName = pluginEntry.getClassName();
Relocator matchingRelocator = findFirstMatchingRelocator(originalClassName, relocators);
if (matchingRelocator != null) {
String newClassName = matchingRelocator.relocateClass(originalClassName);
pluginEntry.setClassName(newClassName);
}
}
}
}
private Relocator findFirstMatchingRelocator(String originalClassName, List<Relocator> relocators) {
for (Relocator relocator : relocators) {
if (relocator.canRelocateClass(originalClassName)) {
return relocator;
}
}
return null;
}
@Test
public void test() throws Exception {
PluginsCacheFileTransformer t = new PluginsCacheFileTransformer();
final InputStream is = getClass().getClassLoader().getResourceAsStream(PLUGIN_CACHE_FILE);
t.processResource(PLUGIN_CACHE_FILE, is, null);
assertFalse(t.hasTransformedResource());
List<Relocator> relocators = new ArrayList<Relocator>();
relocators.add(new SimpleRelocator(null, null, null, null));
t.processResource(PLUGIN_CACHE_FILE, is, relocators);
assertTrue(t.hasTransformedResource());
}
private void testRelocation(String src, String pattern, String target) throws IOException {
PluginsCacheFileTransformer t = new PluginsCacheFileTransformer();
Relocator log4jRelocator = new SimpleRelocator(src, pattern, null, null);
PluginCache aggregator = new PluginCache();
aggregator.loadCacheFiles(enumeration(singletonList(pluginUrl)));
t.relocatePlugin(aggregator, singletonList(log4jRelocator));
for (Map<String, PluginEntry> pluginEntryMap : aggregator.getAllCategories().values()) {
for (PluginEntry entry : pluginEntryMap.values()) {
assertTrue(entry.getClassName().startsWith(target));
}
}
}
@Override
public void processResource(final String resource, final InputStream is, final List<Relocator> relocators,
final long time) throws IOException {
// no-op
}