Skip to content

Commit 830c305

Browse files
committed
Cache maven.config and support reading the (global) settings path
Currently the maven.config is read and parsed on each access, this now adds a caching stage to the MavenProperties that checks if the file actually has changed and if not just use the local copy. Signed-off-by: Christoph Läubrich <[email protected]>
1 parent f201544 commit 830c305

File tree

3 files changed

+111
-30
lines changed

3 files changed

+111
-30
lines changed

org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/embedder/MavenProperties.java

Lines changed: 101 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@
2929
import java.util.ArrayList;
3030
import java.util.Date;
3131
import java.util.List;
32+
import java.util.Map;
33+
import java.util.Optional;
3234
import java.util.Properties;
3335
import java.util.StringTokenizer;
36+
import java.util.concurrent.ConcurrentHashMap;
3437
import java.util.function.BiConsumer;
3538
import java.util.function.Consumer;
3639

@@ -73,14 +76,63 @@ public class MavenProperties {
7376

7477
private static String mavenBuildVersion;
7578

79+
private static Map<File, MavenProperties> mavenProperties = new ConcurrentHashMap<>();
80+
81+
private File configFile;
82+
83+
private CommandLine commandline;
84+
85+
private long lastModified;
86+
87+
private long lastSize;
88+
7689
static {
7790
Properties properties = getMavenRuntimeProperties();
7891
mavenVersion = properties.getProperty(BUILD_VERSION_PROPERTY, BUILD_VERSION_UNKNOWN_PROPERTY);
7992
mavenBuildVersion = createMavenVersionString(properties);
8093
}
8194

82-
private MavenProperties() {
83-
//prevent instanciation
95+
private MavenProperties(File configFile) {
96+
this.configFile = configFile;
97+
}
98+
99+
/**
100+
* @return the configFile
101+
*/
102+
public File getConfigFile() {
103+
return this.configFile;
104+
}
105+
106+
private synchronized boolean checkForUpdate() throws IOException {
107+
if(configFile.isFile()) {
108+
long modified = configFile.lastModified();
109+
long size = configFile.length();
110+
if(commandline == null || lastModified != modified || size != lastSize) {
111+
List<String> args = new ArrayList<>();
112+
for(String arg : new String(Files.readAllBytes(configFile.toPath())).split("\\s+")) {
113+
if(!arg.isEmpty()) {
114+
args.add(arg);
115+
}
116+
}
117+
CLIManager manager = new CLIManager();
118+
try {
119+
commandline = manager.parse(args.toArray(String[]::new));
120+
} catch(ParseException ex) {
121+
throw new IOException("Couldn't parse file " + configFile, ex);
122+
}
123+
}
124+
lastModified = modified;
125+
lastSize = size;
126+
return true;
127+
}
128+
commandline = null;
129+
lastModified = -1;
130+
lastSize = -1;
131+
return false;
132+
}
133+
134+
private synchronized CommandLine getCommandline() {
135+
return this.commandline;
84136
}
85137

86138
static Properties getMavenRuntimeProperties() {
@@ -174,7 +226,7 @@ public static File computeMultiModuleProjectDirectory(File file) {
174226
final File basedir = file.isDirectory() ? file : file.getParentFile();
175227
IWorkspace workspace = ResourcesPlugin.getWorkspace();
176228
File workspaceRoot = workspace.getRoot().getLocation().toFile();
177-
229+
178230
for(File root = basedir; root != null && !root.equals(workspaceRoot); root = root.getParentFile()) {
179231
if(new File(root, IMavenPlexusContainer.MVN_FOLDER).isDirectory()) {
180232
return root;
@@ -183,27 +235,24 @@ public static File computeMultiModuleProjectDirectory(File file) {
183235
return null;
184236
}
185237

186-
public static CommandLine getMavenArgs(File multiModuleProjectDirectory) throws IOException, ParseException {
238+
public static Optional<MavenProperties> getMavenArgs(File multiModuleProjectDirectory) throws IOException {
187239
if(multiModuleProjectDirectory != null) {
188240
File configFile = new File(multiModuleProjectDirectory, MVN_MAVEN_CONFIG);
189241
if(configFile.isFile()) {
190-
List<String> args = new ArrayList<>();
191-
for(String arg : new String(Files.readAllBytes(configFile.toPath())).split("\\s+")) {
192-
if(!arg.isEmpty()) {
193-
args.add(arg);
194-
}
242+
MavenProperties properties = mavenProperties.computeIfAbsent(configFile, MavenProperties::new);
243+
if(properties.checkForUpdate()) {
244+
return Optional.of(properties);
195245
}
196-
CLIManager manager = new CLIManager();
197-
return manager.parse(args.toArray(String[]::new));
198246
}
199247
}
200-
return null;
248+
return Optional.empty();
201249
}
202250

203-
public static void getProfiles(CommandLine commandLine, Consumer<String> activeProfilesConsumer,
251+
public void getProfiles(Consumer<String> activeProfilesConsumer,
204252
Consumer<String> inactiveProfilesConsumer) {
205-
if(commandLine != null && commandLine.hasOption(CLIManager.ACTIVATE_PROFILES)) {
206-
String[] profileOptionValues = commandLine.getOptionValues(CLIManager.ACTIVATE_PROFILES);
253+
CommandLine commandline = getCommandline();
254+
if(commandline != null && commandline.hasOption(CLIManager.ACTIVATE_PROFILES)) {
255+
String[] profileOptionValues = commandline.getOptionValues(CLIManager.ACTIVATE_PROFILES);
207256
if(profileOptionValues != null) {
208257
for(String profileOptionValue : profileOptionValues) {
209258
StringTokenizer tokenizer = new StringTokenizer(profileOptionValue, ",");
@@ -226,9 +275,10 @@ public static void getProfile(String profile, Consumer<String> activeProfilesCon
226275
}
227276
}
228277

229-
public static void getCliProperties(CommandLine commandLine, BiConsumer<String, String> consumer) {
230-
if(commandLine != null && commandLine.hasOption(CLIManager.SET_SYSTEM_PROPERTY)) {
231-
String[] defStrs = commandLine.getOptionValues(CLIManager.SET_SYSTEM_PROPERTY);
278+
public void getCliProperties(BiConsumer<String, String> consumer) {
279+
CommandLine commandline = getCommandline();
280+
if(commandline != null && commandline.hasOption(CLIManager.SET_SYSTEM_PROPERTY)) {
281+
String[] defStrs = commandline.getOptionValues(CLIManager.SET_SYSTEM_PROPERTY);
232282
if(defStrs != null) {
233283
for(String defStr : defStrs) {
234284
MavenProperties.getCliProperty(defStr, consumer);
@@ -237,6 +287,38 @@ public static void getCliProperties(CommandLine commandLine, BiConsumer<String,
237287
}
238288
}
239289

290+
public String getAlternateGlobalSettingsFile() {
291+
CommandLine commandline = getCommandline();
292+
if(commandline != null && commandline.hasOption(CLIManager.ALTERNATE_GLOBAL_SETTINGS)) {
293+
return commandline.getOptionValue(CLIManager.ALTERNATE_GLOBAL_SETTINGS);
294+
}
295+
return null;
296+
}
297+
298+
public String getAlternateUserSettingsFile() {
299+
CommandLine commandline = getCommandline();
300+
if(commandline != null && commandline.hasOption(CLIManager.ALTERNATE_USER_SETTINGS)) {
301+
return commandline.getOptionValue(CLIManager.ALTERNATE_USER_SETTINGS);
302+
}
303+
return null;
304+
}
305+
306+
public String getAlternateGlobalToolchainsFile() {
307+
CommandLine commandline = getCommandline();
308+
if(commandline != null && commandline.hasOption(CLIManager.ALTERNATE_GLOBAL_TOOLCHAINS)) {
309+
return commandline.getOptionValue(CLIManager.ALTERNATE_USER_SETTINGS);
310+
}
311+
return null;
312+
}
313+
314+
public String getAlternateUserToolchainsFile() {
315+
CommandLine commandline = getCommandline();
316+
if(commandline != null && commandline.hasOption(CLIManager.ALTERNATE_USER_TOOLCHAINS)) {
317+
return commandline.getOptionValue(CLIManager.ALTERNATE_USER_TOOLCHAINS);
318+
}
319+
return null;
320+
}
321+
240322
public static void getCliProperty(String property, BiConsumer<String, String> consumer) {
241323
int index = property.indexOf('=');
242324
if(index <= 0) {
@@ -245,4 +327,5 @@ public static void getCliProperty(String property, BiConsumer<String, String> co
245327
consumer.accept(property.substring(0, index).trim(), property.substring(index + 1).trim());
246328
}
247329
}
330+
248331
}

org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/embedder/PlexusContainerManager.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@
3535
import org.slf4j.ILoggerFactory;
3636
import org.slf4j.LoggerFactory;
3737

38-
import org.apache.commons.cli.CommandLine;
39-
4038
import com.google.inject.AbstractModule;
4139

4240
import org.eclipse.core.resources.IResource;
@@ -350,9 +348,9 @@ protected void configure() {
350348
container.lookup(MavenExecutionRequestPopulator.class).populateDefaults(request);
351349
request.setBaseDirectory(multiModuleProjectDirectory);
352350
request.setMultiModuleProjectDirectory(multiModuleProjectDirectory);
353-
CommandLine commandLine = MavenProperties.getMavenArgs(multiModuleProjectDirectory);
351+
Optional<MavenProperties> mavenProperties = MavenProperties.getMavenArgs(multiModuleProjectDirectory);
354352
Properties userProperties = request.getUserProperties();
355-
MavenProperties.getCliProperties(commandLine, userProperties::setProperty);
353+
mavenProperties.ifPresent(prop -> prop.getCliProperties(userProperties::setProperty));
356354
BootstrapCoreExtensionManager resolver = container.lookup(BootstrapCoreExtensionManager.class);
357355
return resolver.loadCoreExtensions(request, coreEntry.getExportedArtifacts(), extensions);
358356
} finally {

org.eclipse.m2e.core/src/org/eclipse/m2e/core/project/ResolverConfiguration.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,11 @@
2424
import java.util.List;
2525
import java.util.Map;
2626
import java.util.Objects;
27+
import java.util.Optional;
2728
import java.util.Properties;
2829
import java.util.Set;
2930
import java.util.stream.Stream;
3031

31-
import org.apache.commons.cli.CommandLine;
32-
import org.apache.commons.cli.ParseException;
33-
3432
import org.eclipse.core.resources.IProject;
3533

3634
import org.eclipse.m2e.core.internal.MavenPluginActivator;
@@ -202,14 +200,16 @@ public void setMultiModuleProjectDirectory(File multiModuleProjectDirectory) {
202200
if(!Objects.equals(this.multiModuleProjectDirectory, multiModuleProjectDirectory)) {
203201
this.multiModuleProjectDirectory = multiModuleProjectDirectory;
204202
try {
205-
CommandLine commandLine = MavenProperties.getMavenArgs(multiModuleProjectDirectory);
203+
Optional<MavenProperties> mavenProps = MavenProperties.getMavenArgs(multiModuleProjectDirectory);
206204
Map<String, String> props = new LinkedHashMap<>();
207-
MavenProperties.getCliProperties(commandLine, props::put);
208-
userProperties = Collections.unmodifiableMap(props);
209205
userActiveProfiles = new ArrayList<>();
210206
userInactiveProfiles = new ArrayList<>();
211-
MavenProperties.getProfiles(commandLine, userActiveProfiles::add, userInactiveProfiles::add);
212-
} catch(IOException | ParseException ex) {
207+
mavenProps.ifPresent(args -> {
208+
args.getCliProperties(props::put);
209+
args.getProfiles(userActiveProfiles::add, userInactiveProfiles::add);
210+
});
211+
userProperties = Collections.unmodifiableMap(props);
212+
} catch(IOException ex) {
213213
//can't use it then... :-(
214214
MavenPluginActivator.getDefault().getLog().error("can't read maven args from " + multiModuleProjectDirectory,
215215
ex);

0 commit comments

Comments
 (0)