2929import java .util .ArrayList ;
3030import java .util .Date ;
3131import java .util .List ;
32+ import java .util .Map ;
33+ import java .util .Optional ;
3234import java .util .Properties ;
3335import java .util .StringTokenizer ;
36+ import java .util .concurrent .ConcurrentHashMap ;
3437import java .util .function .BiConsumer ;
3538import 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}
0 commit comments