53
53
import java .nio .file .Path ;
54
54
import java .nio .file .StandardCopyOption ;
55
55
import java .util .ArrayList ;
56
+ import java .util .Arrays ;
56
57
import java .util .Date ;
57
58
import java .util .Enumeration ;
58
59
import java .util .List ;
@@ -78,6 +79,12 @@ public class Launch4jMojo extends AbstractMojo {
78
79
@ Parameter (defaultValue = "net.sf.launch4j" , required = true )
79
80
private String launch4jGroupId ;
80
81
82
+ // intentionally non-static non-final so it can be hacked with reflection if someone really needs to
83
+ private String DEF_REQADMMAN_RES = "META-INF/resources/manifest-requireAdminRights-v1.xml" ;
84
+
85
+ // intentionally non-static non-final so it can be hacked with reflection if someone really needs to
86
+ private String DEF_REQADMMAN_FILE = "target/classes/META-INF/manifest-requireAdminRights.xml" ;
87
+
81
88
/**
82
89
* Maven Session.
83
90
*/
@@ -239,6 +246,12 @@ public class Launch4jMojo extends AbstractMojo {
239
246
@ Parameter
240
247
private File icon ;
241
248
249
+ /**
250
+ * Whether the executable should ask for admin rights (Windows only).
251
+ */
252
+ @ Parameter (defaultValue = "false" )
253
+ private boolean requireAdminRights ;
254
+
242
255
/**
243
256
* Object files to include. Used for custom headers only.
244
257
*/
@@ -351,6 +364,8 @@ private void doExecute() throws MojoExecutionException {
351
364
return ;
352
365
}
353
366
367
+ processRequireAdminRights ();
368
+
354
369
fillSensibleJreDefaults ();
355
370
356
371
if (!disableVersionInfoDefaults ) {
@@ -506,6 +521,37 @@ private void fillSensibleJreDefaults() throws MojoExecutionException {
506
521
}
507
522
}
508
523
524
+ private void processRequireAdminRights () throws MojoExecutionException {
525
+ if (requireAdminRights ) {
526
+ getLog ().warn ("Modifying the resulting exe to always require Admin rights." );
527
+ getLog ().warn ("Make sure it's necessary. Consider writing your own manifest file." );
528
+
529
+ if (manifest != null ) {
530
+ getLog ().warn ("manifest param is already set, overriding. Make sure that's what's intended." );
531
+ }
532
+
533
+ try {
534
+ File metaInfDir = new File (basedir , "target/classes/META-INF" );
535
+ metaInfDir .mkdir ();
536
+
537
+ File manFile = new File (basedir , DEF_REQADMMAN_FILE );
538
+ byte [] manBytes = FileUtils .readResourceAsBytes (DEF_REQADMMAN_RES );
539
+
540
+ FileUtils .writeBytesIfDiff (manFile , manBytes );
541
+
542
+ byte [] savedBytes = FileUtils .readBytes (manFile );
543
+ if (Arrays .equals (manBytes , savedBytes )) {
544
+ getLog ().info ("Manifest file written to " + manFile );
545
+ }
546
+
547
+ manifest = manFile ;
548
+ } catch (Exception e ) {
549
+ getLog ().error (e );
550
+ throw new MojoExecutionException (e );
551
+ }
552
+ }
553
+ }
554
+
509
555
/**
510
556
* Prepares a little directory for launch4j to do its thing. Launch4j needs a bunch of object files
511
557
* (in the w32api and head directories) and the ld and windres binaries (in the bin directory).
@@ -904,6 +950,7 @@ public String toString() {
904
950
", stayAlive=" + stayAlive +
905
951
", restartOnCrash=" + restartOnCrash +
906
952
", icon=" + icon +
953
+ ", requireAdminRights=" + requireAdminRights +
907
954
", objs=" + objs +
908
955
", libs=" + libs +
909
956
", vars=" + vars +
0 commit comments