Skip to content

plugins: support for overriding layout-provided launcher class #2620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ public class SpringBootPluginExtension {
}
}

/**
* Launcher class, replaces the default specified by layout. Optional.
*/
String launcherClass;

/**
* The main class that should be run. Instead of setting this explicitly you can use the
* 'mainClassName' of the project or the 'main' of the 'run' task. If not specified the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ private void repackage(File file) {
}
Repackager repackager = new LoggingRepackager(file);
setMainClass(repackager);
setLauncherClass(repackager);
if (this.extension.convertLayout() != null) {
repackager.setLayout(this.extension.convertLayout());
}
Expand Down Expand Up @@ -201,6 +202,12 @@ else if (getProject().getTasks().getByName("run").hasProperty("main")) {
getLogger().info("Setting mainClass: " + mainClass);
repackager.setMainClass(mainClass);
}

private void setLauncherClass(Repackager repackager) {
String launcherClass = extension.getLauncherClass();
getLogger().info("Setting mainClass: " + launcherClass);
repackager.setLauncherClass(launcherClass);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class Repackager {

private static final byte[] ZIP_FILE_HEADER = new byte[] { 'P', 'K', 3, 4 };

private String launcherClass;

private String mainClass;

private boolean backupSource = true;
Expand All @@ -58,6 +60,10 @@ public Repackager(File source) {
this.layout = Layouts.forFile(source);
}

public void setLauncherClass(String launcherClass) {
this.launcherClass = launcherClass;
}

/**
* Sets the main class that should be run. If not specified the value from the
* MANIFEST will be used, or if no manifest entry is found the archive will be
Expand Down Expand Up @@ -229,7 +235,9 @@ private Manifest buildManifest(JarFile source) throws IOException {
if (startClass == null) {
startClass = findMainMethod(source);
}
String launcherClassName = this.layout.getLauncherClassName();
String launcherClassName = (launcherClass != null)
? launcherClass
: layout.getLauncherClassName();
if (launcherClassName != null) {
manifest.getMainAttributes()
.putValue(MAIN_CLASS_ATTRIBUTE, launcherClassName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ public class RepackageMojo extends AbstractDependencyFilterMojo {
@Parameter
private String classifier;

/**
* Name of the launcher class; overrides the one specified by layout.
*/
@Parameter
private String launcherClass;

/**
* The name of the main class. If not specified the first compiled class found that
* contains a 'main' method will be used.
Expand Down Expand Up @@ -156,6 +162,7 @@ protected String findMainMethod(JarFile source) throws IOException {
}
};
repackager.setMainClass(this.mainClass);
repackager.setLauncherClass(this.launcherClass);
if (this.layout != null) {
getLog().info("Layout: " + this.layout);
repackager.setLayout(this.layout.layout());
Expand Down