@@ -17,12 +17,13 @@ public interface Packer {
1717
1818 String VERSION_FILE = "version.txt" ;
1919
20- static void createJAR (Path rootClassesDirectory , Optional <Path > rootResourcesDirectory , Path rootJARDirectory , String jarFileName , Optional <Path > mainClass ) throws IOException {
20+ static void createJAR (Path projectRoot , Path rootClassesDirectory , Optional <Path > rootResourcesDirectory , Path rootJARDirectory , String jarFileName , Optional <Path > mainClass ) throws IOException {
2121 var jarFile = rootJARDirectory .resolve (jarFileName );
2222 Files .createDirectories (rootJARDirectory );
2323 Files .deleteIfExists (jarFile );
2424
25- var version = rootResourcesDirectory .flatMap (Packer ::readVersion );
25+ var versionFile = locateVersionFile (projectRoot , rootResourcesDirectory );
26+ var version = versionFile .flatMap (Packer ::readVersionContent );
2627
2728 try (var fos = Files .newOutputStream (jarFile , StandardOpenOption .CREATE_NEW );
2829 var jos = new JarOutputStream (fos )) {
@@ -37,24 +38,44 @@ static void createJAR(Path rootClassesDirectory, Optional<Path> rootResourcesDir
3738 var dir = rootResourcesDirectory .get ();
3839 try (var paths = Files .walk (dir )) {
3940 paths .filter (Files ::isRegularFile )
41+ .filter (path -> !path .equals (dir .resolve (VERSION_FILE )))
4042 .forEach (path -> addEntry (dir , jos , path ));
4143 }
4244 }
45+ versionFile .ifPresent (file -> addVersionEntry (jos , file ));
4346 }
4447 }
4548
46- static Optional <String > readVersion (Path resourcesDirectory ) {
47- var versionFile = resourcesDirectory .resolve (VERSION_FILE );
48- if (! Files .isRegularFile (versionFile )) {
49- return Optional .empty ( );
49+ static Optional <Path > locateVersionFile (Path projectRoot , Optional < Path > resourcesDirectory ) {
50+ var rootCandidate = projectRoot .resolve (VERSION_FILE );
51+ if (Files .isRegularFile (rootCandidate )) {
52+ return Optional .of ( rootCandidate );
5053 }
54+ return resourcesDirectory
55+ .map (dir -> dir .resolve (VERSION_FILE ))
56+ .filter (Files ::isRegularFile );
57+ }
58+
59+ static Optional <String > readVersion (Path projectRoot , Optional <Path > resourcesDirectory ) {
60+ return locateVersionFile (projectRoot , resourcesDirectory ).flatMap (Packer ::readVersionContent );
61+ }
62+
63+ static Optional <String > readVersionContent (Path versionFile ) {
5164 try {
5265 return Optional .of (Files .readString (versionFile ).trim ());
5366 } catch (IOException e ) {
5467 return Optional .empty ();
5568 }
5669 }
5770
71+ static void addVersionEntry (JarOutputStream jos , Path versionFile ) {
72+ try {
73+ addEntry (jos , Path .of (VERSION_FILE ), Files .readAllBytes (versionFile ));
74+ } catch (IOException e ) {
75+ throw new RuntimeException ("Failed to add version.txt to JAR: " + versionFile , e );
76+ }
77+ }
78+
5879 static void addManifest (Path rootClassesDirectory , JarOutputStream jos , Path mainClass , Optional <String > version ) {
5980 var javaPackage = pathToJavaPackage (mainClass );
6081 var fullyQualifiedClassName = javaPackage .replace (".java" , "" );
0 commit comments