diff --git a/src/main/java/picocli/CommandLine.java b/src/main/java/picocli/CommandLine.java index 594ac6f7a..6a0f589ce 100644 --- a/src/main/java/picocli/CommandLine.java +++ b/src/main/java/picocli/CommandLine.java @@ -18972,11 +18972,29 @@ private static Properties loadProperties(CommandSpec commandSpec) { if (commandSpec == null) { return null; } Properties p = System.getProperties(); for (String name : commandSpec.names()) { + String propertiesFileName = "." + name + ".properties"; String path = p.getProperty("picocli.defaults." + name + ".path"); - File defaultPath = new File(p.getProperty("user.home"), "." + name + ".properties"); + File defaultPath = new File(p.getProperty("user.home"), propertiesFileName); File file = path == null ? defaultPath : new File(path); if (file.canRead()) { return createProperties(file, commandSpec); + } else { + Object userObject = commandSpec.userObject(); + if (userObject == null) { + userObject = commandSpec.commandLine; + } + URL resource = userObject.getClass().getClassLoader().getResource(propertiesFileName); + Tracer tracer = CommandLine.tracer(); + if (resource != null) { + file = new File(resource.getFile()); + if (file.canRead()) { + return createProperties(file, commandSpec); + } else { + tracer.warn("could not read defaults from %s", file.getAbsolutePath()); + } + } else { + tracer.warn("defaults configuration file %s doesn not exists on classpath", propertiesFileName); + } } } return loadProperties(commandSpec.parent()); diff --git a/src/test/java/picocli/PropertiesDefaultProviderTest.java b/src/test/java/picocli/PropertiesDefaultProviderTest.java index 52720717e..61425952c 100644 --- a/src/test/java/picocli/PropertiesDefaultProviderTest.java +++ b/src/test/java/picocli/PropertiesDefaultProviderTest.java @@ -52,6 +52,27 @@ static class Subcommand { @Parameters(index = "0", paramLabel = "qqq", descriptionKey = "xxx") int xxx; } + @Test + public void testLoadFromResourceClasspathIfPropertySpecified() throws IOException { + Properties expected = new Properties(); + expected.setProperty("aaa", "111"); + expected.setProperty("bbb", "222"); + expected.setProperty("ppp", "333"); + expected.setProperty("xxx", "444"); + + MyApp myApp = new MyApp(); + assertEquals(myApp.aaa, 0); + assertEquals(myApp.bbb, 0); + assertEquals(myApp.ppp, 0); + assertEquals(myApp.xxx, 0); + new CommandLine(myApp).parseArgs(); + + assertEquals(myApp.aaa, 111); + assertEquals(myApp.bbb, 222); + assertEquals(myApp.ppp, 333); + assertEquals(myApp.xxx, 444); + } + @Test public void testLoadFromUserHomeCommandNameByDefault() throws IOException { File f = new File(System.getProperty("user.home"), ".providertest.properties"); diff --git a/src/test/resources/.providertest.properties b/src/test/resources/.providertest.properties new file mode 100644 index 000000000..e1876e87d --- /dev/null +++ b/src/test/resources/.providertest.properties @@ -0,0 +1,6 @@ +#exported from test +#Mon Sep 18 12:37:21 CEST 2023 +aaa=111 +ppp=333 +bbb=222 +xxx=444