Initial Commit
This commit is contained in:
84
src/main/java/com/hypherionmc/jarmanager/JarManager.java
Normal file
84
src/main/java/com/hypherionmc/jarmanager/JarManager.java
Normal file
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* This file is part of JarManager, licensed under The MIT License (MIT).
|
||||
* Copyright HypherionSA and Contributors
|
||||
*/
|
||||
package com.hypherionmc.jarmanager;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import me.lucko.jarrelocator.JarRelocator;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.zip.Deflater;
|
||||
|
||||
/**
|
||||
* @author HypherionSA
|
||||
* Main JarManager class. Allows you to enable debugging, set the compression level and is a central place to run jar tasks
|
||||
*/
|
||||
@NoArgsConstructor(staticName = "getInstance")
|
||||
public final class JarManager {
|
||||
|
||||
/**
|
||||
* Enable additional debug logging
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
private boolean debugMode = false;
|
||||
|
||||
/**
|
||||
* Set the compression level for the output jar
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
private int compressionLevel = Deflater.DEFAULT_COMPRESSION;
|
||||
|
||||
/**
|
||||
* Pack a directory into a jar file
|
||||
* @param inputDirectory - The directory containing the content for the jar
|
||||
* @param outputJar - The file that the jar will be written to
|
||||
* @return - The packaged jar file
|
||||
* @throws IOException - Thrown when an IO error occurs
|
||||
*/
|
||||
public File packJar(File inputDirectory, File outputJar) throws IOException {
|
||||
JarPackageTask task = new JarPackageTask(this, inputDirectory, outputJar);
|
||||
return task.pack();
|
||||
}
|
||||
|
||||
/**
|
||||
* Unpack a jar file into a directory
|
||||
* @param inputJar - The input jar file to be unpacked
|
||||
* @param outputDirectory - The directory the jar file will be unpacked to
|
||||
* @return - A list of unpacked files and directories
|
||||
* @throws IOException - Thrown when an IO error occurs
|
||||
*/
|
||||
public List<File> unpackJar(File inputJar, File outputDirectory) throws IOException {
|
||||
JarUnpackTask task = new JarUnpackTask(this, inputJar, outputDirectory);
|
||||
return task.unpackJar();
|
||||
}
|
||||
|
||||
/**
|
||||
* Relocate or remap packages inside a jar file.
|
||||
* For example, from com.google.gson to lib.com.google.gson
|
||||
* @param inputJar - The input jar file that will be modified
|
||||
* @param outputJar - The file the modified file will be saved to
|
||||
* @param relocations - Packages to relocate. See example above
|
||||
* @throws IOException - Thrown when an IO error occurs
|
||||
*/
|
||||
public void remapJar(File inputJar, File outputJar, HashMap<String, String> relocations) throws IOException {
|
||||
// Set up temp file, to prevent accidental overwrites
|
||||
File tempJar = new File(inputJar.getParentFile(), "tempRelocated.jar");
|
||||
|
||||
// Run the jar relocater task
|
||||
JarRelocator relocator = new JarRelocator(inputJar, tempJar, relocations);
|
||||
relocator.run();
|
||||
|
||||
// Move the temporary file to the outputJar
|
||||
Files.move(tempJar.toPath(), outputJar.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
}
|
180
src/main/java/com/hypherionmc/jarmanager/JarPackageTask.java
Normal file
180
src/main/java/com/hypherionmc/jarmanager/JarPackageTask.java
Normal file
@@ -0,0 +1,180 @@
|
||||
/*
|
||||
* This file is part of JarManager, licensed under The MIT License (MIT).
|
||||
* Copyright HypherionSA and Contributors
|
||||
*/
|
||||
package com.hypherionmc.jarmanager;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.file.Files;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarOutputStream;
|
||||
|
||||
/**
|
||||
* @author HypherionSA
|
||||
* Internal task to perform the jar packing action. Should never be used directly.
|
||||
* Should be invoked from {@link JarManager#packJar(File, File)}
|
||||
*/
|
||||
final class JarPackageTask {
|
||||
|
||||
// JarManager instance
|
||||
private final JarManager jarManager;
|
||||
|
||||
// Output stream
|
||||
private JarOutputStream outputStream;
|
||||
|
||||
// Files
|
||||
private final File inputDirectory;
|
||||
private final File outputJar;
|
||||
|
||||
/**
|
||||
* Internal. Should not be used directly.
|
||||
* Create a new instance of the jar packager task
|
||||
* @param jarManager - Initialized instance of {@link JarManager}
|
||||
* @param inputDirectory The input directory that will be packed
|
||||
* @param outputJar - The output file the jar will be written to
|
||||
*/
|
||||
JarPackageTask(JarManager jarManager, File inputDirectory, File outputJar) {
|
||||
this.jarManager = jarManager;
|
||||
this.inputDirectory = inputDirectory;
|
||||
this.outputJar = outputJar;
|
||||
|
||||
if (!inputDirectory.exists())
|
||||
throw new IllegalStateException("inputDirectory does not exist");
|
||||
|
||||
if (!inputDirectory.isDirectory())
|
||||
throw new IllegalStateException("inputDirectory is not a directory");
|
||||
|
||||
if (outputJar == null)
|
||||
throw new IllegalStateException("outputJar cannot be null!");
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the package task
|
||||
* @return - The output jar that was packaged
|
||||
* @throws IOException - Thrown when an IO error occurs
|
||||
*/
|
||||
File pack() throws IOException {
|
||||
// Set up the output stream and set the chosen compression level
|
||||
this.outputStream = new JarOutputStream(Files.newOutputStream(outputJar.toPath()));
|
||||
this.outputStream.setLevel(jarManager.getCompressionLevel());
|
||||
|
||||
// Debug Logging
|
||||
if (jarManager.isDebugMode())
|
||||
System.out.println("Packing jar " + outputJar.getName());
|
||||
|
||||
// Processes the folder to add all files and directories to the output jar
|
||||
this.addFilesRecursively(inputDirectory, null);
|
||||
|
||||
// Debug Logging
|
||||
if (jarManager.isDebugMode())
|
||||
System.out.println("Packed jar " + outputJar.getName());
|
||||
|
||||
// Close the output stream
|
||||
try {
|
||||
this.outputStream.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// Return the new jar
|
||||
return outputJar;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a file to the output jar
|
||||
* @param source - The file to be added
|
||||
* @param outputDir - The output directory. Used to fix file names in the output jar
|
||||
*/
|
||||
private void addFile(File source, File outputDir) {
|
||||
// Input reader stream
|
||||
BufferedInputStream inputStream;
|
||||
|
||||
try {
|
||||
// Get the correct file name for the final jar
|
||||
String sourcePath = source.getAbsolutePath().substring(outputDir.getAbsolutePath().length() + 1);
|
||||
|
||||
// Source is a file. Process it
|
||||
if (!source.isDirectory()) {
|
||||
// Set up a new jar entry
|
||||
JarEntry entry = new JarEntry(sourcePath.replace("\\", "/"));
|
||||
entry.setTime(source.lastModified());
|
||||
this.outputStream.putNextEntry(entry);
|
||||
|
||||
// Set up the input stream and buffer
|
||||
inputStream = new BufferedInputStream(Files.newInputStream(source.toPath()));
|
||||
byte[] buffer = new byte[1024];
|
||||
int count;
|
||||
|
||||
// Read and write the file to the jar
|
||||
while ((count = inputStream.read(buffer)) != -1) {
|
||||
this.outputStream.write(buffer, 0, count);
|
||||
}
|
||||
|
||||
// Close the input/output stream
|
||||
this.outputStream.closeEntry();
|
||||
inputStream.close();
|
||||
return;
|
||||
}
|
||||
|
||||
// Source is a directory. Add it to the jar, and process its files
|
||||
String name = sourcePath.replace("\\", "/");
|
||||
if (!name.isEmpty()) {
|
||||
// Add a trailing slash to the file name if there isn't one
|
||||
if (!name.endsWith("/")) {
|
||||
name = name + "/";
|
||||
}
|
||||
|
||||
// Write the jar entry to the jar
|
||||
JarEntry entry = new JarEntry(name);
|
||||
entry.setTime(source.lastModified());
|
||||
this.outputStream.putNextEntry(entry);
|
||||
|
||||
// Close the entry
|
||||
this.outputStream.closeEntry();
|
||||
}
|
||||
|
||||
// Loop over files in the directory
|
||||
File[] files = source.listFiles();
|
||||
if (files == null)
|
||||
return;
|
||||
|
||||
for (File f : files) {
|
||||
// Add the files to the output jar
|
||||
this.addFile(f, outputDir);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loop over files and folders in the inputDir to add them to the jar file
|
||||
* @param inputDir - The input directory containing the files that will be added to the output jar
|
||||
* @param outputDir - The directory holding the output. Used to fix file names in the output jar
|
||||
*/
|
||||
private void addFilesRecursively(File inputDir, File outputDir) {
|
||||
if (outputDir == null)
|
||||
outputDir = inputDir;
|
||||
|
||||
// Get a list of files and folders from the directory
|
||||
File[] files = inputDir.listFiles();
|
||||
if (files == null) {
|
||||
if (jarManager.isDebugMode())
|
||||
System.err.println("inputDirectory doesn't appear to contain files. Skipping...");
|
||||
return;
|
||||
}
|
||||
|
||||
for (File f : files) {
|
||||
// It's a file. Add it
|
||||
if (f.isFile()) {
|
||||
this.addFile(f, outputDir);
|
||||
continue;
|
||||
}
|
||||
|
||||
// It's a folder. Process it to find files, to add to the jar
|
||||
if (f.isDirectory())
|
||||
this.addFilesRecursively(f, outputDir);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
123
src/main/java/com/hypherionmc/jarmanager/JarUnpackTask.java
Normal file
123
src/main/java/com/hypherionmc/jarmanager/JarUnpackTask.java
Normal file
@@ -0,0 +1,123 @@
|
||||
/*
|
||||
* This file is part of JarManager, licensed under The MIT License (MIT).
|
||||
* Copyright HypherionSA and Contributors
|
||||
*/
|
||||
package com.hypherionmc.jarmanager;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarFile;
|
||||
|
||||
/**
|
||||
* @author HypherionSA
|
||||
* Internal task to perform the jar unpacking action. Should never be used directly.
|
||||
* Should be invoked from {@link JarManager#unpackJar(File, File)}
|
||||
*/
|
||||
final class JarUnpackTask {
|
||||
|
||||
// JarManager instance
|
||||
private final JarManager manager;
|
||||
|
||||
// List of unpacked files and directories that will be returned
|
||||
private final List<File> files = new ArrayList<>();
|
||||
|
||||
// Files
|
||||
private final File inputJar;
|
||||
private final File outputDirectory;
|
||||
|
||||
/**
|
||||
* Internal. Should not be used directly.
|
||||
* Create a new instance of the jar unpacker task
|
||||
* @param manager - Initialized instance of {@link JarManager}
|
||||
* @param inputJar - The input jar that will be unpacked
|
||||
* @param outputDirectory - The directory the jar will be unpacked to
|
||||
*/
|
||||
JarUnpackTask(JarManager manager, File inputJar, File outputDirectory) {
|
||||
this.manager = manager;
|
||||
this.inputJar = inputJar;
|
||||
this.outputDirectory = outputDirectory;
|
||||
|
||||
if (!inputJar.exists())
|
||||
throw new IllegalStateException("inputJar does not exist");
|
||||
|
||||
if (!inputJar.isFile())
|
||||
throw new IllegalStateException("inputJar is not a file");
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the unpack task
|
||||
* @return - Returns a list of unpacked files and directories
|
||||
* @throws IOException - Thrown when an IO error occurs
|
||||
*/
|
||||
List<File> unpackJar() throws IOException {
|
||||
// Open the input file for processing
|
||||
try (JarFile jarFile = new JarFile(inputJar)) {
|
||||
// Debug Logging
|
||||
if (manager.isDebugMode())
|
||||
System.out.println("Unpacking " + jarFile.getName() + "...");
|
||||
|
||||
// Get a list of entries from the input jar
|
||||
Enumeration<JarEntry> enumEntries = jarFile.entries();
|
||||
|
||||
// Create output directories
|
||||
if (!outputDirectory.exists()) {
|
||||
boolean created = outputDirectory.mkdirs();
|
||||
|
||||
if (created && manager.isDebugMode())
|
||||
System.out.println("Created output directory " + outputDirectory.getAbsolutePath());
|
||||
|
||||
if (!created && manager.isDebugMode())
|
||||
System.out.println("Failed to create output directory " + outputDirectory.getAbsolutePath());
|
||||
}
|
||||
|
||||
// Process the entries
|
||||
while (enumEntries.hasMoreElements()) {
|
||||
JarEntry entry = enumEntries.nextElement();
|
||||
|
||||
// Entry is a directory. Skip and continue
|
||||
if (entry.isDirectory())
|
||||
continue;
|
||||
|
||||
// The output file that will be written
|
||||
File outFile = new File(outputDirectory, entry.getName());
|
||||
|
||||
// Debug Logging
|
||||
if (manager.isDebugMode()) {
|
||||
System.out.println("[Unpacking] " + entry.getName() + " => " + outFile.getAbsolutePath());
|
||||
}
|
||||
|
||||
// Create output directory for the file
|
||||
outFile.getParentFile().mkdirs();
|
||||
|
||||
// Create the read and write streams
|
||||
InputStream inputStream = jarFile.getInputStream(entry);
|
||||
FileOutputStream outputStream = new FileOutputStream(outFile);
|
||||
|
||||
// Initialize the buffer
|
||||
byte[] buffer = new byte[1024];
|
||||
int read;
|
||||
|
||||
// Read the entry from the jar and write to the output directory
|
||||
while ((read = inputStream.read(buffer)) != -1) {
|
||||
outputStream.write(buffer, 0, read);
|
||||
}
|
||||
|
||||
// Add the processed entry to the return list
|
||||
this.files.add(outFile);
|
||||
|
||||
// Close everything
|
||||
outputStream.close();
|
||||
inputStream.close();
|
||||
}
|
||||
}
|
||||
|
||||
return this.files;
|
||||
}
|
||||
|
||||
}
|
72
src/test/java/JarTests.java
Normal file
72
src/test/java/JarTests.java
Normal file
@@ -0,0 +1,72 @@
|
||||
import com.hypherionmc.jarmanager.JarManager;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.zip.Deflater;
|
||||
|
||||
public class JarTests {
|
||||
|
||||
private static final File testDirectory = new File("testdir");
|
||||
private static final File outputDirectory = new File(testDirectory, "output");
|
||||
|
||||
public void unpackJar() throws IOException {
|
||||
File testDirectory = new File("testdir");
|
||||
File outputDirectory = new File(testDirectory, "output");
|
||||
|
||||
if (!outputDirectory.exists())
|
||||
outputDirectory.mkdirs();
|
||||
|
||||
// Create a JarManager instance
|
||||
JarManager manager = JarManager.getInstance();
|
||||
|
||||
// Unpack the Jar
|
||||
manager.unpackJar(new File(testDirectory, "input.jar"), outputDirectory);
|
||||
}
|
||||
|
||||
public void repackJar() throws IOException {
|
||||
File testDirectory = new File("testdir");
|
||||
File inputDirectory = new File(testDirectory, "output");
|
||||
|
||||
// Create a JarManager instance
|
||||
JarManager manager = JarManager.getInstance();
|
||||
|
||||
// Unpack the Jar
|
||||
manager.packJar(inputDirectory, new File(testDirectory, "output.jar"));
|
||||
}
|
||||
|
||||
public void remapJar() throws IOException {
|
||||
File testDirectory = new File("testdir");
|
||||
File inputFile = new File(testDirectory, "input.jar");
|
||||
|
||||
// Create a JarManager instance
|
||||
JarManager manager = JarManager.getInstance();
|
||||
|
||||
// Remap the Jar
|
||||
HashMap<String, String> rl = new HashMap<>();
|
||||
rl.put("com.gitlab.cdagaming", "test.com.gitlab.cdagaming");
|
||||
manager.remapJar(inputFile, new File(testDirectory, "remapped.jar"), rl);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
if (!outputDirectory.exists())
|
||||
outputDirectory.mkdirs();
|
||||
|
||||
File inputJar = new File(testDirectory, "forge.jar");
|
||||
File outputJar = new File(testDirectory, "forge-output.jar");
|
||||
|
||||
// Unpack Jar
|
||||
JarManager manager = JarManager.getInstance();
|
||||
manager.unpackJar(inputJar, outputDirectory);
|
||||
|
||||
// Repack jar with best compression
|
||||
manager.setCompressionLevel(Deflater.BEST_COMPRESSION);
|
||||
manager.packJar(outputDirectory, outputJar);
|
||||
|
||||
// Relocation Test
|
||||
HashMap<String, String> rl = new HashMap<>();
|
||||
rl.put("com.gitlab.cdagaming", "test.com.gitlab.cdagaming");
|
||||
manager.remapJar(outputJar, new File(testDirectory, "outputJar.jar"), rl);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user