If you are a Java developer you certainly know Apache Maven, which is (more or less) a Make equivalent for the Java world.
Some days ago I needed to automatically extract some values from the pom.xml file to automatize some operations. For example, I wanted to extract the values of the groupId, artifactId and version nodes from a Maven project, i.e. from the pom.xml file.
I finally created a pure Windows batch solution in order to do that, I called this little utility: POM Tool.
Here is the source code of it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
@echo off setlocal EnableDelayedExpansion set POM_TOOL_VERSION=20180914 rem Parameters set MAVEN_POM_INPUT_TAG=%1 set MAVEN_POM_FILE=pom.xml if "%2" neq "" set MAVEN_POM_FILE=%2 rem Check input parameters if "%MAVEN_POM_INPUT_TAG%"=="" goto help if "%MAVEN_POM_FILE%"=="" goto error_pom_not_found if not exist %MAVEN_POM_FILE% goto error_pom_not_found rem Compute start/end positions to extract the value from the XML tag call:strlen %MAVEN_POM_INPUT_TAG% set /a TAG_BEGIN=%_strlen% + 2 set /a TAG_END=(%TAG_BEGIN% + 1) * -1 rem Perform the XML tag value extraction call:extractxml %MAVEN_POM_INPUT_TAG% %TAG_BEGIN% %TAG_END% if "%_extractxml%"=="" goto error_extraction_failed echo %_extractxml% goto:eof :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: rem --- rem Display help message rem --- :help call:banner echo Usage: %~n0 ^<xml_tag^> [pom_file] echo. echo Example: %~n0 version - will return artifact version echo %~n0 groupId - will return artifact groupId goto:eof rem --- rem Error: The requested tag was not found rem --- :error_extraction_failed call:banner echo Error: The tag "%MAVEN_POM_INPUT_TAG%" wasn't found in the "%MAVEN_POM_FILE%" file. goto:eof rem --- rem Error: The Maven POM file wasn't found rem --- :error_pom_not_found call:banner echo Error: The "%MAVEN_POM_FILE%" file wasn't found. goto:eof rem --- rem Function: banner rem Used to display the tool banner rem --- :banner echo POM Tool - Ver. %POM_TOOL_VERSION% echo. goto:eof rem --- rem Function: extractxml rem Used to parse %MAVEN_POM_FILE% for extracting tags value. rem Usage: call:extractxml %param% where %param% is the tag to search. rem --- :extractxml setlocal set _extracted_xml_result= set _token=^<%~1^> set _begin=%~2 set _end=%~3 for /f "tokens=*" %%a in (%MAVEN_POM_FILE%) do ( set row=%%a if /i "!row:%_token%=!"=="!row!" (echo NOP > nul) else ( set _extracted_xml_result=!row! if /i "!_extracted_xml_result!" neq "" goto extractxml_endloop ) ) :extractxml_endloop set "_final=%_extracted_xml_result%" if "%_final%" neq "" call set _final=%%_final:~%_begin%,%_end%%% (endlocal if "%~1" neq "" set "_extractxml=%_final%" ) goto:eof rem --- rem Function: strlen rem Get the length of a string rem Thanks to: SS64 (https://ss64.com/nt/syntax-strlen.html) rem --- :strlen setlocal set _str=%~1 rem Remove any quotes set _str=%_str:"=% rem Test if empty if not defined _str echo endlocal & set _strlen=0&goto:eof set MAX_STRING_LENGTH=256 for /l %%g in (0,1,!MAX_STRING_LENGTH!) do ( REM extract one character set "_char=!_str:~%%g,1!" REM if _char is empty we are at the end of the string if not defined _char endlocal & set _strlen=%%g& goto:eof ) goto:eof |
Just copy-paste that source code into a new file and call the file pomtool.cmd.
The usage is pretty simple:
1 |
pomtool artifactId c:\temp\my-maven-project\pom.xml |
This will give you the value of the artifactId node for the c:\temp\my-maven-project\pom.xml file!
Of course you can include a call to the POM Tool in another batch file, for example:
1 2 3 4 |
@echo off for /f "tokens=*" %%i in ('pomtool description c:\my\project\pom.xml') do set "PROJECT_NAME=%%i" echo %PROJECT_NAME% pause |
In that way, the %PROJECT_NAME% variable in your batch will have the output of the POM Tool utility, in that example the content of the description node! 🙂
Don’t hesitate to comments if you have any question! 🙂