Repository: joelittlejohn/jsonschema2pojo (java)

Issue:
command line option to show version
It seems there is no (easy) possibility to find out which version of the command line tool one is running.
we should either get a "-v" and "--version" option, and/or display the version in/with the --help infos.

Pull request description:
Add command line option to print version
Closes #1435

Patch:
diff --git a/jsonschema2pojo-cli/pom.xml b/jsonschema2pojo-cli/pom.xml
index fd3391e2..8e0dd091 100644
--- a/jsonschema2pojo-cli/pom.xml
+++ b/jsonschema2pojo-cli/pom.xml
@@ -22,6 +22,10 @@
                 </includes>
                 <targetPath>${project.build.directory}</targetPath>
             </resource>
+            <resource>
+                <filtering>true</filtering>
+                <directory>${basedir}/src/main/resources</directory>
+            </resource>
         </resources>
         <plugins>
             <plugin>
diff --git a/jsonschema2pojo-cli/src/main/java/org/jsonschema2pojo/cli/Arguments.java b/jsonschema2pojo-cli/src/main/java/org/jsonschema2pojo/cli/Arguments.java
index d7a2891b..893efa61 100644
--- a/jsonschema2pojo-cli/src/main/java/org/jsonschema2pojo/cli/Arguments.java
+++ b/jsonschema2pojo-cli/src/main/java/org/jsonschema2pojo/cli/Arguments.java
@@ -20,11 +20,13 @@ import static org.apache.commons.lang3.StringUtils.*;
 
 import java.io.File;
 import java.io.FileFilter;
+import java.io.IOException;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Properties;
 import java.util.stream.Collectors;
 
 import org.jsonschema2pojo.AllFileFilter;
@@ -249,6 +251,9 @@ public class Arguments implements GenerationConfig {
     @Parameter(names = { "--useJakartaValidation" }, description = "Whether to use annotations from jakarta.validation package instead of javax.validation package when adding JSR-303/349 annotations to generated Java types")
     private boolean useJakartaValidation = false;
 
+    @Parameter(names = { "-v", "--version"}, description = "Print version information", help = true)
+    private boolean printVersion = false;
+
     private static final int EXIT_OKAY = 0;
     private static final int EXIT_ERROR = 1;
 
@@ -274,8 +279,13 @@ public class Arguments implements GenerationConfig {
             if (this.showHelp) {
                 jCommander.usage();
                 exit(EXIT_OKAY);
+            } else if (printVersion) {
+                Properties properties = new Properties();
+                properties.load(getClass().getResourceAsStream("version.properties"));
+                jCommander.getConsole().println(jCommander.getProgramName() + " version " + properties.getProperty("version"));
+                exit(EXIT_OKAY);
             }
-        } catch (ParameterException e) {
+        } catch (IOException | ParameterException e) {
             System.err.println(e.getMessage());
             jCommander.usage();
             exit(EXIT_ERROR);
diff --git a/jsonschema2pojo-cli/src/main/resources/org/jsonschema2pojo/cli/version.properties b/jsonschema2pojo-cli/src/main/resources/org/jsonschema2pojo/cli/version.properties
new file mode 100644
index 00000000..e5683df8
--- /dev/null
+++ b/jsonschema2pojo-cli/src/main/resources/org/jsonschema2pojo/cli/version.properties
@@ -0,0 +1,1 @@
+version=${project.version}
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index b6d396ec..ae68bea6 100644
--- a/pom.xml
+++ b/pom.xml
@@ -301,7 +301,7 @@
             <dependency>
                 <groupId>com.beust</groupId>
                 <artifactId>jcommander</artifactId>
-                <version>1.30</version>
+                <version>1.82</version>
             </dependency>
             <dependency>
                 <groupId>com.fasterxml.jackson.core</groupId>


Test patch:
diff --git a/jsonschema2pojo-cli/src/test/java/org/jsonschema2pojo/cli/ArgumentsTest.java b/jsonschema2pojo-cli/src/test/java/org/jsonschema2pojo/cli/ArgumentsTest.java
index cf06c9e9..8e88565b 100644
--- a/jsonschema2pojo-cli/src/test/java/org/jsonschema2pojo/cli/ArgumentsTest.java
+++ b/jsonschema2pojo-cli/src/test/java/org/jsonschema2pojo/cli/ArgumentsTest.java
@@ -21,7 +21,6 @@ import static org.hamcrest.Matchers.*;
 
 import java.io.ByteArrayOutputStream;
 import java.io.File;
-import java.io.IOException;
 import java.io.PrintStream;
 import java.nio.charset.StandardCharsets;
 
@@ -127,7 +126,7 @@ public class ArgumentsTest {
     }
 
     @Test
-    public void missingArgsCausesHelp() throws IOException {
+    public void missingArgsCausesHelp() {
         ArgsForTest args = (ArgsForTest) new ArgsForTest().parse(new String[] {});
 
         assertThat(args.status, is(1));
@@ -137,13 +136,21 @@ public class ArgumentsTest {
     }
 
     @Test
-    public void requestingHelpCausesHelp() throws IOException {
+    public void requestingHelpCausesHelp() {
         ArgsForTest args = (ArgsForTest) new ArgsForTest().parse(new String[] { "--help" });
 
         assertThat(args.status, is(notNullValue()));
         assertThat(new String(systemOutCapture.toByteArray(), StandardCharsets.UTF_8), is(containsString("Usage: jsonschema2pojo")));
     }
 
+    @Test
+    public void requestingVersionCausesVersion() {
+        ArgsForTest args = (ArgsForTest) new ArgsForTest().parse(new String[] { "--version" });
+
+        assertThat(args.didExit(), is(true));
+        assertThat(new String(systemOutCapture.toByteArray(), StandardCharsets.UTF_8).matches("(?s)jsonschema2pojo version \\d.*"), is(true));
+    }
+
     private File theFile(String path) {
         return new File(path);
     }