|
| 1 | +/* |
| 2 | + * Copyright the GradleX team. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.gradlex.javamodule.packaging.test; |
| 18 | + |
| 19 | +import org.gradlex.javamodule.packaging.test.fixture.GradleBuild; |
| 20 | +import org.junit.jupiter.api.BeforeEach; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +import static org.assertj.core.api.Assertions.assertThat; |
| 24 | + |
| 25 | +/** |
| 26 | + * Tests for setting various options for jpackage or the underlying jlink. |
| 27 | + * The tests are OS-dependent and should run on each operating system once. |
| 28 | + */ |
| 29 | +class JavaModulePackagingOptionsTest { |
| 30 | + |
| 31 | + GradleBuild build = new GradleBuild(); |
| 32 | + |
| 33 | + @BeforeEach |
| 34 | + void setup() { |
| 35 | + var macosArch = System.getProperty("os.arch").contains("aarch") ? "aarch64" : "x86-64"; |
| 36 | + build.appBuildFile.appendText(""" |
| 37 | + version = "1.0" |
| 38 | + javaModulePackaging { |
| 39 | + target("macos") { |
| 40 | + operatingSystem.set("macos") |
| 41 | + architecture.set("%s") |
| 42 | + packageTypes.set(listOf("dmg")) |
| 43 | + } |
| 44 | + target("ubuntu") { |
| 45 | + operatingSystem.set("linux") |
| 46 | + architecture.set("x86-64") |
| 47 | + packageTypes.set(listOf("deb")) |
| 48 | + } |
| 49 | + target("windows") { |
| 50 | + operatingSystem.set("windows") |
| 51 | + architecture.set("x86-64") |
| 52 | + packageTypes.set(listOf("exe")) |
| 53 | + } |
| 54 | + } |
| 55 | + """.formatted(macosArch)); |
| 56 | + build.appModuleInfoFile.writeText(""" |
| 57 | + module org.example.app { |
| 58 | + } |
| 59 | + """); |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void can_configure_jlink_options() { |
| 65 | + build.appBuildFile.appendText(""" |
| 66 | + javaModulePackaging { |
| 67 | + jlinkOptions.addAll( |
| 68 | + "--ignore-signing-information", |
| 69 | + "--compress", "zip-6", |
| 70 | + "--no-header-files", |
| 71 | + "--no-man-pages", |
| 72 | + "--bind-services", |
| 73 | + "--unsupported-option" |
| 74 | + ) |
| 75 | + } |
| 76 | + """); |
| 77 | + |
| 78 | + var result = build.fail(":app:jpackage"); |
| 79 | + |
| 80 | + // The error shows that all options before '--unsupported-option' are passed through to jlink |
| 81 | + assertThat(result.getOutput()).contains("jlink failed with: Error: unknown option: --unsupported-option"); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void can_configure_java_options() { |
| 86 | + build.appBuildFile.appendText(""" |
| 87 | + application { |
| 88 | + applicationDefaultJvmArgs = listOf( |
| 89 | + "-XX:+UnlockExperimentalVMOptions", |
| 90 | + "-XX:+UseCompactObjectHeaders", |
| 91 | + "-Xmx1g", |
| 92 | + "-Dsome.prop=some.val" |
| 93 | + ) |
| 94 | + } |
| 95 | + """); |
| 96 | + |
| 97 | + build.build(":app:jpackage"); |
| 98 | + |
| 99 | + assertThat(build.file("app/build/packages/macos/app.app/Contents/app/app.cfg").getAsPath()).hasContent(""" |
| 100 | + [Application] |
| 101 | + app.mainmodule=org.example.app/org.example.app.Main |
| 102 | + |
| 103 | + [JavaOptions] |
| 104 | + java-options=-Djpackage.app-version=1.0 |
| 105 | + java-options=-XX:+UnlockExperimentalVMOptions |
| 106 | + java-options=-XX:+UseCompactObjectHeaders |
| 107 | + java-options=-Xmx1g |
| 108 | + java-options=-Dsome.prop=some.val |
| 109 | + """); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + void can_configure_add_modules() { |
| 114 | + build.appBuildFile.appendText(""" |
| 115 | + javaModulePackaging { |
| 116 | + addModules.addAll("com.acme.boo") |
| 117 | + } |
| 118 | + """); |
| 119 | + |
| 120 | + var result = build.fail(":app:jpackage"); |
| 121 | + |
| 122 | + // The error shows that the option is passed on to jlink |
| 123 | + assertThat(result.getOutput()).contains("jlink failed with: Error: Module com.acme.boo not found"); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + void can_set_verbose_option() { |
| 128 | + build.appBuildFile.appendText(""" |
| 129 | + javaModulePackaging { |
| 130 | + verbose.set(true) |
| 131 | + } |
| 132 | + """); |
| 133 | + |
| 134 | + var result = build.build(":app:jpackage"); |
| 135 | + |
| 136 | + assertThat(result.getOutput()).contains("Creating app package: app.app in"); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + void can_set_target_specific_option() { |
| 141 | + build.appBuildFile.appendText(""" |
| 142 | + javaModulePackaging { |
| 143 | + targetsWithOs("windows") { |
| 144 | + singleStepPackaging.set(true) |
| 145 | + options.addAll("--dummy") |
| 146 | + appImageOptions.addAll("--dummyimg") // no effect due to single-step |
| 147 | + } |
| 148 | + targetsWithOs("linux") { |
| 149 | + singleStepPackaging.set(true) |
| 150 | + options.addAll("--dummy") |
| 151 | + appImageOptions.addAll("--dummyimg") // no effect due to single-step |
| 152 | + } |
| 153 | + targetsWithOs("macos") { |
| 154 | + singleStepPackaging.set(true) |
| 155 | + options.addAll("--dummy") |
| 156 | + appImageOptions.addAll("--dummyimg") // no effect due to single-step |
| 157 | + } |
| 158 | + } |
| 159 | + """); |
| 160 | + |
| 161 | + var result = build.fail(":app:jpackage"); |
| 162 | + |
| 163 | + assertThat(result.getOutput()).contains("Error: Invalid Option: [--dummy]"); |
| 164 | + } |
| 165 | + |
| 166 | + @Test |
| 167 | + void can_set_target_specific_option_for_app_image() { |
| 168 | + build.appBuildFile.appendText(""" |
| 169 | + javaModulePackaging { |
| 170 | + targetsWithOs("windows") { |
| 171 | + options.addAll("--dummy") // no effect as app-image fails first |
| 172 | + appImageOptions.addAll("--dummyimg") |
| 173 | + } |
| 174 | + targetsWithOs("linux") { |
| 175 | + options.addAll("--dummy") // no effect as app-image fails first |
| 176 | + appImageOptions.addAll("--dummyimg") |
| 177 | + } |
| 178 | + targetsWithOs("macos") { |
| 179 | + options.addAll("--dummy") // no effect as app-image fails first |
| 180 | + appImageOptions.addAll("--dummyimg") |
| 181 | + } |
| 182 | + } |
| 183 | + """); |
| 184 | + |
| 185 | + var result = build.fail(":app:jpackage"); |
| 186 | + |
| 187 | + assertThat(result.getOutput()).contains("Error: Invalid Option: [--dummyimg]"); |
| 188 | + } |
| 189 | + |
| 190 | + @Test |
| 191 | + void can_build_package_in_one_step() { |
| 192 | + build.appBuildFile.appendText(""" |
| 193 | + javaModulePackaging { |
| 194 | + targetsWithOs("windows") { singleStepPackaging.set(true) } |
| 195 | + targetsWithOs("linux") { singleStepPackaging.set(true) } |
| 196 | + targetsWithOs("macos") { singleStepPackaging.set(true) } |
| 197 | + } |
| 198 | + """); |
| 199 | + |
| 200 | + build.build(":app:jpackage"); |
| 201 | + |
| 202 | + assertThat(build.file("app/build/packages/macos/app-1.0.dmg").getAsPath()).exists(); |
| 203 | + assertThat(build.file("app/build/packages/macos/app-1.0.dmg.sha256").getAsPath()).exists(); |
| 204 | + assertThat(build.projectDir.dir("app/build/packages/macos").getAsPath()).isDirectoryNotContaining( |
| 205 | + f -> f.toFile().isDirectory()); |
| 206 | + } |
| 207 | +} |
0 commit comments