1212import org .gradle .api .DefaultTask ;
1313import org .gradle .api .file .DirectoryProperty ;
1414import org .gradle .api .file .FileCollection ;
15- import org .gradle .api .file .RegularFileProperty ;
1615import org .gradle .api .provider .Property ;
1716import org .gradle .api .tasks .Input ;
18- import org .gradle .api .tasks .InputFile ;
1917import org .gradle .api .tasks .InputFiles ;
20- import org .gradle .api .tasks .Optional ;
2118import org .gradle .api .tasks .OutputDirectory ;
2219import org .gradle .api .tasks .TaskAction ;
2320
2421import java .io .File ;
2522import java .io .IOException ;
23+ import java .io .UncheckedIOException ;
2624import java .nio .charset .StandardCharsets ;
2725import java .nio .file .Files ;
2826import java .nio .file .Path ;
29-
30- // TODO: 3 sets - testBuildInfo, internalClusterTest, external (as dependency of another)
27+ import java .util .ArrayList ;
28+ import java .util .Arrays ;
29+ import java .util .HashMap ;
30+ import java .util .List ;
31+ import java .util .Map ;
32+ import java .util .jar .JarFile ;
3133
3234public abstract class GenerateTestBuildInfoTask extends DefaultTask {
3335
@@ -41,14 +43,6 @@ public GenerateTestBuildInfoTask() {
4143 @ Input
4244 public abstract Property <String > getComponentName ();
4345
44- @ InputFile
45- @ Optional
46- public abstract RegularFileProperty getDescriptorFile ();
47-
48- @ InputFile
49- @ Optional
50- public abstract RegularFileProperty getPolicyFile ();
51-
5246 @ InputFiles
5347 public abstract Property <FileCollection > getCodeLocations ();
5448
@@ -57,6 +51,41 @@ public GenerateTestBuildInfoTask() {
5751
5852 @ TaskAction
5953 public void generatePropertiesFile () throws IOException {
54+ Map <String , String > classesToModules = new HashMap <>();
55+ for (File file : getCodeLocations ().get ().getFiles ()) {
56+ if (file .exists ()) {
57+ if (file .getName ().endsWith (".jar" )) {
58+ try (JarFile jarFile = new JarFile (file )) {
59+ jarFile .stream ()
60+ .filter (
61+ je -> je .getName ().startsWith ("META-INF" ) == false
62+ && je .getName ().equals ("module-info.class" ) == false
63+ && je .getName ().endsWith (".class" )
64+ )
65+ .findFirst ()
66+ .ifPresent (entry -> classesToModules .put (entry .getName (), "module-goes-here" ));
67+ } catch (IOException ioe ) {
68+ throw new UncheckedIOException (ioe );
69+ }
70+ } else if (file .isDirectory ()) {
71+ List <File > files = new ArrayList <>(List .of (file ));
72+ while (files .isEmpty () == false ) {
73+ File find = files .removeFirst ();
74+ if (find .exists ()) {
75+ if (find .isDirectory () && find .getName ().equals ("META_INF" ) == false ) {
76+ files .addAll (Arrays .asList (find .listFiles ()));
77+ } else if (find .getName ().equals ("module-info.class" ) == false && find .getName ().contains ("$" ) == false ) {
78+ classesToModules .put (find .getName (), "module-goes-here" );
79+ break ;
80+ }
81+ }
82+ }
83+ } else {
84+ throw new IllegalArgumentException ("Unrecognized classpath file: " + file );
85+ }
86+ }
87+ }
88+
6089 Path outputDirectory = getOutputDirectory ().get ().getAsFile ().toPath ();
6190 Files .createDirectories (outputDirectory );
6291 Path outputFile = outputDirectory .resolve (PROPERTIES_FILENAME );
@@ -68,24 +97,15 @@ public void generatePropertiesFile() throws IOException {
6897 writer .write (getComponentName ().get ());
6998 writer .write ("\" ,\n " );
7099
71- if (getDescriptorFile ().isPresent ()) {
72- writer .write (" \" descriptor\" : \" " );
73- writer .write (getDescriptorFile ().getAsFile ().get ().getAbsolutePath ());
74- writer .write ("\" ,\n " );
75- }
76-
77- if (getPolicyFile ().isPresent ()) {
78- writer .write (" \" policy\" : \" " );
79- writer .write (getPolicyFile ().getAsFile ().get ().getAbsolutePath ());
80- writer .write ("\" ,\n " );
81- }
82-
83100 writer .write (" \" locations\" : [\n " );
84101 StringBuilder sb = new StringBuilder ();
85- for (File jar : getCodeLocations ().get ()) {
86- sb .append (" \" " );
87- sb .append (jar .getAbsolutePath ());
88- sb .append ("\" ,\n " );
102+ for (Map .Entry <String , String > entry : classesToModules .entrySet ()) {
103+ sb .append (" {\n " );
104+ sb .append (" \" class\" : \" " );
105+ sb .append (entry .getKey ());
106+ sb .append ("\" ,\n \" module\" : \" " );
107+ sb .append (entry .getValue ());
108+ sb .append ("\" \n },\n " );
89109 }
90110 writer .write (sb .substring (0 , sb .length () - 2 ));
91111 writer .write ("\n ]\n }\n " );
0 commit comments