1010package org .elasticsearch .gradle .plugin ;
1111
1212import com .fasterxml .jackson .databind .ObjectMapper ;
13+ import com .fasterxml .jackson .databind .PropertyNamingStrategies ;
1314import com .fasterxml .jackson .databind .SerializationFeature ;
1415
1516import org .gradle .api .DefaultTask ;
3132import java .io .FileInputStream ;
3233import java .io .IOException ;
3334import java .io .InputStream ;
35+ import java .lang .module .ModuleFinder ;
3436import java .nio .charset .StandardCharsets ;
3537import java .nio .file .Files ;
3638import java .nio .file .Path ;
5456public abstract class GenerateTestBuildInfoTask extends DefaultTask {
5557
5658 public static final String DESCRIPTION = "generates plugin test dependencies file" ;
59+
5760 public static final String META_INF_VERSIONS_PREFIX = "META-INF/versions/" ;
61+ public static final String JAR_DESCRIPTOR_SUFFIX = ".jar" ;
5862
5963 public GenerateTestBuildInfoTask () {
6064 setDescription (DESCRIPTION );
@@ -79,7 +83,8 @@ public void generatePropertiesFile() throws IOException {
7983 Files .createDirectories (outputFile .getParent ());
8084
8185 try (var writer = Files .newBufferedWriter (outputFile , StandardCharsets .UTF_8 )) {
82- ObjectMapper mapper = new ObjectMapper ().configure (SerializationFeature .INDENT_OUTPUT , true );
86+ ObjectMapper mapper = new ObjectMapper ().configure (SerializationFeature .INDENT_OUTPUT , true )
87+ .setPropertyNamingStrategy (PropertyNamingStrategies .SNAKE_CASE );
8388 mapper .writeValue (writer , new OutputFileContents (getComponentName ().get (), buildLocationList ()));
8489 }
8590 }
@@ -114,7 +119,7 @@ private List<Location> buildLocationList() throws IOException {
114119 List <Location > locations = new ArrayList <>();
115120 for (File file : getCodeLocations ().get ().getFiles ()) {
116121 if (file .exists ()) {
117- if (file .getName ().endsWith (".jar" )) {
122+ if (file .getName ().endsWith (JAR_DESCRIPTOR_SUFFIX )) {
118123 extractLocationsFromJar (file , locations );
119124 } else if (file .isDirectory ()) {
120125 extractLocationsFromDirectory (file , locations );
@@ -158,8 +163,8 @@ private java.util.Optional<String> extractClassNameFromJar(JarFile jarFile) {
158163 }
159164
160165 /**
161- * look through the jar for the module name with
162- * each step commented inline
166+ * look through the jar for the module name with each step commented inline;
167+ * the algorithm used here is based on the described in {@link ModuleFinder#of}
163168 */
164169 private String extractModuleNameFromJar (File file , JarFile jarFile ) throws IOException {
165170 String moduleName = null ;
@@ -229,7 +234,7 @@ private String extractModuleNameFromJar(File file, JarFile jarFile) throws IOExc
229234 // if the jar does not have module-info.class and no module name in the manifest
230235 // default to the jar name without .jar and no versioning
231236 if (moduleName == null ) {
232- String jn = file .getName ().substring (0 , file .getName ().length () - ".jar" .length ());
237+ String jn = file .getName ().substring (0 , file .getName ().length () - JAR_DESCRIPTOR_SUFFIX .length ());
233238 Matcher matcher = Pattern .compile ("-(\\ d+(\\ .|$))" ).matcher (jn );
234239 if (matcher .find ()) {
235240 jn = jn .substring (0 , matcher .start ());
@@ -244,9 +249,9 @@ private String extractModuleNameFromJar(File file, JarFile jarFile) throws IOExc
244249 /**
245250 * find the first class and module when the class path entry is a directory
246251 */
247- private void extractLocationsFromDirectory (File dir , List <Location > locations ) throws IOException {
248- String className = extractClassNameFromDirectory (dir );
249- String moduleName = extractModuleNameFromDirectory (dir );
252+ private void extractLocationsFromDirectory (File directory , List <Location > locations ) throws IOException {
253+ String className = extractClassNameFromDirectory (directory );
254+ String moduleName = extractModuleNameFromDirectory (directory );
250255
251256 if (className != null && moduleName != null ) {
252257 locations .add (new Location (moduleName , className ));
@@ -257,15 +262,15 @@ private void extractLocationsFromDirectory(File dir, List<Location> locations) t
257262 * look through the directory to find the first unique class that isn't
258263 * module-info.class (which may not be unique) and avoid anonymous classes
259264 */
260- private String extractClassNameFromDirectory (File dir ) {
261- List <File > files = new ArrayList <>(List .of (dir ));
265+ private String extractClassNameFromDirectory (File directory ) throws IOException {
266+ List <File > files = new ArrayList <>(List .of (directory ));
262267 while (files .isEmpty () == false ) {
263268 File find = files .removeFirst ();
264269 if (find .exists ()) {
265270 if (find .getName ().endsWith (".class" )
266271 && find .getName ().equals ("module-info.class" ) == false
267272 && find .getName ().contains ("$" ) == false ) {
268- return find .getAbsolutePath ().substring (dir .getAbsolutePath ().length () + 1 );
273+ return find .getAbsolutePath ().substring (directory .getAbsolutePath ().length () + 1 );
269274 } else if (find .isDirectory ()) {
270275 files .addAll (Arrays .asList (find .listFiles ()));
271276 }
@@ -293,7 +298,7 @@ private String extractModuleNameFromDirectory(File dir) throws IOException {
293298 }
294299 }
295300 }
296- return getModuleName ().isPresent () ? getModuleName (). get () : null ;
301+ return getModuleName ().getOrNull () ;
297302 }
298303
299304 /**
0 commit comments