1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.tools.plugin.extractor.beanshell;
20
21 import javax.inject.Named;
22 import javax.inject.Singleton;
23
24 import java.io.File;
25 import java.io.InputStreamReader;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Set;
30
31 import bsh.EvalError;
32 import bsh.Interpreter;
33 import org.apache.maven.plugin.descriptor.InvalidPluginDescriptorException;
34 import org.apache.maven.plugin.descriptor.MojoDescriptor;
35 import org.apache.maven.tools.plugin.PluginToolsRequest;
36 import org.apache.maven.tools.plugin.extractor.AbstractScriptedMojoDescriptorExtractor;
37 import org.apache.maven.tools.plugin.extractor.ExtractionException;
38 import org.apache.maven.tools.plugin.extractor.GroupKey;
39
40 import static java.nio.charset.StandardCharsets.UTF_8;
41
42
43
44
45
46
47 @Deprecated
48 @Named(BeanshellMojoDescriptorExtractor.NAME)
49 @Singleton
50 public class BeanshellMojoDescriptorExtractor extends AbstractScriptedMojoDescriptorExtractor {
51 public static final String NAME = "bsh";
52
53 private static final GroupKey GROUP_KEY = new GroupKey("bsh", 100);
54
55 @Override
56 public String getName() {
57 return NAME;
58 }
59
60 @Override
61 public GroupKey getGroupKey() {
62 return GROUP_KEY;
63 }
64
65
66
67
68 @Override
69 protected String getScriptFileExtension(PluginToolsRequest request) {
70 return ".bsh";
71 }
72
73
74
75
76 @Override
77 protected List<MojoDescriptor> extractMojoDescriptors(
78 Map<String, Set<File>> scriptFilesKeyedByBasedir, PluginToolsRequest request)
79 throws ExtractionException, InvalidPluginDescriptorException {
80 List<MojoDescriptor> descriptors = new ArrayList<>();
81
82 for (Map.Entry<String, Set<File>> entry : scriptFilesKeyedByBasedir.entrySet()) {
83 String basedir = entry.getKey();
84 Set<File> metadataFiles = entry.getValue();
85
86 for (File scriptFile : metadataFiles) {
87 String relativePath = null;
88
89 if (basedir.endsWith("/")) {
90 basedir = basedir.substring(0, basedir.length() - 2);
91 }
92
93 relativePath = scriptFile.getPath().substring(basedir.length());
94
95 relativePath = relativePath.replace('\\', '/');
96
97 MojoDescriptor mojoDescriptor = createMojoDescriptor(basedir, relativePath, request);
98 descriptors.add(mojoDescriptor);
99 }
100 }
101
102 return descriptors;
103 }
104
105
106
107
108
109
110
111
112
113 private MojoDescriptor createMojoDescriptor(String basedir, String resource, PluginToolsRequest request)
114 throws InvalidPluginDescriptorException {
115 MojoDescriptor mojoDescriptor = new MojoDescriptor();
116 mojoDescriptor.setPluginDescriptor(request.getPluginDescriptor());
117
118 mojoDescriptor.setLanguage("bsh");
119 mojoDescriptor.setComponentConfigurator("bsh");
120
121 mojoDescriptor.setImplementation(resource);
122
123 Interpreter interpreter = new Interpreter();
124
125 try {
126 interpreter.set("file", new File(basedir, resource));
127
128 interpreter.set("mojoDescriptor", mojoDescriptor);
129
130 interpreter.set("encoding", "UTF-8");
131
132 interpreter.eval(new InputStreamReader(getClass().getResourceAsStream("/extractor.bsh"), UTF_8));
133 } catch (EvalError evalError) {
134 throw new InvalidPluginDescriptorException("Error scanning beanshell script", evalError);
135 }
136
137
138 return mojoDescriptor;
139 }
140 }