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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 | package Torello.Java.Build; import Torello.Java.Shell; import Torello.Java.OSResponse; import Torello.Java.OSExtras; import Torello.BuildJAR.Build; import Torello.Java.FileNode; import Torello.Java.StrCmpr; import Torello.Java.StrFilter; import Torello.Java.FileRW; import Torello.Java.RTC; import Torello.Java.Additional.BiAppendable; import Torello.Java.Additional.AppendableSafe; import Torello.Java.ReadOnly.ROArrayListBuilder; import Torello.Java.ReadOnly.ReadOnlyList; import static Torello.Java.C.*; import java.io.IOException; import java.io.File; import java.io.FilenameFilter; import java.io.FileFilter; import java.util.TreeMap; import java.util.stream.Stream; /** * This is the fourth Build-Stage, and it runs the UNIX-Shell utility {@code 'tar'} and the Java * utility {@code 'jar'}. These operations are performed via {@link Shell Torello.Java.Shell}. * * <EMBED CLASS='external-html' DATA-FILE-ID=S04_TAR_JAR> */ @Torello.JavaDoc.StaticFunctional public class S04_TarJar { // Completely irrelevant, and the 'private' modifier keeps it off of JavaDoc private S04_TarJar() { } private static final String FS = File.separator; // ******************************************************************************************** // ******************************************************************************************** // This class MAIN METHOD // ******************************************************************************************** // ******************************************************************************************** public static void compress(Builder builder) throws IOException { builder.timers.startStage04(); final StringBuilder SB_TAR = new StringBuilder(); final StringBuilder SB_JAR = new StringBuilder(); Printing.startStep(4); if (! builder.cli.JAR_ONLY) { twoTarFiles(builder, SB_TAR); System.out.println(); } jarFile(builder, SB_JAR); if (! builder.cli.JAR_ONLY) builder.logs.write_S04_LOGS(SB_TAR.toString(), SB_JAR.toString()); if (builder.cli.JAR_ONLY && (builder.JAR_FILE_NAME != null)) { System.out.println( "Moving " + BYELLOW + builder.JAR_FILE + RESET + " to " + BYELLOW + builder.JAR_FILE_NAME + RESET + '\n' ); FileRW.moveFile(builder.JAR_FILE, builder.JAR_FILE_NAME, false); } builder.timers.endStage04(); } // ******************************************************************************************** // ******************************************************************************************** // Build the 2 TAR-Files, Build the 1 JAR-File // ******************************************************************************************** // ******************************************************************************************** private static void twoTarFiles (final Builder builder, final StringBuilder SB_TAR) throws IOException { // NOTE: It is OK to print the entire command directly to standard output, because the // '.tar' Files are very short commands, that only tar-up a single directory // // MAIN-TAR ==> Root-Source // JAVA-DOC-TAR ==> 'javadoc/' // // Shell-Constructor Paramters used: // (outputAppendable, commandStrAppendable, standardOutput, errorOutput) Shell shell = new Shell(SB_TAR, new BiAppendable(SB_TAR, System.out), null, null); // JavaHTML-1.x.tar.gz CHECK( shell.COMMAND( "tar", new String[] { "-cvzf", builder.TAR_FILE, builder.TAR_SOURCE_DIR } )); SB_TAR.append(Printing.TAR_JAR_DIVIDER); // JavaHTML-javadoc-1.x.tar CHECK( shell.COMMAND( "tar", new String[] { "-cvf", builder.JAVADOC_TAR_FILE, builder.LOCAL_JAVADOC_DIR } )); } // ******************************************************************************************** // ******************************************************************************************** // Build the JAR-File // ******************************************************************************************** // ******************************************************************************************** private static void jarFile(final Builder builder, final StringBuilder logOnly) throws IOException { // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** // Setup some variables / constants for running this class // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** // Uses Shell-Contructor: // (outputAppendable, commandStrAppendable, standardOutput, errorOutput) final Shell shell = new Shell(logOnly, logOnly, null, null); final AppendableSafe logAndScreen = new AppendableSafe( new BiAppendable(System.out, logOnly), AppendableSafe.USE_APPENDABLE_ERROR ); // The first loop-iteration uses a different command-switch to the 'jar' command // *** First jar-command uses: "-cvf" (Create, Verbose, Files) // *** Successive jar-commands use: "-uvf" (Update, Verbose, Files) boolean firstIteration = true; // The Shell 'jar' command must be executed from the File-System directory set to the // "Current Working Directory" - or else the location inside the '.jar' will just be all // messed up! // // NOTE: This cute little thing here was A LOT of work, not just a little bit. The // "Relative Path" within the jar cannot include extranneous class-path directory // stuff. In the end, using it just 3 lines of code! final OSExtras osExtras = new OSExtras(); // This creates a 'FileFilter' that is **ALSO** to configured to print out directories that // match - into the provided StringBuilder - 'logAndScreen' // // Prior to printing out the directories it accepts, "getSubPackageDirFilter" was a private // static final constant named "PACKAGE_DIR_FILTER" (or something like that - it wasn't a // method is the only point). final FileFilter JAR_SUB_PACKAGES_DIR_FILTER = getSubPackageDirFilter(logAndScreen); // Since the Shell & OSExtras combo is being used - where the command is actually executed // from a separate directory, the Jar-File needs to be referenced using its absolute path // name final String JAR_FILE_NAME_ABSOLUTE = builder.HOME_DIR + builder.JAR_FILE; // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** // Loop all Packages in the Project // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** for (BuildPackage bp : builder.packageList) { // The user is allowed to specify (via the "BuildPackage" Constructor and one of that // classes flags), to avoid / skip putting some packages into the '.jar' if (bp.doNotJAR) continue; // Classes considered to be "still in early development" are also not put into the jar if (bp.earlyDevelopment) continue; // Since this is just **TOO** long, use a "System.out" that just says what it's doing final String note = "Add to '.jar' File: " + bp.fullName + (bp.hasSubPackages ? (" & " + BCYAN + "Sub-Packages" + RESET) : "") + '\n'; logAndScreen.append(note); // This is how to PROPERLY insert files into the JAR - making-sure that their actual // File-Name DOES-NOT include the Class-Path part of the directory-location, *AND* // DOES include the Java-Package part of the diretory-location. // // For "MyProjects/src/main/My/Java/Package/SourceFile.java" // The Name in the JAR for any '.class' File whose package is named: // "My.Java.Package.SomeSourceFile" // // SHOULD INCLUDE: "My/Java/Package/SomeSourceFile.class" // SHOULD NOT INCLUDE: "MyProjects/src/main/" // // That is all this "cpStrLen" and "classPathLocationStr-Length" and "map(substring)" // stuff is actually trying to acheive. (And doing it very well, I might add) // I am choosing to call this "JAR File Integrity" final int cpStrLen = bp.classPathLocation.length(); /* System.out.println( "bp.classPathLocation: [" + bp.classPathLocation + ']' + '\n' + "bp.cpl.length(): " + bp.classPathLocation.length() ); */ // Unlike the TAR-Files, when creating the JAR-File, the actual files being inserted // have to be NAMED, EXPLICITLY. As a result, class FileNode needs to be used. // Also note that because of this reason, the jar-commands will not be printed to the // user's terminal (because they grow very long and unreadable / ugly). final Stream.Builder<String> b = FileNode .createRoot(bp.pkgRootDirectory) .loadTree( bp.hasSubPackages ? -1 : 0, JAR_FILENAME_FILTER, bp.hasSubPackages ? JAR_SUB_PACKAGES_DIR_FILTER : null ) .flattenJustFiles(RTC.FULLPATH_STREAM_BUILDER(null)); // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** // Any & All User-Provided / User-Specified "Helper Package" Sub-Directories // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** // // NOTE: the "BuildPackage.helperPackages" is just a User-Provided Configuration that // allows the user to add some brief sub-packages / sub-directories that remain // undocumented (and, therefore, invisible) - but are still imported into the JAR // // The "Helper Packages" are very few/rare. For instance package "Torello.HTML" has a // very small sub-directory called "parse/"... That is a helper package! // "Torello.Java" has a slightly larger one called "VariableReturnType" // // Stuff inside of a Helper Package wasn't important enough to include it in the // JAR-API, and it is not documented (not included in Java-Doc). The classes and // methods they export are, indeed, public - but they just aren't mentioned anywhere // because the end-users wouldn't be able to do anything with them. for (String helperPkgDirName : bp.helperPackages) { logAndScreen.append( " Adding Helper-Package Files: " + BCYAN + helperPkgDirName + RESET + '\n' ); FileNode .createRoot(helperPkgDirName) .loadTree(0, JAR_FILENAME_FILTER, null) .flattenJustFiles(RTC.FULLPATH_STREAM_BUILDER(b)); } // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** // The "data-files/" directory // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** // // If there is a "data-files/" sub-directory of a Package-Directory, then all of it's // contents also need to be added to the '.jar' final String fDataDirName = bp.pkgRootDirectory + "data-files" + FS; final File fDataDir = new File(fDataDirName); // FIRST: Check if there is a package/data-files/ directory if (fDataDir.exists() && fDataDir.isDirectory()) addDataFilesDir(fDataDirName, b, logAndScreen); // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** // Now Convert the Stream.Builder<String> into a String[] array // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** // // In order to actually utilize the Class-Path / cpStrLen-Substring stuff, the building // of these '.class' File String[]-Arrays is done using the ? : syntax thingy... final String[] jarFilesArr = (cpStrLen == 0) ? b.build().toArray(String[]::new) : b.build().map(fileName -> fileName.substring(cpStrLen)).toArray(String[]::new); // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** // Run the '.jar' Command // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** // // Now just build the Operating-System 'jar' Command. // All that needs to happen is to concatenate the 2 command switches and the list of // files from the jar-files String[]-Array that was just created previously. String[] JAR_COMMAND = new String[2 + jarFilesArr.length]; JAR_COMMAND[0] = firstIteration ? "-cvf" : "-uvf"; JAR_COMMAND[1] = JAR_FILE_NAME_ABSOLUTE; System.arraycopy(jarFilesArr, 0, JAR_COMMAND, 2, jarFilesArr.length); if (cpStrLen > 0) { // REMEMBER: shell.osExtras is re-assigned null after each and every invocation // shell.printAndRun(). This resetting to null is done INSIDE class // OSCommands. // // Note that, here, we are currently inside of a for-loop, and this instruction // will be executed each time the for-loop iterates - and, of course, when // 'cpStrLen' is greater than zero (meaning that the current 'BuildPackage' // being iterated by the loop isn't in the CWD from whence the Builder class was // executed, but rather some sub-directory, which is the whole reason an 'osExtras' // instance is necessary) osExtras.currentWorkingDirectory = new File(bp.classPathLocation); shell.osExtras = osExtras; } // Run the Command using class Shell. "CHECK" will actually halt the entire JRE using // System.exit if there were any errors (and print a thoughtfully worded error-message) CHECK(shell.COMMAND("jar", JAR_COMMAND)); // This prints a total of how many files were just inserted into the jar printTotals(jarFilesArr, logAndScreen); logOnly.append(Printing.TAR_JAR_DIVIDER); firstIteration = false; } // This chunk-o-stuff was relocated to its own dedicated method. The method simply // iterates the "jarIncludes" ReadOnlyList, and adds each directory listed in the list // directly into the Jar-File. A "JarInclude" is stuff like the "META-INF" - but it may be // essentially anything else that needs to be placed into the '.jar' // // The user controls / configures this idea by instantiating some "JarInclude's", and // putting into the Builder's Configuration-Constructor class "Config" if ((builder.jarIncludes != null) && (builder.jarIncludes.size() > 0)) addJarExtras( builder.jarIncludes, JAR_FILE_NAME_ABSOLUTE, shell, osExtras, logAndScreen, logOnly ); } // ******************************************************************************************** // ******************************************************************************************** // HELPERS FOR: "Build JAR-File" // ******************************************************************************************** // ******************************************************************************************** private static final FilenameFilter DEFAULT_DATA_DIR_FILTER = (File dir, String name) -> StrCmpr.endsWithNAND(name, ".java", ".class"); // Each 'BuildPackage' that is included by the 'Builder' may contain a `data-files` directory. // When there is a `data-files` directory, each file in that directory ALSO NEEDS TO BE ADDED // into the JAR. // // If the user so chooses to provide a DataFilesList.txt file inside his Package's data-files/ // directory, he may do so. All this file is intended to do is specify which files inside that // directory actually need to be inserted into the JAR. // // If there is no 'DataFilesList.txt' file, then **ALL** files in the data-files/ subdirectory // are inserted into the '.jar' private static void addDataFilesDir( final String fDataDirName, final Stream.Builder<String> b, final AppendableSafe logAndScreen ) throws IOException { logAndScreen.append (" Adding '" + BCYAN + "data-files/" + RESET + "' Directory-Contents\n"); final String fDataFilesListFileName = fDataDirName + "DataFilesList.txt"; final File fDataFilesListFile = new File(fDataFilesListFileName); // Check if there is a src-package/data-files/DataFilesList.txt file // If there is a "DataFilesList.txt" file, convert that into a File-Name List. if (fDataFilesListFile.exists() && fDataFilesListFile.isFile()) FileRW .loadFileToStream(fDataFilesListFileName, false) .map((String line) -> line.trim()) .filter((String line) -> line.length() > 0) .filter((String line) -> ! line.startsWith("#")) .map((String fileName) -> fDataDirName + fileName) .forEachOrdered((String dataFileName) -> b.accept(dataFileName)); else FileNode .createRoot(fDataDirName) .loadTree(-1, DEFAULT_DATA_DIR_FILTER, null) .flattenJustFiles(RTC.FULLPATH_STREAM_BUILDER(b)); } // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** // Add the '.jar' File-Extras. (Uses the "JarInclude" User Data-Configuration Class) // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** // // Some examples of "Jar-Extras" can (hopefully / probably) be seen with the Jar-Includes from // Java-HTML. These are things like the "META-INF" directories, and the Data-File Directory. // // final FileNodeFilter FILTER = (FileNode fn) -> // StrCmpr.endsWithNAND(fn.name, ".java", ".class"); // return new JarInclude() // .add("", "Torello/Data/", true, FILTER, TRUE_FILTER) // .add("Torello/BuildJAR/IncludeFiles/", "META-INF/", true, TRUE_FILTER, TRUE_FILTER) // .add("Torello/etc/External/", "META-INF/", true, TRUE_FILTER, TRUE_FILTER); private static void addJarExtras( final Iterable<JarInclude.Descriptor> includes, final String JAR_FILE_NAME_ABSOLUTE, final Shell shell, final OSExtras osExtras, final AppendableSafe logAndScreen, final StringBuilder logOnly ) throws IOException { for (JarInclude.Descriptor jid : includes) { logAndScreen.append("Adding Jar-Include: " + jid.toString() + '\n'); final int cpStrLen = jid.workingDirectory.length(); final FileNode tempFN = FileNode .createRoot(jid.workingDirectory + jid.subDirectory) .loadTree((jid.traverseTree ? -1 : 0), null, null); final String[] extraFiles = (cpStrLen == 0) ? tempFN.flatten (RTC.FULLPATH_ARRAY(), -1, jid.fileFilter, true, jid.dirFilter, false) : tempFN .flatten(RTC.FULLPATH_STREAM(), -1, jid.fileFilter, true, jid.dirFilter, false) .map((String fileName) -> fileName.substring(cpStrLen)) .toArray(String[]::new); String[] JAR_COMMAND = new String[2 + extraFiles.length]; JAR_COMMAND[0] = "-uvf"; JAR_COMMAND[1] = JAR_FILE_NAME_ABSOLUTE; System.arraycopy(extraFiles, 0, JAR_COMMAND, 2, extraFiles.length); if (cpStrLen > 0) { osExtras.currentWorkingDirectory = new File(jid.workingDirectory); shell.osExtras = osExtras; } CHECK(shell.COMMAND("jar", JAR_COMMAND)); printTotals(extraFiles, logAndScreen); logOnly.append(Printing.TAR_JAR_DIVIDER); } } // ******************************************************************************************** // ******************************************************************************************** // JAR-File: MORE HELPER's. Simpler ones // ******************************************************************************************** // ******************************************************************************************** private static void CHECK(OSResponse osr) { if (osr.errorOutput.length() > 0) { System.out.println (BRED + "\nTEXT PRINTED TO STANDARD ERROR:\n" + RESET + osr.errorOutput); Util.ERROR_EXIT("Build Tar-Jar (using Regular Shell)"); } } // This allows for '.class' files, but also includes anything else in the directory - for // instance ".properties" files, or other stuff that may or may not have been left there. It // seems a little questionable, but I cannot think of any generalized rule for this. So, in // the end, it just makes sure to leave out the '.java' file // // Remember: javax.json has a ".properties" file... That's really the SINGLE-ONLY REASON that // this doesn't just say instead (more specifically): name.endsWith(".class") private static final FilenameFilter JAR_FILENAME_FILTER = (File file, String name) -> ! name.endsWith(".java"); // This isn't used at all - EXCEPT when a 'BuildPackage' claims to have "Sub-packages" // "SubPackages" means that the entire directory-tree rooted at bp.pkgRootDirectory (and all of // its '.class' files) needs to be included in the '.jar' // // NOTE: This essentially talking about the Packages: JDUInternal and BuildJAR // FURTHERMORE: Since 'BuildJAR' isn't put into the '.jar', this is only used for JDUInternal // // The last addition to this thing was to make it print out the directories that match so that // the build script tells the user all of the sub-packages. It looks nice, however, whereas // this used to be a 'FileFilter' **FIELD**, it is now a **METHOD** that returns a 'FileFilter' private static final FileFilter getSubPackageDirFilter(AppendableSafe logAndScreen) { // This is creating a Lambda/Predicate that **ALSO** prints out the directories that match return (File file) -> { final String path = file.getPath(); final boolean ret = ! StrCmpr.containsOR (path, "upgrade-files", "doc-files", "package-source"); if (ret) logAndScreen.append (" Adding Sub-Package Files from: " + BCYAN + path + RESET + '\n'); return ret; }; } // Just prints out a count of how many files (and the file types) that were just inserted // on the most recent loop-iteration, into the JAR. private static void printTotals(String[] files, Appendable a) throws IOException { TreeMap<String, Integer> tm = new TreeMap<>(); for (String fileName : files) { int slashPos = fileName.lastIndexOf(FS); int dotPos = fileName.lastIndexOf('.'); final String fileExt = ((dotPos == -1) || (slashPos > dotPos)) ? "NOEXT" : fileName.substring(dotPos); tm.compute( fileExt, (String dummy, Integer count) -> (count == null) ? 1 : (count+1) ); } for (String fileExt : tm.keySet()) a.append( " Added " + BGREEN + tm.get(fileExt) + RESET + (fileExt.equals("NOEXT") ? (BRED + " Other " + RESET) : (" '" + BYELLOW + fileExt + RESET + "' ")) + "Files.\n" ); // for (String fileName : files) System.out.print(fileName + ", "); // System.out.println(); a.append('\n'); } } |