4848import org .gradle .testing .base .TestSuite ;
4949import org .gradlex .javamodule .packaging .model .Target ;
5050import org .gradlex .javamodule .packaging .tasks .Jpackage ;
51+ import org .gradlex .javamodule .packaging .tasks .ValidateHostSystemAction ;
5152
5253import javax .inject .Inject ;
5354import java .util .Arrays ;
@@ -86,10 +87,19 @@ abstract public class JavaModulePackagingExtension {
8687 abstract protected Project getProject ();
8788
8889
90+ /**
91+ * Retrieve the target with the given 'label'. If the target does not yet exist, it will be created.
92+ */
93+ @ SuppressWarnings ("unused" )
8994 public Target target (String label ) {
9095 return target (label , target -> {});
9196 }
9297
98+ /**
99+ * Register or update a target with the given 'label'. The 'label' uniquely identifies the target.
100+ * It is used for task names and can be chosen freely.
101+ * Details of the target are configured in the {@link Target} configuration action.
102+ */
93103 public Target target (String label , Action <? super Target > action ) {
94104 Target target ;
95105 if (targets .getNames ().contains (label )) {
@@ -102,6 +112,11 @@ public Target target(String label, Action<? super Target> action) {
102112 return target ;
103113 }
104114
115+ /**
116+ * Set a 'primary target'. Standard Gradle tasks that are not bound to a specific target – like 'assemble' – use
117+ * this 'primary target'.
118+ */
119+ @ SuppressWarnings ("unused" )
105120 public Target primaryTarget (Target target ) {
106121 SourceSetContainer sourceSets = getProject ().getExtensions ().getByType (SourceSetContainer .class );
107122 ConfigurationContainer configurations = getProject ().getConfigurations ();
@@ -117,22 +132,25 @@ public Target primaryTarget(Target target) {
117132 return target ;
118133 }
119134
135+ /**
136+ * Set a test suite to be 'multi-target'. This registers an additional 'test' task for each target.
137+ */
120138 public TestSuite multiTargetTestSuite (TestSuite testSuite ) {
121139 if (!(testSuite instanceof JvmTestSuite )) {
122140 return testSuite ;
123141 }
124142
125143 JvmTestSuite suite = (JvmTestSuite ) testSuite ;
126- targets .all (target -> {
127- suite . getTargets ().register ( testSuite . getName () + capitalize ( target .getName ()), testTarget -> {
128- testTarget . getTestTask ().configure ( task -> {
129- ConfigurationContainer configurations = getProject (). getConfigurations ();
130- task . setClasspath ( configurations . getByName ( target . getName () + capitalize ( suite . getSources (). getRuntimeClasspathConfigurationName ())). plus (
131- getObjects (). fileCollection (). from ( getProject (). getTasks (). named (suite .getSources ().getJarTaskName ())))
132- );
133- } );
134- } );
135- });
144+ targets .all (target -> suite . getTargets (). register ( testSuite . getName () + capitalize ( target . getName ()), testTarget -> testTarget . getTestTask (). configure ( task -> {
145+ task . getInputs ().property ( "operatingSystem" , target .getOperatingSystem ());
146+ task . getInputs ().property ( "architecture" , target . getArchitecture ());
147+
148+ ConfigurationContainer configurations = getProject (). getConfigurations ();
149+ task . setClasspath ( configurations . getByName ( target . getName () + capitalize (suite .getSources ().getRuntimeClasspathConfigurationName ())). plus (
150+ getObjects (). fileCollection (). from ( getProject (). getTasks (). named ( suite . getSources (). getJarTaskName ())))
151+ );
152+ task . doFirst ( new ValidateHostSystemAction () );
153+ }))) ;
136154
137155 return testSuite ;
138156 }
0 commit comments