Skip to content

Commit

Permalink
Cache maven.config and support reading the (global) settings path
Browse files Browse the repository at this point in the history
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 <laeubi@laeubi-soft.de>
  • Loading branch information
laeubi committed Feb 14, 2024
1 parent f201544 commit 830c305
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

Expand Down Expand Up @@ -73,14 +76,63 @@ public class MavenProperties {

private static String mavenBuildVersion;

private static Map<File, MavenProperties> mavenProperties = new ConcurrentHashMap<>();

private File configFile;

private CommandLine commandline;

private long lastModified;

private long lastSize;

static {
Properties properties = getMavenRuntimeProperties();
mavenVersion = properties.getProperty(BUILD_VERSION_PROPERTY, BUILD_VERSION_UNKNOWN_PROPERTY);
mavenBuildVersion = createMavenVersionString(properties);
}

private MavenProperties() {
//prevent instanciation
private MavenProperties(File configFile) {
this.configFile = configFile;
}

/**
* @return the configFile
*/
public File getConfigFile() {
return this.configFile;
}

private synchronized boolean checkForUpdate() throws IOException {
if(configFile.isFile()) {
long modified = configFile.lastModified();
long size = configFile.length();
if(commandline == null || lastModified != modified || size != lastSize) {
List<String> args = new ArrayList<>();
for(String arg : new String(Files.readAllBytes(configFile.toPath())).split("\\s+")) {
if(!arg.isEmpty()) {
args.add(arg);
}
}
CLIManager manager = new CLIManager();
try {
commandline = manager.parse(args.toArray(String[]::new));
} catch(ParseException ex) {
throw new IOException("Couldn't parse file " + configFile, ex);
}
}
lastModified = modified;
lastSize = size;
return true;
}
commandline = null;
lastModified = -1;
lastSize = -1;
return false;
}

private synchronized CommandLine getCommandline() {
return this.commandline;
}

static Properties getMavenRuntimeProperties() {
Expand Down Expand Up @@ -174,7 +226,7 @@ public static File computeMultiModuleProjectDirectory(File file) {
final File basedir = file.isDirectory() ? file : file.getParentFile();
IWorkspace workspace = ResourcesPlugin.getWorkspace();
File workspaceRoot = workspace.getRoot().getLocation().toFile();

for(File root = basedir; root != null && !root.equals(workspaceRoot); root = root.getParentFile()) {
if(new File(root, IMavenPlexusContainer.MVN_FOLDER).isDirectory()) {
return root;
Expand All @@ -183,27 +235,24 @@ public static File computeMultiModuleProjectDirectory(File file) {
return null;
}

public static CommandLine getMavenArgs(File multiModuleProjectDirectory) throws IOException, ParseException {
public static Optional<MavenProperties> getMavenArgs(File multiModuleProjectDirectory) throws IOException {
if(multiModuleProjectDirectory != null) {
File configFile = new File(multiModuleProjectDirectory, MVN_MAVEN_CONFIG);
if(configFile.isFile()) {
List<String> args = new ArrayList<>();
for(String arg : new String(Files.readAllBytes(configFile.toPath())).split("\\s+")) {
if(!arg.isEmpty()) {
args.add(arg);
}
MavenProperties properties = mavenProperties.computeIfAbsent(configFile, MavenProperties::new);
if(properties.checkForUpdate()) {
return Optional.of(properties);
}
CLIManager manager = new CLIManager();
return manager.parse(args.toArray(String[]::new));
}
}
return null;
return Optional.empty();
}

public static void getProfiles(CommandLine commandLine, Consumer<String> activeProfilesConsumer,
public void getProfiles(Consumer<String> activeProfilesConsumer,
Consumer<String> inactiveProfilesConsumer) {
if(commandLine != null && commandLine.hasOption(CLIManager.ACTIVATE_PROFILES)) {
String[] profileOptionValues = commandLine.getOptionValues(CLIManager.ACTIVATE_PROFILES);
CommandLine commandline = getCommandline();
if(commandline != null && commandline.hasOption(CLIManager.ACTIVATE_PROFILES)) {
String[] profileOptionValues = commandline.getOptionValues(CLIManager.ACTIVATE_PROFILES);
if(profileOptionValues != null) {
for(String profileOptionValue : profileOptionValues) {
StringTokenizer tokenizer = new StringTokenizer(profileOptionValue, ",");
Expand All @@ -226,9 +275,10 @@ public static void getProfile(String profile, Consumer<String> activeProfilesCon
}
}

public static void getCliProperties(CommandLine commandLine, BiConsumer<String, String> consumer) {
if(commandLine != null && commandLine.hasOption(CLIManager.SET_SYSTEM_PROPERTY)) {
String[] defStrs = commandLine.getOptionValues(CLIManager.SET_SYSTEM_PROPERTY);
public void getCliProperties(BiConsumer<String, String> consumer) {
CommandLine commandline = getCommandline();
if(commandline != null && commandline.hasOption(CLIManager.SET_SYSTEM_PROPERTY)) {
String[] defStrs = commandline.getOptionValues(CLIManager.SET_SYSTEM_PROPERTY);
if(defStrs != null) {
for(String defStr : defStrs) {
MavenProperties.getCliProperty(defStr, consumer);
Expand All @@ -237,6 +287,38 @@ public static void getCliProperties(CommandLine commandLine, BiConsumer<String,
}
}

public String getAlternateGlobalSettingsFile() {
CommandLine commandline = getCommandline();
if(commandline != null && commandline.hasOption(CLIManager.ALTERNATE_GLOBAL_SETTINGS)) {
return commandline.getOptionValue(CLIManager.ALTERNATE_GLOBAL_SETTINGS);
}
return null;
}

public String getAlternateUserSettingsFile() {
CommandLine commandline = getCommandline();
if(commandline != null && commandline.hasOption(CLIManager.ALTERNATE_USER_SETTINGS)) {
return commandline.getOptionValue(CLIManager.ALTERNATE_USER_SETTINGS);
}
return null;
}

public String getAlternateGlobalToolchainsFile() {
CommandLine commandline = getCommandline();
if(commandline != null && commandline.hasOption(CLIManager.ALTERNATE_GLOBAL_TOOLCHAINS)) {
return commandline.getOptionValue(CLIManager.ALTERNATE_USER_SETTINGS);
}
return null;
}

public String getAlternateUserToolchainsFile() {
CommandLine commandline = getCommandline();
if(commandline != null && commandline.hasOption(CLIManager.ALTERNATE_USER_TOOLCHAINS)) {
return commandline.getOptionValue(CLIManager.ALTERNATE_USER_TOOLCHAINS);
}
return null;
}

public static void getCliProperty(String property, BiConsumer<String, String> consumer) {
int index = property.indexOf('=');
if(index <= 0) {
Expand All @@ -245,4 +327,5 @@ public static void getCliProperty(String property, BiConsumer<String, String> co
consumer.accept(property.substring(0, index).trim(), property.substring(index + 1).trim());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
import org.slf4j.ILoggerFactory;
import org.slf4j.LoggerFactory;

import org.apache.commons.cli.CommandLine;

import com.google.inject.AbstractModule;

import org.eclipse.core.resources.IResource;
Expand Down Expand Up @@ -350,9 +348,9 @@ protected void configure() {
container.lookup(MavenExecutionRequestPopulator.class).populateDefaults(request);
request.setBaseDirectory(multiModuleProjectDirectory);
request.setMultiModuleProjectDirectory(multiModuleProjectDirectory);
CommandLine commandLine = MavenProperties.getMavenArgs(multiModuleProjectDirectory);
Optional<MavenProperties> mavenProperties = MavenProperties.getMavenArgs(multiModuleProjectDirectory);
Properties userProperties = request.getUserProperties();
MavenProperties.getCliProperties(commandLine, userProperties::setProperty);
mavenProperties.ifPresent(prop -> prop.getCliProperties(userProperties::setProperty));
BootstrapCoreExtensionManager resolver = container.lookup(BootstrapCoreExtensionManager.class);
return resolver.loadCoreExtensions(request, coreEntry.getExportedArtifacts(), extensions);
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.stream.Stream;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.ParseException;

import org.eclipse.core.resources.IProject;

import org.eclipse.m2e.core.internal.MavenPluginActivator;
Expand Down Expand Up @@ -202,14 +200,16 @@ public void setMultiModuleProjectDirectory(File multiModuleProjectDirectory) {
if(!Objects.equals(this.multiModuleProjectDirectory, multiModuleProjectDirectory)) {
this.multiModuleProjectDirectory = multiModuleProjectDirectory;
try {
CommandLine commandLine = MavenProperties.getMavenArgs(multiModuleProjectDirectory);
Optional<MavenProperties> mavenProps = MavenProperties.getMavenArgs(multiModuleProjectDirectory);
Map<String, String> props = new LinkedHashMap<>();
MavenProperties.getCliProperties(commandLine, props::put);
userProperties = Collections.unmodifiableMap(props);
userActiveProfiles = new ArrayList<>();
userInactiveProfiles = new ArrayList<>();
MavenProperties.getProfiles(commandLine, userActiveProfiles::add, userInactiveProfiles::add);
} catch(IOException | ParseException ex) {
mavenProps.ifPresent(args -> {
args.getCliProperties(props::put);
args.getProfiles(userActiveProfiles::add, userInactiveProfiles::add);
});
userProperties = Collections.unmodifiableMap(props);
} catch(IOException ex) {
//can't use it then... :-(
MavenPluginActivator.getDefault().getLog().error("can't read maven args from " + multiModuleProjectDirectory,
ex);
Expand Down

0 comments on commit 830c305

Please sign in to comment.