Skip to content

Commit

Permalink
Support overriding mainClass provided by JavaApplication (#1182)
Browse files Browse the repository at this point in the history
* Add canOverrideMainClassAttrInManifestBlock

* Check attr key before injecting

* Note this change
  • Loading branch information
Goooler authored Jan 23, 2025
1 parent ee55936 commit cb08953
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/docs/changes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

## [Unreleased]

**Fixed**

- Support overriding `mainClass` provided by `JavaApplication`. ([#1182](https://github.com/GradleUp/shadow/pull/1182))


## [v9.0.0-beta6] (2025-01-23)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import assertk.assertions.exists
import assertk.assertions.isEqualTo
import com.github.jengelman.gradle.plugins.shadow.ShadowApplicationPlugin.Companion.SHADOW_INSTALL_TASK_NAME
import com.github.jengelman.gradle.plugins.shadow.ShadowApplicationPlugin.Companion.SHADOW_RUN_TASK_NAME
import com.github.jengelman.gradle.plugins.shadow.util.Issue
import com.github.jengelman.gradle.plugins.shadow.util.JarPath
import com.github.jengelman.gradle.plugins.shadow.util.containsEntries
import com.github.jengelman.gradle.plugins.shadow.util.getContent
Expand Down Expand Up @@ -45,7 +46,7 @@ class ApplicationPluginTest : BasePluginTest() {

assertThat(result.output).contains(
"Running application with JDK 17",
"Hello, World! (foo)",
"Hello, World! (foo) from Main",
)

commonAssertions(jarPath("build/install/myapp-shadow/lib/myapp-1.0-all.jar"))
Expand Down Expand Up @@ -108,6 +109,33 @@ class ApplicationPluginTest : BasePluginTest() {
commonAssertions(jarPath("build/install/myapp-shadow/lib/myapp-1.0-all.jar"))
}

@Issue(
"https://github.com/GradleUp/shadow/issues/613",
)
@Test
fun canOverrideMainClassAttrInManifestBlock() {
writeMainClass(className = "Main2")
prepare(
projectBlock = """
shadowJar {
manifest {
attributes 'Main-Class': 'shadow.Main2'
}
}
""".trimIndent(),
)

val result = run(SHADOW_RUN_TASK_NAME)

assertThat(result.output).contains(
"Hello, World! (foo) from Main2",
)
commonAssertions(
jarPath("build/install/myapp-shadow/lib/myapp-1.0-all.jar"),
mainClassAttr = "shadow.Main2",
)
}

private fun prepare(
projectBlock: String = "",
settingsBlock: String = "",
Expand Down Expand Up @@ -142,10 +170,11 @@ class ApplicationPluginTest : BasePluginTest() {
private fun commonAssertions(
jarPath: JarPath,
entriesContained: Array<String> = arrayOf("a.properties", "a2.properties", "shadow/Main.class"),
mainClassAttr: String = "shadow.Main",
) {
assertThat(jarPath).useAll {
containsEntries(*entriesContained)
getMainAttr("Main-Class").isEqualTo("shadow.Main")
getMainAttr("Main-Class").isEqualTo(mainClassAttr)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,17 @@ abstract class BasePluginTest {

fun writeMainClass(
withImports: Boolean = false,
className: String = "Main",
) {
val imports = if (withImports) "import junit.framework.Test;" else ""

path("src/main/java/shadow/Main.java").writeText(
path("src/main/java/shadow/$className.java").writeText(
"""
package shadow;
$imports
public class Main {
public class $className {
public static void main(String[] args) {
String content = String.format("Hello, World! (%s)", args);
String content = String.format("Hello, World! (%s) from $className", args);
System.out.println(content);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ public abstract class ShadowApplicationPlugin : Plugin<Project> {
shadowJar.configure { jar ->
jar.inputs.property("mainClassName", classNameProvider)
jar.doFirst {
jar.manifest.attributes["Main-Class"] = classNameProvider.get()
// Inject the Main-Class attribute if it is not already present.
if (!jar.manifest.attributes.contains("Main-Class")) {
jar.manifest.attributes["Main-Class"] = classNameProvider.get()
}
}
}
}
Expand Down

0 comments on commit cb08953

Please sign in to comment.