2727import org .gradle .api .tasks .Classpath ;
2828import org .gradle .api .tasks .Input ;
2929import org .gradle .api .tasks .InputDirectory ;
30+ import org .gradle .api .tasks .InputFiles ;
3031import org .gradle .api .tasks .Nested ;
3132import org .gradle .api .tasks .Optional ;
3233import org .gradle .api .tasks .OutputDirectory ;
4243import java .nio .file .Files ;
4344import java .security .MessageDigest ;
4445import java .security .NoSuchAlgorithmException ;
46+ import java .util .Arrays ;
47+ import java .util .List ;
48+ import java .util .stream .Collectors ;
4549
4650import static java .util .Objects .requireNonNull ;
4751import static org .gradle .nativeplatform .OperatingSystemFamily .WINDOWS ;
@@ -76,7 +80,11 @@ abstract public class Jpackage extends DefaultTask {
7680
7781 @ InputDirectory
7882 @ PathSensitive (PathSensitivity .RELATIVE )
79- abstract public DirectoryProperty getApplicationResources ();
83+ abstract public DirectoryProperty getJpackageResources ();
84+
85+ @ InputFiles
86+ @ PathSensitive (PathSensitivity .RELATIVE )
87+ abstract public ConfigurableFileCollection getResources ();
8088
8189 @ Input
8290 @ Optional
@@ -95,11 +103,9 @@ abstract public class Jpackage extends DefaultTask {
95103 @ Input
96104 abstract public ListProperty <String > getPackageTypes ();
97105
98-
99106 @ OutputDirectory
100107 abstract public DirectoryProperty getDestination ();
101108
102-
103109 @ Inject
104110 abstract protected FileOperations getFiles ();
105111
@@ -117,64 +123,100 @@ public void runJpackage() throws Exception {
117123
118124 validateHostSystem (arch , hostArch , os , hostOs );
119125
120- Directory resourcesDir = getDestination ().dir ("additional-resources" ).get ();
126+ Directory tmpDir = getDestination ().dir ("tmp" ).get ();
127+ Directory resourcesDir = tmpDir .dir ("jpackage-resources" );
128+ Directory appImageParent = tmpDir .dir ("app-image" );
129+ //noinspection ResultOfMethodCallIgnored
130+ resourcesDir .getAsFile ().mkdirs ();
131+
121132 getFiles ().copy (c -> {
122- c .from (getApplicationResources ());
133+ c .from (getJpackageResources ());
123134 c .into (resourcesDir );
124135 c .rename (f -> f .replace ("icon" , getApplicationName ().get ()));
125136 });
126- //noinspection ResultOfMethodCallIgnored
127- resourcesDir .getAsFile ().mkdirs (); // if no files were copied, make sure there is an empty directory
128137
129- String executable = WINDOWS .equals (os ) ? "bin/jpackage.exe" : "bin/jpackage" ;
138+ String executableName = WINDOWS .equals (os ) ? "jpackage.exe" : "jpackage" ;
139+ String jpackage = getJavaInstallation ().get ().getInstallationPath ().file ("bin/" + executableName ).getAsFile ().getAbsolutePath ();
140+
141+ // create app image folder
142+ getExec ().exec (e -> {
143+ e .commandLine (
144+ jpackage ,
145+ "--type" ,
146+ "app-image" ,
147+ "--module" ,
148+ getMainModule ().get (),
149+ "--resource-dir" ,
150+ resourcesDir .getAsFile ().getPath (),
151+ "--app-version" ,
152+ getVersion ().get (),
153+ "--module-path" ,
154+ getModulePath ().getAsPath (),
155+ "--name" ,
156+ getApplicationName ().get (),
157+ "--dest" ,
158+ appImageParent .getAsFile ().getPath ()
159+ );
160+ if (getApplicationDescription ().isPresent ()) {
161+ e .args ("--description" , getApplicationDescription ().get ());
162+ }
163+ if (getVendor ().isPresent ()) {
164+ e .args ("--vendor" , getVendor ().get ());
165+ }
166+ if (getCopyright ().isPresent ()) {
167+ e .args ("--copyright" , getCopyright ().get ());
168+ }
169+ for (String option : getOptions ().get ()) {
170+ e .args (option );
171+ }
172+ for (String javaOption : getJavaOptions ().get ()) {
173+ e .args ("--java-options" , javaOption );
174+ }
175+ });
176+
177+ File appImageFolder = requireNonNull (appImageParent .getAsFile ().listFiles ())[0 ];
178+ File appResourcesFolder ;
179+ if (os .contains ("macos" )) {
180+ appResourcesFolder = new File (appImageFolder , "Contents/app" );
181+ } else if (os .contains ("windows" )) {
182+ appResourcesFolder = new File (appImageFolder , "app" );
183+ } else {
184+ appResourcesFolder = new File (appImageFolder , "lib/app" );
185+ }
186+
187+ // copy additional resource into app-image folder
188+ getFiles ().copy (c -> {
189+ c .from (getResources ());
190+ c .into (appResourcesFolder );
191+ });
192+
193+ // package with additional resources
130194 getPackageTypes ().get ().forEach (packageType ->
131195 getExec ().exec (e -> {
132196 e .commandLine (
133- getJavaInstallation (). get (). getInstallationPath (). file ( executable ). getAsFile (). getAbsolutePath () ,
197+ jpackage ,
134198 "--type" ,
135199 packageType ,
136- "--module" ,
137- getMainModule ().get (),
138- "--resource-dir" ,
139- resourcesDir ,
140- "--app-version" ,
141- getVersion ().get (),
142- "--module-path" ,
143- getModulePath ().getAsPath (),
144- "--name" ,
145- getApplicationName ().get (),
200+ "--app-image" ,
201+ appImageFolder .getPath (),
146202 "--dest" ,
147203 getDestination ().get ().getAsFile ().getPath ()
148204 );
149- if (getApplicationDescription ().isPresent ()) {
150- e .args ("--description" , getApplicationDescription ().get ());
151- }
152- if (getVendor ().isPresent ()) {
153- e .args ("--vendor" , getVendor ().get ());
154- }
155- if (getCopyright ().isPresent ()) {
156- e .args ("--copyright" , getCopyright ().get ());
157- }
158- for (String option : getOptions ().get ()) {
159- e .args (option );
160- }
161- for (String javaOption : getJavaOptions ().get ()) {
162- e .args ("--java-options" , javaOption );
163- }
164205 })
165206 );
166207
167- getFiles ().delete (resourcesDir );
208+ // getFiles().delete(tmpDir );
168209
169210 generateChecksums ();
170211 }
171212
172213 private void generateChecksums () throws NoSuchAlgorithmException , IOException {
173- File dest = getDestination ().get ().getAsFile ();
174- for (File result : requireNonNull (dest .listFiles ())) {
214+ File destination = getDestination ().get ().getAsFile ();
215+ List <File > allFiles = Arrays .stream (requireNonNull (destination .listFiles ())).filter (File ::isFile ).collect (Collectors .toList ());
216+ for (File result : allFiles ) {
175217 MessageDigest digest = MessageDigest .getInstance ("SHA-256" );
176218 byte [] encoded = digest .digest (Files .readAllBytes (result .toPath ()));
177- Files .write (new File (dest , result .getName () + ".sha256" ).toPath (), bytesToHex (encoded ).getBytes ());
219+ Files .write (new File (destination , result .getName () + ".sha256" ).toPath (), bytesToHex (encoded ).getBytes ());
178220 }
179221 }
180222
0 commit comments