0
# JAR/WAR Repackaging
1
2
Core functionality for transforming regular JAR/WAR files into executable Spring Boot archives. The repackaging process handles main class detection, library inclusion, manifest generation, and loader class integration to create self-contained executable files.
3
4
## Capabilities
5
6
### Basic Repackaging
7
8
Transform a regular JAR into an executable Spring Boot JAR with embedded dependencies and loader classes.
9
10
```java { .api }
11
public class Repackager {
12
/**
13
* Create a repackager for the given source file.
14
*
15
* @param source the source JAR or WAR file to repackage
16
*/
17
public Repackager(File source);
18
19
/**
20
* Repackage the source file with the given libraries.
21
* Creates a backup of the original file and replaces it with the repackaged version.
22
*
23
* @param libraries the libraries to include in the repackaged file
24
* @throws IOException if the repackaging fails
25
*/
26
public void repackage(Libraries libraries) throws IOException;
27
28
/**
29
* Repackage the source file to a specific destination with the given libraries.
30
*
31
* @param destination the destination file for the repackaged archive
32
* @param libraries the libraries to include in the repackaged file
33
* @throws IOException if the repackaging fails
34
*/
35
public void repackage(File destination, Libraries libraries) throws IOException;
36
37
/**
38
* Repackage with launch script support for Unix-like systems.
39
*
40
* @param destination the destination file
41
* @param libraries the libraries to include
42
* @param launchScript the launch script to prepend to the JAR
43
* @throws IOException if the repackaging fails
44
*/
45
public void repackage(File destination, Libraries libraries, LaunchScript launchScript) throws IOException;
46
47
/**
48
* Full control repackaging with launch script and custom modification time.
49
*
50
* @param destination the destination file
51
* @param libraries the libraries to include
52
* @param launchScript the launch script to prepend
53
* @param lastModifiedTime the last modified time to set on entries
54
* @throws IOException if the repackaging fails
55
*/
56
public void repackage(File destination, Libraries libraries, LaunchScript launchScript, FileTime lastModifiedTime) throws IOException;
57
58
/**
59
* Configure whether to create a backup of the source file.
60
*
61
* @param backupSource true to create a backup, false otherwise
62
*/
63
public void setBackupSource(boolean backupSource);
64
}
65
```
66
67
### Advanced Packager
68
69
Abstract base class providing extensive customization options for the packaging process.
70
71
```java { .api }
72
public abstract class Packager {
73
/**
74
* Create a packager for the given source file.
75
*
76
* @param source the source file to package
77
*/
78
protected Packager(File source);
79
80
/**
81
* Set the main class for the executable archive.
82
* If not set, the main class will be automatically detected.
83
*
84
* @param mainClass the fully qualified main class name
85
*/
86
public void setMainClass(String mainClass);
87
88
/**
89
* Set the layout strategy for organizing archive contents.
90
*
91
* @param layout the layout to use
92
*/
93
public void setLayout(Layout layout);
94
95
/**
96
* Set the loader implementation to use.
97
*
98
* @param loaderImplementation the loader implementation
99
*/
100
public void setLoaderImplementation(LoaderImplementation loaderImplementation);
101
102
/**
103
* Set the factory for creating layouts.
104
*
105
* @param layoutFactory the layout factory
106
*/
107
public void setLayoutFactory(LayoutFactory layoutFactory);
108
109
/**
110
* Set the layers configuration for Docker optimization.
111
*
112
* @param layers the layers configuration
113
*/
114
public void setLayers(Layers layers);
115
116
/**
117
* Configure whether to include relevant JAR mode libraries.
118
*
119
* @param includeRelevantJarModeJars true to include JAR mode libraries
120
*/
121
public void setIncludeRelevantJarModeJars(boolean includeRelevantJarModeJars);
122
123
/**
124
* Get the backup file location.
125
*
126
* @return the backup file location
127
*/
128
public File getBackupFile();
129
130
/**
131
* Add a listener for main class timeout warnings.
132
*
133
* @param listener the warning listener
134
*/
135
public void addMainClassTimeoutWarningListener(MainClassTimeoutWarningListener listener);
136
}
137
```
138
139
### Image Packaging
140
141
Specialized packager for creating container images with custom export handling.
142
143
```java { .api }
144
public class ImagePackager extends Packager {
145
/**
146
* Create an image packager for the given source file.
147
*
148
* @param source the source file to package
149
*/
150
public ImagePackager(File source);
151
152
/**
153
* Package for image creation with custom entry export handling.
154
*
155
* @param libraries the libraries to include
156
* @param exporter custom handler for processing ZIP entries
157
* @throws IOException if packaging fails
158
*/
159
public void packageImage(Libraries libraries, BiConsumer<ZipEntry, EntryWriter> exporter) throws IOException;
160
}
161
```
162
163
## Types
164
165
```java { .api }
166
@FunctionalInterface
167
public interface MainClassTimeoutWarningListener {
168
/**
169
* Called when main class detection times out.
170
*
171
* @param duration the time taken for the search
172
* @param mainClass the main class that was found (may be null)
173
*/
174
void handleTimeoutWarning(Duration duration, String mainClass);
175
}
176
```
177
178
## Usage Examples
179
180
### Basic Repackaging
181
182
```java
183
import org.springframework.boot.loader.tools.Repackager;
184
import org.springframework.boot.loader.tools.Libraries;
185
import java.io.File;
186
187
File sourceJar = new File("myapp.jar");
188
Repackager repackager = new Repackager(sourceJar);
189
190
// Repackage with no additional libraries
191
repackager.repackage(Libraries.NONE);
192
193
// The original JAR is backed up and replaced with the executable version
194
// Run with: java -jar myapp.jar
195
```
196
197
### Repackaging with Libraries
198
199
```java
200
import org.springframework.boot.loader.tools.*;
201
import java.io.File;
202
import java.util.List;
203
204
File sourceJar = new File("myapp.jar");
205
File destination = new File("myapp-executable.jar");
206
Repackager repackager = new Repackager(sourceJar);
207
208
// Create libraries collection
209
List<File> libraryFiles = List.of(
210
new File("lib/spring-boot.jar"),
211
new File("lib/spring-context.jar"),
212
new File("lib/logback-classic.jar")
213
);
214
215
Libraries libraries = callback -> {
216
for (File libFile : libraryFiles) {
217
LibraryScope scope = libFile.getName().contains("test")
218
? LibraryScope.PROVIDED
219
: LibraryScope.COMPILE;
220
callback.library(new Library(libFile, scope));
221
}
222
};
223
224
// Repackage to specific destination
225
repackager.repackage(destination, libraries);
226
```
227
228
### Advanced Configuration
229
230
```java
231
import org.springframework.boot.loader.tools.*;
232
import java.io.File;
233
import java.util.Map;
234
235
File sourceJar = new File("myapp.jar");
236
Repackager repackager = new Repackager(sourceJar);
237
238
// Configure advanced options
239
repackager.setMainClass("com.example.MyApplication");
240
repackager.setLayout(Layouts.JAR);
241
repackager.setLoaderImplementation(LoaderImplementation.DEFAULT);
242
repackager.setBackupSource(true);
243
244
// Add timeout warning listener
245
repackager.addMainClassTimeoutWarningListener((duration, mainClass) -> {
246
System.out.println("Main class detection took " + duration.toMillis() + "ms");
247
if (mainClass == null) {
248
System.out.println("No main class found!");
249
}
250
});
251
252
// Create launch script for Unix systems
253
File scriptTemplate = new File("launch-script.sh");
254
Map<String, String> properties = Map.of(
255
"initInfoProvides", "myapp",
256
"initInfoShortDescription", "My Spring Boot Application"
257
);
258
LaunchScript launchScript = new DefaultLaunchScript(scriptTemplate, properties);
259
260
// Repackage with launch script
261
File destination = new File("myapp-executable.jar");
262
repackager.repackage(destination, Libraries.NONE, launchScript);
263
264
// The resulting JAR can be executed directly: ./myapp-executable.jar
265
```