1818
1919import org .gradle .api .Action ;
2020import org .gradle .api .NamedDomainObjectContainer ;
21+ import org .gradle .api .NamedDomainObjectSet ;
2122import org .gradle .api .Project ;
2223import org .gradle .api .Task ;
2324import org .gradle .api .artifacts .Configuration ;
3536import org .gradle .api .plugins .JavaApplication ;
3637import org .gradle .api .plugins .JavaPluginExtension ;
3738import org .gradle .api .plugins .jvm .JvmTestSuite ;
39+ import org .gradle .api .provider .ListProperty ;
3840import org .gradle .api .provider .Property ;
3941import org .gradle .api .tasks .JavaExec ;
4042import org .gradle .api .tasks .SourceSet ;
4547import org .gradle .nativeplatform .MachineArchitecture ;
4648import org .gradle .nativeplatform .OperatingSystemFamily ;
4749import org .gradle .testing .base .TestSuite ;
50+ import org .gradlex .javamodule .packaging .internal .HostIdentification ;
4851import org .gradlex .javamodule .packaging .model .Target ;
4952import org .gradlex .javamodule .packaging .tasks .Jpackage ;
5053import org .gradlex .javamodule .packaging .tasks .ValidateHostSystemAction ;
6366abstract public class JavaModulePackagingExtension {
6467 private static final Attribute <Boolean > JAVA_MODULE_ATTRIBUTE = Attribute .of ("javaModule" , Boolean .class );
6568 private static final String INTERNAL = "internal" ;
69+ private static final String JPACKAGE = "jpackage" ;
6670
6771 abstract public Property <String > getApplicationName ();
6872 abstract public Property <String > getApplicationVersion ();
6973 abstract public Property <String > getApplicationDescription ();
7074 abstract public Property <String > getVendor ();
7175 abstract public Property <String > getCopyright ();
76+ abstract public ListProperty <String > getJlinkOptions ();
77+ abstract public ListProperty <String > getAddModules ();
7278 abstract public DirectoryProperty getJpackageResources ();
7379 abstract public ConfigurableFileCollection getResources ();
80+ abstract public Property <Boolean > getVerbose ();
7481
7582 private final NamedDomainObjectContainer <Target > targets = getObjects ().domainObjectContainer (Target .class );
7683
@@ -109,6 +116,16 @@ public Target target(String label, Action<? super Target> action) {
109116 return target ;
110117 }
111118
119+ /**
120+ * Configure all targets for the given OS.
121+ */
122+ @ SuppressWarnings ("unused" )
123+ public void targetsWithOs (String operatingSystem , Action <? super Target > action ) {
124+ NamedDomainObjectSet <Target > matches = targets .matching (t ->
125+ t .getOperatingSystem ().isPresent () && t .getOperatingSystem ().get ().equals (operatingSystem ));
126+ matches .all (action );
127+ }
128+
112129 /**
113130 * Set a 'primary target'. Standard Gradle tasks that are not bound to a specific target – like 'assemble' – use
114131 * this 'primary target'.
@@ -132,6 +149,7 @@ public Target primaryTarget(Target target) {
132149 /**
133150 * Set a test suite to be 'multi-target'. This registers an additional 'test' task for each target.
134151 */
152+ @ SuppressWarnings ({"unused" , "UnstableApiUsage" })
135153 public TestSuite multiTargetTestSuite (TestSuite testSuite ) {
136154 if (!(testSuite instanceof JvmTestSuite )) {
137155 return testSuite ;
@@ -220,7 +238,7 @@ private void registerTargetSpecificTasks(Target target, String applicationJarTas
220238 JavaPluginExtension java = getProject ().getExtensions ().getByType (JavaPluginExtension .class );
221239 JavaApplication application = getProject ().getExtensions ().getByType (JavaApplication .class );
222240
223- TaskProvider <Jpackage > jpackage = tasks .register ("jpackage" + capitalize (target .getName ()), Jpackage .class , t -> {
241+ TaskProvider <Jpackage > jpackage = tasks .register (JPACKAGE + capitalize (target .getName ()), Jpackage .class , t -> {
224242 t .getJavaInstallation ().convention (getJavaToolchains ().compilerFor (java .getToolchain ()).get ().getMetadata ());
225243 t .getOperatingSystem ().convention (target .getOperatingSystem ());
226244 t .getArchitecture ().convention (target .getArchitecture ());
@@ -230,14 +248,20 @@ private void registerTargetSpecificTasks(Target target, String applicationJarTas
230248 t .getModulePath ().from (runtimeClasspath );
231249
232250 t .getApplicationName ().convention (getApplicationName ());
233- t .getJpackageResources ().convention (getJpackageResources ().dir (target .getOperatingSystem ()));
251+ t .getJpackageResources ().from (getJpackageResources ().dir (target .getOperatingSystem ()));
234252 t .getApplicationDescription ().convention (getApplicationDescription ());
235253 t .getVendor ().convention (getVendor ());
236254 t .getCopyright ().convention (getCopyright ());
237255 t .getJavaOptions ().convention (application .getApplicationDefaultJvmArgs ());
256+ t .getJlinkOptions ().convention (getJlinkOptions ());
257+ t .getAddModules ().convention (getAddModules ());
238258 t .getOptions ().convention (target .getOptions ());
259+ t .getAppImageOptions ().convention (target .getAppImageOptions ());
239260 t .getPackageTypes ().convention (target .getPackageTypes ());
261+ t .getSingleStepPackaging ().convention (target .getSingleStepPackaging ());
240262 t .getResources ().from (getResources ());
263+ t .getTargetResources ().from (target .getTargetResources ());
264+ t .getVerbose ().convention (getVerbose ());
241265
242266 t .getDestination ().convention (getProject ().getLayout ().getBuildDirectory ().dir ("packages/" + target .getName ()));
243267 t .getTempDirectory ().convention (getProject ().getLayout ().getBuildDirectory ().dir ("tmp/jpackage/" + target .getName ()));
@@ -252,15 +276,25 @@ private void registerTargetSpecificTasks(Target target, String applicationJarTas
252276 t .setJvmArgs (application .getApplicationDefaultJvmArgs ());
253277 t .classpath (tasks .named ("jar" ), runtimeClasspath );
254278 });
279+ maybeAddJpackageLifecycleTask (tasks , target , jpackage );
280+ }
255281
256- String targetAssembleLifecycle = "assemble" + capitalize (target .getName ());
257- if (!tasks .getNames ().contains (targetAssembleLifecycle )) {
258- TaskProvider <Task > lifecycleTask = tasks .register (targetAssembleLifecycle , t -> {
282+ private void maybeAddJpackageLifecycleTask (TaskContainer tasks , Target target , TaskProvider <Jpackage > targetJpackage ) {
283+ // if a task already exists, do nothing to avoid conflciting with other plugins
284+ TaskProvider <Task > jpackage ;
285+ if (tasks .getNames ().contains (JPACKAGE )) {
286+ jpackage = tasks .named (JPACKAGE );
287+ } else {
288+ jpackage = tasks .register (JPACKAGE , t -> {
259289 t .setGroup (BUILD_GROUP );
260- t .setDescription ("Builds this project for " + target . getName () );
290+ t .setDescription ("Build the package for the current host system" );
261291 });
262292 }
263- tasks .named (targetAssembleLifecycle , t -> t .dependsOn (jpackage ));
293+ jpackage .configure (t -> {
294+ if (HostIdentification .isHostTarget (target )) {
295+ t .dependsOn (targetJpackage );
296+ }
297+ });
264298 }
265299
266300 private Configuration maybeCreateInternalConfiguration () {
0 commit comments